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

跨多个WebService管理Session

<DIV>
<A class=postTitle2 id=ctl02_TitleUrl href="http://www.cnblogs.com/hoojo/archive/2011/03/16/1985704.html"><FONT color=#1a8bc8>六、 跨多个WebService管理Session</FONT></A>
<DIV id=cnblogs_post_body>
<P>当多个WebService的时候,我们要管理它的Session。这个时候我们得依靠ServiceGroupContext保存session信息;然后在发布WebService的时候,services.xml文件的的service表情的scope就不再说request或是transportsession了,而是application;最后同样要开启对session的管理,即options.setManageSession(<B>true</B>);</P>
<P>1、 首先多个WebService的session管理的代码如下:</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_963053 style="DISPLAY: none" .click="this.style.display='none'; document.getElementById('Code_Closed_Text_963053').style.display='none'; document.getElementById('Code_Open_Image_963053').style.display='inline'; document.getElementById('Code_Open_Text_963053').style.display='inline';" height=16 src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 align=top><IMG id=Code_Open_Image_963053 style="DISPLAY: inline" .click="this.style.display='none'; document.getElementById('Code_Open_Text_963053').style.display='none'; getElementById('Code_Closed_Image_963053').style.display='inline'; getElementById('Code_Closed_Text_963053').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_963053 style="DISPLAY: none"><FONT face="Courier New">代码</FONT></SPAN><SPAN id=Code_Open_Text_963053 style="DISPLAY: inline"><FONT face="Courier New"><SPAN style="COLOR: #0000ff">package</SPAN> com.hoo.service;

<SPAN style="COLOR: #0000ff">import</SPAN> org.apache.axis2.context.MessageContext;
<SPAN style="COLOR: #0000ff">import</SPAN> org.apache.axis2.context.ServiceGroupContext;

</FONT><FONT face="Courier New"><SPAN style="COLOR: #008000">/**
* &lt;b&gt;function:&lt;/b&gt;管理多个会话Session信息
* @author hoojo
* @createDate 2011-3-9 下午05:11:07
* @file LoginSessionService.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> LoginSessionService {
        <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN style="COLOR: #0000ff">boolean</SPAN> login(String userName, String password) {
                MessageContext context = MessageContext.getCurrentMessageContext();
                ServiceGroupContext ctx = context.getServiceGroupContext();
                <SPAN style="COLOR: #0000ff">if</SPAN> ("<SPAN style="COLOR: #8b0000">admin</SPAN>".equals(userName) &amp;&amp; "<SPAN style="COLOR: #8b0000">123456</SPAN>".equals(password)) {
                        ctx.setProperty("<SPAN style="COLOR: #8b0000">userName</SPAN>", userName);
                        ctx.setProperty("<SPAN style="COLOR: #8b0000">password</SPAN>", password);
                        ctx.setProperty("<SPAN style="COLOR: #8b0000">msg</SPAN>", "<SPAN style="COLOR: #8b0000">登陆成功</SPAN>");
                        <SPAN style="COLOR: #0000ff">return</SPAN> <SPAN style="COLOR: #0000ff">true</SPAN>;
                }
                ctx.setProperty("<SPAN style="COLOR: #8b0000">msg</SPAN>", "<SPAN style="COLOR: #8b0000">登陆失败</SPAN>");
                <SPAN style="COLOR: #0000ff">return</SPAN> <SPAN style="COLOR: #0000ff">false</SPAN>;
        }
       
        <SPAN style="COLOR: #0000ff">public</SPAN> String getLoginMessage() {
                MessageContext context = MessageContext.getCurrentMessageContext();
                ServiceGroupContext ctx = context.getServiceGroupContext();
                <SPAN style="COLOR: #0000ff">return</SPAN> ctx.getProperty("<SPAN style="COLOR: #8b0000">userName</SPAN>") + "<SPAN style="COLOR: #8b0000">#</SPAN>" + ctx.getProperty("<SPAN style="COLOR: #8b0000">msg</SPAN>");
        }
}</FONT></DIV><BR></SPAN>
<P><FONT face="Courier New"></FONT></P>
<P>和上面的Session一样的操作,只不过是用ServiceGroupContext上下文来存取session信息</P>
<P>另外还需要用一个Service来查询session的信息,SearchService的代码如下:</P>
<P></P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_275271 .click="this.style.display='none'; document.getElementById('Code_Closed_Text_275271').style.display='none'; document.getElementById('Code_Open_Image_275271').style.display='inline'; document.getElementById('Code_Open_Text_275271').style.display='inline';" height=16 src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 align=top><IMG id=Code_Open_Image_275271 style="DISPLAY: none" .click="this.style.display='none'; document.getElementById('Code_Open_Text_275271').style.display='none'; getElementById('Code_Closed_Image_275271').style.display='inline'; getElementById('Code_Closed_Text_275271').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_275271><FONT face="Courier New">代码</FONT></SPAN><SPAN id=Code_Open_Text_275271 style="DISPLAY: none"><FONT face="Courier New"><SPAN style="COLOR: #0000ff">package</SPAN> com.hoo.service;

<SPAN style="COLOR: #0000ff">import</SPAN> org.apache.axis2.context.MessageContext;
<SPAN style="COLOR: #0000ff">import</SPAN> org.apache.axis2.context.ServiceGroupContext;

</FONT><FONT face="Courier New"><SPAN style="COLOR: #008000">/**
* &lt;b&gt;function:&lt;/b&gt;查找多服务Session会话中的消息
* @author hoojo
* @createDate 2011-3-9 下午05:22:39
* @file SearchSessionServcie.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> SearchSessionServcie {
        <SPAN style="COLOR: #0000ff">public</SPAN> String findSessionMessage(String key) {
      MessageContext mc = MessageContext.getCurrentMessageContext();
      ServiceGroupContext ctx =mc.getServiceGroupContext();               
      <SPAN style="COLOR: #0000ff">if</SPAN> (ctx.getProperty(key) != <SPAN style="COLOR: #0000ff">null</SPAN>) {
            <SPAN style="COLOR: #0000ff">return</SPAN> "<SPAN style="COLOR: #8b0000">找到的数据&lt;</SPAN>" + key + "<SPAN style="COLOR: #8b0000">, </SPAN>" + ctx.getProperty(key) + "<SPAN style="COLOR: #8b0000">&gt;</SPAN>";
      } <SPAN style="COLOR: #0000ff">else</SPAN> {
            <SPAN style="COLOR: #0000ff">return</SPAN> "<SPAN style="COLOR: #8b0000">没有找到&lt;</SPAN>" + key + "<SPAN style="COLOR: #8b0000">&gt;的数据</SPAN>";
      }
        }
}</FONT></DIV><BR></SPAN>
<P>2、 编写services.xml来发布这2个服务,还以前不一样的。这一次是用一个services.xml文件配置2个service,同时发布2个服务。Xml代码如下:</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_816457 .click="this.style.display='none'; document.getElementById('Code_Closed_Text_816457').style.display='none'; document.getElementById('Code_Open_Image_816457').style.display='inline'; document.getElementById('Code_Open_Text_816457').style.display='inline';" height=16 src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 align=top><IMG id=Code_Open_Image_816457 style="DISPLAY: none" .click="this.style.display='none'; document.getElementById('Code_Open_Text_816457').style.display='none'; getElementById('Code_Closed_Image_816457').style.display='inline'; getElementById('Code_Closed_Text_816457').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_816457><FONT face="Courier New">代码</FONT></SPAN><SPAN id=Code_Open_Text_816457 style="DISPLAY: none"><FONT face="Courier New"><SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">serviceGroup</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN>
        <SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">service</SPAN> <SPAN style="COLOR: #ff0000">name</SPAN>=<SPAN style="COLOR: #0000ff">"LoginSessionService"</SPAN> <SPAN style="COLOR: #ff0000">scope</SPAN>=<SPAN style="COLOR: #0000ff">"application"</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>
                        Web Service Session例子
                <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.LoginSessionService
                <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>

        <SPAN style="COLOR: #0000ff">&lt;</SPAN><SPAN style="COLOR: #800000">service</SPAN> <SPAN style="COLOR: #ff0000">name</SPAN>=<SPAN style="COLOR: #0000ff">"SearchSessionService"</SPAN> <SPAN style="COLOR: #ff0000">scope</SPAN>=<SPAN style="COLOR: #0000ff">"application"</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>
                        Web Service Search Session例子
                <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.SearchSessionServcie
                <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>
<SPAN style="COLOR: #0000ff">&lt;/</SPAN><SPAN style="COLOR: #800000">serviceGroup</SPAN><SPAN style="COLOR: #0000ff">&gt;</SPAN></FONT></DIV><BR></SPAN><SPAN id=Code_Open_Text_801845 style="DISPLAY: none"></SPAN>
<P><FONT face="Courier New"></FONT></P>
<P>3、 发布完成后,可以通过<a href="http://localhost:8080/axis2/services/listServices" target="_blank"><FONT color=#1a8bc8>http://localhost:8080/axis2/services/listServices</FONT></A>查看发布的WebService服务,编写客户端的测试代码,code如下:</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_168966 .click="this.style.display='none'; document.getElementById('Code_Closed_Text_168966').style.display='none'; document.getElementById('Code_Open_Image_168966').style.display='inline'; document.getElementById('Code_Open_Text_168966').style.display='inline';" height=16 src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" width=11 align=top><IMG id=Code_Open_Image_168966 style="DISPLAY: none" .click="this.style.display='none'; document.getElementById('Code_Open_Text_168966').style.display='none'; getElementById('Code_Closed_Image_168966').style.display='inline'; getElementById('Code_Closed_Text_168966').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_168966><FONT face="Courier New">代码</FONT></SPAN><SPAN id=Code_Open_Text_168966 style="DISPLAY: none"><FONT face="Courier New"><SPAN style="COLOR: #0000ff">package</SPAN> com.hoo.service;

<SPAN style="COLOR: #0000ff">import</SPAN> javax.xml.namespace.QName;
<SPAN style="COLOR: #0000ff">import</SPAN> org.apache.axis2.AxisFault;
<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.rpc.client.RPCServiceClient;

</FONT><FONT face="Courier New"><SPAN style="COLOR: #008000">/**
* &lt;b&gt;function:&lt;/b&gt;多会话Session管理,WebService客户端请求代码
* @author hoojo
* @createDate 2011-3-9 下午05:17:15
* @file LoginSessionServiceClient.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> LoginSessionServiceClient {

        <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> AxisFault {
                String target = "<SPAN style="COLOR: #8b0000">http://localhost:8080/axis2/services/LoginSessionService</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">login</SPAN>");
                <SPAN style="COLOR: #008000">//指定调用的方法和传递参数数据,及设置返回值的类型</SPAN>
                Object[] result = client.invokeBlocking(qname, <SPAN style="COLOR: #0000ff">new</SPAN> Object[] { "<SPAN style="COLOR: #8b0000">admin</SPAN>", "<SPAN style="COLOR: #8b0000">123456</SPAN>" }, <SPAN style="COLOR: #0000ff">new</SPAN> Class[] { <SPAN style="COLOR: #0000ff">boolean</SPAN>.<SPAN style="COLOR: #0000ff">class</SPAN> });
                System.out.println(result);
               
                qname = <SPAN style="COLOR: #0000ff">new</SPAN> QName("<SPAN style="COLOR: #8b0000">http://service.hoo.com</SPAN>", "<SPAN style="COLOR: #8b0000">getLoginMessage</SPAN>");
                result = client.invokeBlocking(qname, <SPAN style="COLOR: #0000ff">new</SPAN> Object[] { <SPAN style="COLOR: #0000ff">null</SPAN> }, <SPAN style="COLOR: #0000ff">new</SPAN> Class[] { String.<SPAN style="COLOR: #0000ff">class</SPAN> });
                System.out.println(result);
               
                target = "<SPAN style="COLOR: #8b0000">http://localhost:8080/axis2/services/SearchSessionService</SPAN>";
                epr = <SPAN style="COLOR: #0000ff">new</SPAN> EndpointReference(target);
                options.setTo(epr);
               
                qname = <SPAN style="COLOR: #0000ff">new</SPAN> QName("<SPAN style="COLOR: #8b0000">http://service.hoo.com</SPAN>", "<SPAN style="COLOR: #8b0000">findSessionMessage</SPAN>");
                result = client.invokeBlocking(qname, <SPAN style="COLOR: #0000ff">new</SPAN> Object[] { "<SPAN style="COLOR: #8b0000">userName</SPAN>" }, <SPAN style="COLOR: #0000ff">new</SPAN> Class[] { String.<SPAN style="COLOR: #0000ff">class</SPAN> });
                System.out.println(result);
                qname = <SPAN style="COLOR: #0000ff">new</SPAN> QName("<SPAN style="COLOR: #8b0000">http://service.hoo.com</SPAN>", "<SPAN style="COLOR: #8b0000">findSessionMessage</SPAN>");
                result = client.invokeBlocking(qname, <SPAN style="COLOR: #0000ff">new</SPAN> Object[] { "<SPAN style="COLOR: #8b0000">msg</SPAN>" }, <SPAN style="COLOR: #0000ff">new</SPAN> Class[] { String.<SPAN style="COLOR: #0000ff">class</SPAN> });
                System.out.println(result);
               
                qname = <SPAN style="COLOR: #0000ff">new</SPAN> QName("<SPAN style="COLOR: #8b0000">http://service.hoo.com</SPAN>", "<SPAN style="COLOR: #8b0000">findSessionMessage</SPAN>");
                result = client.invokeBlocking(qname, <SPAN style="COLOR: #0000ff">new</SPAN> Object[] { "<SPAN style="COLOR: #8b0000">password</SPAN>" }, <SPAN style="COLOR: #0000ff">new</SPAN> Class[] { String.<SPAN style="COLOR: #0000ff">class</SPAN> });
                System.out.println(result);
        }
}</FONT></DIV><BR></SPAN><SPAN id=Code_Open_Text_962799 style="DISPLAY: none"></SPAN>
<P><FONT face="Courier New"></FONT></P>
<P>运行后结果如下:</P>
<P>true</P>
<P>admin#登陆成功</P>
<P>找到的数据&lt;userName, admin&gt;</P>
<P>找到的数据&lt;msg, 登陆成功&gt;</P>
<P>找到的数据&lt;password, 123456&gt;</P>
<P>4、 如果将services.xml文件&lt;service name=<I>"SearchSessionService"</I> scope=<I>"application"</I>&gt;的内容改成scope=transportsession,看看什么情况。是不是找不到session中的内容。</P></DIV></DIV>
页: [1]
查看完整版本: 跨多个WebService管理Session