test

WebService给人最直观的感受就是由一个个方法组成,并在客户端经过SOAP协议调用这些方法。这些方法可能有返回值,也可能没有返回值。虽然这样能够完成一些工具,但这些被调用的方法是孤立的,当一个方法被调用后,在其余的方法中没法得到这个方法调用后的状态,也就是说没法保留状态。java

读者能够想象,这对于一个完整的应用程序,没法保留状态,就意味着只依靠WebService很难完成所有的工做。例如,一个完整的应用系统都须要进行登陆,这在Web应用中使用Session来保存用户登陆状态,而若是用WebService的方法来进行登陆处理,没法保存登陆状态是很是使人尴尬的。固然,这也能够经过其余的方法来解决,如在服务端使用static变量来保存用户状态,并发送一个id到客户端,经过在服务端和客户端传递这个id来取得相应的用户状态。这很是相似于Web应用中经过SessionCookie来管理用户状态。但这就须要由开发人员作不少工做,不过幸亏Axis2为咱们提供了WebService状态管理的功能。apache

使用Axis2来管理WebService的状态基本上对于开发人员是透明的。在WebService类须要使用org.apache.axis2.context.MessageContextorg.apache.axis2.context.ServiceContext类来保存与得到保存在服务端的状态信息,这有些象使用HttpSession接口的getAttributesetAttribute方法得到与设置Session域属性。session

除此以外,还须要修改services.xml文件的内容,为<service>元素加一个scope属性,该属性有四个可取的值:Application, SOAPSession, TransportSession, Request,不过要注意一下,虽然Axis2的官方文档将这四个值的单词首字母和缩写字母都写成了大写,但经笔者测试,必须所有小写才有效,也就是这四个值应为:applicationsoapsessiontransportsessionrequest,其中requestscope属性的默认值。读者能够选择使用transportsessionapplication分别实现同一个WebService类和跨WebService类的会话管理。并发

在客户端须要使用setManageSession(true)打开Session管理功能。app

综上所述,实现同一个WebServiceSession管理须要以下三步:ide

1. 使用MessageContextServiceContext得到与设置key-value对。工具

2. 为要进行Session管理的WebService类所对应的<service>元素添加一个scope属性,并将该属性值设为transportsession测试

3. 在客户端使用setManageSession(true)打开Session管理功能。spa

下面是一个在同一个WebService类中管理Session的例子。

先创建一个WebService类,代码以下:
orm


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->package service;
import org.apache.axis2.context.ServiceContext;
import org.apache.axis2.context.MessageContext;
public class LoginService
{
    
public boolean login(String username, String password)
    {
        
if("bill".equals(username) && "1234".equals(password))
        {
            
//  第1步:设置key-value对
            MessageContext mc = MessageContext.getCurrentMessageContext();
            ServiceContext sc 
= mc.getServiceContext();
            sc.setProperty(
"login""成功登陆");    
            
return true;
        }
        
else
        {
            
return false;
        }
    }
    
public String getLoginMsg()
    {
        
//  第1步:得到key-value对中的value
        MessageContext mc = MessageContext.getCurrentMessageContext();
        ServiceContext sc 
= mc.getServiceContext();
        
return (String)sc.getProperty("login");    
    }
}

Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 <style. /* Style. Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style.

LoginService类中有两个方法:logingetLoginMsg,若是login方法登陆成功,会将“成功登陆”字符串保存在ServiceContext对象中。若是在login方法返回true后调用getLoginMsg方法,就会返回“成功登陆”。

下面是LoginService类的配置代码(services.xml):

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><!--  第2步:添加scope属性  -->
<service name="loginService" scope="transportsession">
    
<description>
        登陆服务
        
</description>
    
<parameter name="ServiceClass">
        service.LoginService
        
</parameter>
    
<messageReceivers>
        
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
            class
="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    
</messageReceivers>
</service>

Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 <style. /* Style. Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style.

使用以下的命令生成客户端使用的stub类:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->%AXIS2_HOME%\bin\wsdl2java -uri http://localhost:8080/axis2/services/loginService?wsdl -p client -s -o stub

Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 <style. /* Style. Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style.

stub\src\client目录中生成了一个LoginServiceStub.java类,在该类中找到以下的构造句方法:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->public LoginServiceStub(org.apache.axis2.context.ConfigurationContext configurationContext,
        java.lang.String targetEndpoint, 
boolean useSeparateListener)
        
throws org.apache.axis2.AxisFault 
{
     
    _serviceClient.getOptions().setSoapVersionURI(
                                 org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
}

Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 <style. /* Style. Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style. 在该方法中最后添加以下的代码:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->//  第3步:打开客户端的Session管理功能
_serviceClient.getOptions().setManageSession(true);

Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 <style. /* Style. Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style.

下面的客户端代码使用LoginServiceStub对象访问了刚才创建的WebService

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->LoginServiceStub stub = new LoginServiceStub();
LoginServiceStub.Login login 
= new LoginServiceStub.Login();
login.setUsername(
"bill");
login.setPassword(
"1234");
if(stub.login(login).local_return)
{
    System.out.println(stub.getLoginMsg().local_return);
}

Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 <style. /* Style. Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} </style.

运行上面的代码后,会输出“成功登陆”信息。

相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息