跨多个WebService管理Session

简介: 六、 跨多个WebService管理Session 当多个WebService的时候,我们要管理它的Session。这个时候我们得依靠ServiceGroupContext保存session信息;然后在发布WebService的时候,services.
六、 跨多个WebService管理Session

当多个WebService的时候,我们要管理它的Session。这个时候我们得依靠ServiceGroupContext保存session信息;然后在发布WebService的时候,services.xml文件的的service表情的scope就不再说request或是transportsession了,而是application;最后同样要开启对session的管理,即options.setManageSession(true);

1、 首先多个WebService的session管理的代码如下:

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif package com.hoo.service; import org.apache.axis2.context.MessageContext; import org.apache.axis2.context.ServiceGroupContext; /** * function:管理多个会话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 */ public class LoginSessionService { public boolean login(String userName, String password) { MessageContext context = MessageContext.getCurrentMessageContext(); ServiceGroupContext ctx = context.getServiceGroupContext(); if ("admin".equals(userName) && "123456".equals(password)) { ctx.setProperty("userName", userName); ctx.setProperty("password", password); ctx.setProperty("msg", "登陆成功"); return true; } ctx.setProperty("msg", "登陆失败"); return false; } public String getLoginMessage() { MessageContext context = MessageContext.getCurrentMessageContext(); ServiceGroupContext ctx = context.getServiceGroupContext(); return ctx.getProperty("userName") + "#" + ctx.getProperty("msg"); } }

和上面的Session一样的操作,只不过是用ServiceGroupContext上下文来存取session信息

另外还需要用一个Service来查询session的信息,SearchService的代码如下:

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif 代码 package com.hoo.service; import org.apache.axis2.context.MessageContext; import org.apache.axis2.context.ServiceGroupContext; /** * function:查找多服务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 */ public class SearchSessionServcie { public String findSessionMessage(String key) { MessageContext mc = MessageContext.getCurrentMessageContext(); ServiceGroupContext ctx = mc.getServiceGroupContext(); if (ctx.getProperty(key) != null) { return "找到的数据" + key + ", " + ctx.getProperty(key) + ">"; } else { return "没有找到" + key + ">的数据"; } } }

2、 编写services.xml来发布这2个服务,还以前不一样的。这一次是用一个services.xml文件配置2个service,同时发布2个服务。Xml代码如下:

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif 代码 serviceGroup> service name="LoginSessionService" scope="application"> description> Web Service Session例子 description> parameter name="ServiceClass"> com.hoo.service.LoginSessionService parameter> messageReceivers> messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> messageReceivers> service> service name="SearchSessionService" scope="application"> description> Web Service Search Session例子 description> parameter name="ServiceClass"> com.hoo.service.SearchSessionServcie parameter> messageReceivers> messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> messageReceivers> service> serviceGroup>

3、 发布完成后,可以通过http://localhost:8080/axis2/services/listServices查看发布的WebService服务,编写客户端的测试代码,code如下:

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif 代码 package com.hoo.service; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; /** * function:多会话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 */ public class LoginSessionServiceClient { public static void main(String[] args) throws AxisFault { String target = "http://localhost:8080/axis2/services/LoginSessionService"; RPCServiceClient client = new RPCServiceClient(); Options options = client.getOptions(); options.setManageSession(true); EndpointReference epr = new EndpointReference(target); options.setTo(epr); QName qname = new QName("http://service.hoo.com", "login"); //指定调用的方法和传递参数数据,及设置返回值的类型 Object[] result = client.invokeBlocking(qname, new Object[] { "admin", "123456" }, new Class[] { boolean.class }); System.out.println(result[0]); qname = new QName("http://service.hoo.com", "getLoginMessage"); result = client.invokeBlocking(qname, new Object[] { null }, new Class[] { String.class }); System.out.println(result[0]); target = "http://localhost:8080/axis2/services/SearchSessionService"; epr = new EndpointReference(target); options.setTo(epr); qname = new QName("http://service.hoo.com", "findSessionMessage"); result = client.invokeBlocking(qname, new Object[] { "userName" }, new Class[] { String.class }); System.out.println(result[0]); qname = new QName("http://service.hoo.com", "findSessionMessage"); result = client.invokeBlocking(qname, new Object[] { "msg" }, new Class[] { String.class }); System.out.println(result[0]); qname = new QName("http://service.hoo.com", "findSessionMessage"); result = client.invokeBlocking(qname, new Object[] { "password" }, new Class[] { String.class }); System.out.println(result[0]); } }

运行后结果如下:

true

admin#登陆成功

找到的数据

找到的数据

找到的数据

4、 如果将services.xml文件"SearchSessionService" scope="application">的内容改成scope=transportsession,看看什么情况。是不是找不到session中的内容。

目录
相关文章
|
.NET 开发框架
在asp.net webservice中如何使用session
原文:在asp.net webservice中如何使用session 原文:刘武|在asp.net webservice中如何使用session   在使用asp.net编写webservice时,默认情况下是不支持session的,但我们可以把WebMethod的EnableSession...
884 0
|
数据格式 XML .NET
Webservice中使用Session、Application
原文:Webservice中使用Session、Application 在Asp.Net 2.0里,已经能够在WebMethod里使用 Session 、 Application 这种服务器变量了。一、Session     [WebMethod(EnableSession = true)] ...
911 0
|
.NET
WCF常见问题(1) -- WebService/WCF Session Cookie
原文: WCF常见问题(1) -- WebService/WCF Session Cookie 在.net 3.0推出WCF之前使用的WebService,有的应用有使用Session保持一些信息,在不同的WebMethod中共享存储信息。
1046 0
|
.NET Windows 开发框架
ASP.NET Web Service中使用Session 及 Session丢失解决方法 续
原文:ASP.NET Web Service中使用Session 及 Session丢失解决方法 续 1、关于Session丢失问题的说明汇总,参考这里 2、在Web Servcie中使用Session,需要对Web Method做如下处理 [WebMethod(EnableSession = true)]public void usingSession(){    Session["Name"] = "Name";}   如果不加EnableSession = true,在Web Service中是不能使用Session的。
751 0
|
容器
Webservice服务中如何保持Session
问题一:webservice服务中如果保持Session 调用Session 对于Web Service,每个方法的调用都会启动一个Session,可以用下面的方法来使多个调用在同一个Session里 CWSSyscfg cwsCfg = new CWSSyscfg(); cwsCfg.
550 0
|
Java Apache
WebService会话Session的管理
1、新建Session的WebService测试代码,代码很简单。就是记录用户登录的状态信息,放在MessageContext的ServiceContext中。代码如下: 代码package com.
787 0
下一篇
无影云桌面