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.

運行上面的代碼後,會輸出「成功登陸」信息。

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息