`
seara
  • 浏览: 624665 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

WebService大讲堂之Axis2(5):会话(Session)管理

阅读更多

本文为原创,如需转载,请注明作者和出处,谢谢!


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

读者可以想象,这对于一个完整的应用程序,无法保留状态,就意味着只依靠WebService很难完成全部的工作。例如,一个完整的应用系统都需要进行登录,这在Web应用中使用Session来保存用户登录状态,而如果用WebService的方法来进行登录处理,无法保存登录状态是非常令人尴尬的。当然,这也可以通过其他的方法来解决,如在服务端使用static变量来保存用户状态,并发送一个id到客户端,通过在服务端和客户端传递这个id来取得相应的用户状态。这非常类似于Web应用中通过SessionCookie来管理用户状态。但这就需要由开发人员做很多工作,不过幸好Axis2为我们提供了WebService状态管理的功能。

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

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

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

综上所述,实现同一个WebServiceSession管理需要如下三步:

1.使用MessageContextServiceContext获得与设置key-value对。

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

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

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

先建立一个WebService类,代码如下:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->packageservice;
importorg.apache.axis2.context.ServiceContext;
importorg.apache.axis2.context.MessageContext;
publicclassLoginService
{
publicbooleanlogin(Stringusername,Stringpassword)
{
if("bill".equals(username)&&"1234".equals(password))
{
//第1步:设置key-value对
MessageContextmc=MessageContext.getCurrentMessageContext();
ServiceContextsc
=mc.getServiceContext();
sc.setProperty(
"login","成功登录");
returntrue;
}
else
{
returnfalse;
}
}
publicStringgetLoginMsg()
{
//第1步:获得key-value对中的value
MessageContextmc=MessageContext.getCurrentMessageContext();
ServiceContextsc
=mc.getServiceContext();
return(String)sc.getProperty("login");
}
}
<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--> <!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --> <!--[endif]-->

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

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

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><!--第2步:添加scope属性-->
<servicename="loginService"scope="transportsession">
<description>
登录服务
</description>
<parametername="ServiceClass">
service.LoginService
</parameter>
<messageReceivers>
<messageReceivermep="http://www.w3.org/2004/08/wsdl/in-out"
class
="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
</service>

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--> <!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --> <!--[endif]-->

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

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->%AXIS2_HOME%\bin\wsdl2java-urihttp://localhost:8080/axis2/services/loginService?wsdl-pclient-s-ostub

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--> <!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --> <!--[endif]-->

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

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->publicLoginServiceStub(org.apache.axis2.context.ConfigurationContextconfigurationContext,
java.lang.StringtargetEndpoint,
booleanuseSeparateListener)
throwsorg.apache.axis2.AxisFault
{

_serviceClient.getOptions().setSoapVersionURI(org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
}

<!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --> <!--[endif]-->在该方法中最后添加如下的代码:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->//第3步:打开客户端的Session管理功能
_serviceClient.getOptions().setManageSession(true);
<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--> <!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --> <!--[endif]-->

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

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->LoginServiceStubstub=newLoginServiceStub();
LoginServiceStub.Loginlogin
=newLoginServiceStub.Login();
login.setUsername(
"bill");
login.setPassword(
"1234");
if(stub.login(login).local_return)
{
System.out.println(stub.getLoginMsg().local_return);
}

<!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--> <!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman";} --> <!--[endif]-->


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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics