1、新建webservice服务类,AsyncService.java
public class AsyncService { public static String getServerTime() throws InterruptedException{ Thread.sleep(5000); return "测试"; } }
?
2、将AsyncService编译后的clas文件放到 axis2\WEB-INF\pojo 目录下。
?
3、新建客户端类 AsyncServiceClient.java
package client; import java.io.IOException; import java.util.Calendar; import javax.xml.namespace.QName; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.async.AxisCallback; import org.apache.axis2.context.MessageContext; import org.apache.axis2.rpc.client.RPCServiceClient; public class AsyncServiceClient { public static void main(String[] args) throws IOException, InterruptedException { RPCServiceClient client = new RPCServiceClient(); //设置webservice服务地址 Options options = client.getOptions(); EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/AsyncService"); options.setTo(targetEPR); //设置要调用的方法名、命名空间 QName qName = new QName("http://ws.apache.org/axis2","getServerTime"); //设置入参 Object[] inParams = new Object[]{}; //开始异步调用 client.invokeNonBlocking(qName, inParams, new AxisCallback(){ public void onComplete() { } public void onError(Exception arg0) { } public void onFault(MessageContext arg0) { } public void onMessage(MessageContext mc) { System.out.println(Thread.currentThread().getName() +"线程接收到返回值时间:"+getClientTime()); // 输出返回值 System.out.println("服务器端返回信息:"+mc.getEnvelope()); } }); System.out.println(Thread.currentThread().getName() +"线程执行完毕时间:"+getClientTime()); System.out.println(Thread.currentThread().getName()+"开始休眠"); //使主线程休眠20s,以等待异步调用结束。 Thread.sleep(20000); System.out.println(Thread.currentThread().getName()+"结束休眠"); } /* * 获取当前系统时间 */ public static String getClientTime(){ Calendar calen = Calendar.getInstance(); int year = calen.get(Calendar.YEAR); int month = calen.get(Calendar.MONTH)+1;//需加一 int day = calen.get(Calendar.DAY_OF_MONTH); int hour = calen.get(Calendar.HOUR_OF_DAY); int minute = calen.get(Calendar.MINUTE); int seconds = calen.get(Calendar.SECOND); int weekday = calen.get(Calendar.DAY_OF_WEEK)-1;//需减一 return year+"年"+month+"月"+day+"日"+hour+"时"+minute+"分"+seconds+"秒,礼拜"+weekday; } }
?
?
4、执行客户端类,输出结果如下:
?
main线程执行完毕时间:2012年3月16日16时37分56秒,礼拜5 |
?
返回的xml格式化后如下:
?
?
http://huangqiqing123.iteye.com/blog/1455417
?