renxiao2003 发表于 2011-12-21 08:44

异步调用WebService

<A class=postTitle2 id=ctl02_TitleUrl href="http://www.cnblogs.com/hoojo/archive/2011/03/16/1985744.html"><FONT color=#1a8bc8>八、 异步调用WebService</FONT></A>
<DIV id=cnblogs_post_body>
<P>异步,说到异步需要首先将以下同步。同步就是代码按照顺序执行,当前面的代码的请求没有正常返回结果的情况下,后面的代码是不能运行。而异步正好和这点不同,异步是代码运行后,不管当前的请求是否返回结果,后面的代码都会继续运行。</P>
<P>关于异步在此就不再赘述了,有兴趣的可以去网上查查这方面的资料。</P>
<P>1、 编写服务器端的代码。</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_498155 .click="this.style.display='none'; document.getElementById('Code_Closed_Text_498155').style.display='none'; document.getElementById('Code_Open_Image_498155').style.display='inline'; document.getElementById('Code_Open_Text_498155').style.display='inline';" height=16 src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 align=top><IMG id=Code_Open_Image_498155 style="DISPLAY: none" .click="this.style.display='none'; document.getElementById('Code_Open_Text_498155').style.display='none'; getElementById('Code_Closed_Image_498155').style.display='inline'; getElementById('Code_Closed_Text_498155').style.display='inline';" height=16 src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 align=top><SPAN class=cnblogs_code_Collapse id=Code_Closed_Text_498155><FONT face="Courier New">代码</FONT></SPAN><SPAN id=Code_Open_Text_498155 style="DISPLAY: none"><FONT face="Courier New"><SPAN style="COLOR: #0000ff">package</SPAN> com.hoo.service;

</FONT><FONT face="Courier New"><SPAN style="COLOR: #008000">/**
* &lt;b&gt;function:&lt;/b&gt;异步WebService服务器端代码
* @author hoojo
* @createDate 2011-3-14 上午08:16:59
* @file AsynchronousService.java
* @package com.hoo.service
* @project Axis2WebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/</SPAN>
<SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">class</SPAN> AsynchronousService {
        <SPAN style="COLOR: #0000ff">public</SPAN> String execute() {
                System.out.println("<SPAN style="COLOR: #8b0000">正在执行此代码……</SPAN>");
                <SPAN style="COLOR: #008000">//延迟5秒后,返回结果</SPAN>
                <SPAN style="COLOR: #0000ff">try</SPAN> {
                        Thread.sleep(5000);
                } <SPAN style="COLOR: #0000ff">catch</SPAN> (InterruptedException e) {
                        e.printStackTrace();
                }
                <SPAN style="COLOR: #0000ff">return</SPAN> "<SPAN style="COLOR: #8b0000">完成</SPAN>";
        }
}</FONT></DIV><BR></SPAN>
<P><FONT face="Courier New"></FONT></P>
<P>2、 services.xml文件,创建aar文件,然后复制aar文件到\webapps\axis2\WEB-INF\services目录下</P>
<P>services.xml</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_692644 .click="this.style.display='none'; document.getElementById('Code_Closed_Text_692644').style.display='none'; document.getElementById('Code_Open_Image_692644').style.display='inline'; document.getElementById('Code_Open_Text_692644').style.display='inline';" height=16 src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 align=top><IMG id=Code_Open_Image_692644 style="DISPLAY: none" .click="this.style.display='none'; document.getElementById('Code_Open_Text_692644').style.display='none'; getElementById('Code_Closed_Image_692644').style.display='inline'; getElementById('Code_Closed_Text_692644').style.display='inline';" height=16 src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 align=top><SPAN class=cnblogs_code_Collapse id=Code_Closed_Text_692644><FONT face="Courier New">代码</FONT></SPAN><SPAN id=Code_Open_Text_692644 style="DISPLAY: none"><FONT face="Courier New"><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">service</SPAN> <SPAN style="COLOR: #ff0000">name</SPAN>=<SPAN style="COLOR: #0000ff">"AsyncService"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN>
        <SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">description</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN>
                AsyncService
           <SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">description</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN>
        <SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">parameter</SPAN> <SPAN style="COLOR: #ff0000">name</SPAN>=<SPAN style="COLOR: #0000ff">"ServiceClass"</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN>
                com.hoo.service.AsynchronousService
           <SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">parameter</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN>
        <SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">messageReceivers</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN>
                <SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">messageReceiver</SPAN> <SPAN style="COLOR: #ff0000">mep</SPAN>=<SPAN style="COLOR: #0000ff">"http://www.w3.org/2004/08/wsdl/in-out"</SPAN>
                        <SPAN style="COLOR: #ff0000">class</SPAN>=<SPAN style="COLOR: #0000ff">"org.apache.axis2.rpc.receivers.RPCMessageReceiver"</SPAN> <SPAN style="COLOR: #0000ff">/&gt;</SPAN>
                <SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">messageReceiver</SPAN> <SPAN style="COLOR: #ff0000">mep</SPAN>=<SPAN style="COLOR: #0000ff">"http://www.w3.org/2004/08/wsdl/in-only"</SPAN>
                        <SPAN style="COLOR: #ff0000">class</SPAN>=<SPAN style="COLOR: #0000ff">"org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"</SPAN> <SPAN style="COLOR: #0000ff">/&gt;</SPAN>
        <SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">messageReceivers</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN>
<SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">service</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN></FONT></DIV><BR></SPAN>
<P>3、 编写客户端测试代码</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_670056 .click="this.style.display='none'; document.getElementById('Code_Closed_Text_670056').style.display='none'; document.getElementById('Code_Open_Image_670056').style.display='inline'; document.getElementById('Code_Open_Text_670056').style.display='inline';" height=16 src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 align=top><IMG id=Code_Open_Image_670056 style="DISPLAY: none" .click="this.style.display='none'; document.getElementById('Code_Open_Text_670056').style.display='none'; getElementById('Code_Closed_Image_670056').style.display='inline'; getElementById('Code_Closed_Text_670056').style.display='inline';" height=16 src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" width=11 align=top><SPAN class=cnblogs_code_Collapse id=Code_Closed_Text_670056><FONT face="Courier New">代码</FONT></SPAN><SPAN id=Code_Open_Text_670056 style="DISPLAY: none"><FONT face="Courier New"><SPAN style="COLOR: #0000ff">package</SPAN> com.hoo.service;

<SPAN style="COLOR: #0000ff">import</SPAN> java.io.IOException;
<SPAN style="COLOR: #0000ff">import</SPAN> javax.xml.namespace.QName;
<SPAN style="COLOR: #0000ff">import</SPAN> org.apache.axis2.addressing.EndpointReference;
<SPAN style="COLOR: #0000ff">import</SPAN> org.apache.axis2.client.Options;
<SPAN style="COLOR: #0000ff">import</SPAN> org.apache.axis2.client.async.AxisCallback;
<SPAN style="COLOR: #0000ff">import</SPAN> org.apache.axis2.context.MessageContext;
<SPAN style="COLOR: #0000ff">import</SPAN> org.apache.axis2.rpc.client.RPCServiceClient;

</FONT><FONT face="Courier New"><SPAN style="COLOR: #008000">/**
* &lt;b&gt;function:&lt;/b&gt;异步WebService客户端代码
* @author hoojo
* @createDate 2011-3-14 上午09:00:03
* @file AsynchronousServiceClient.java
* @package com.hoo.service
* @project Axis2WebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/</SPAN>
<SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">class</SPAN> AsynchronousServiceClient {

        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">static</SPAN> <SPAN style="COLOR: #0000ff">void</SPAN> main(String[] args) <SPAN style="COLOR: #0000ff">throws</SPAN> IOException {
                String target = "<SPAN style="COLOR: #8b0000">http://localhost:8080/axis2/services/AsyncService?wsdl</SPAN>";
                RPCServiceClient client = <SPAN style="COLOR: #0000ff">new</SPAN> RPCServiceClient();
                Options options = client.getOptions();
                options.setManageSession(<SPAN style="COLOR: #0000ff">true</SPAN>);
               
                EndpointReference epr = <SPAN style="COLOR: #0000ff">new</SPAN> EndpointReference(target);
                options.setTo(epr);
               
                QName qname = <SPAN style="COLOR: #0000ff">new</SPAN> QName("<SPAN style="COLOR: #8b0000">http://service.hoo.com</SPAN>", "<SPAN style="COLOR: #8b0000">execute</SPAN>");
                <SPAN style="COLOR: #008000">//指定调用的方法和传递参数数据,及设置返回值的类型</SPAN>
                client.invokeNonBlocking(qname, <SPAN style="COLOR: #0000ff">new</SPAN> Object[] {}, <SPAN style="COLOR: #0000ff">new</SPAN> AxisCallback() {
                       
                        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">void</SPAN> onMessage(MessageContext ctx) {
                                System.out.println(ctx.getEnvelope());
                                System.out.println("<SPAN style="COLOR: #8b0000">Message:</SPAN>" + ctx.getEnvelope().getFirstElement().getFirstElement().getFirstElement().getText());
                        }
                       
                        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">void</SPAN> onFault(MessageContext ctx) {
                        }
                       
                        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">void</SPAN> onError(Exception ex) {
                        }
                       
                        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">void</SPAN> onComplete() {
                        }
                });
                System.out.println("<SPAN style="COLOR: #8b0000">异步WebService</SPAN>");
                <SPAN style="COLOR: #008000">//阻止程序退出</SPAN>
      System.in.read();
        }
}</FONT></DIV><BR></SPAN>
<P><FONT face="Courier New"></FONT></P>
<P>上面是异步调用WebService的代码,调用的方法是client.invokeNonBlocking,这个方法有三个参数,参数一是执行的方法签名,参数二是执行该方法的参数,参数三是异步回调,这里隐式实现AxiaCallback接口</P>
<P>注意的是运行程序的时候要用Debug方式运行。</P></DIV>
页: [1]
查看完整版本: 异步调用WebService