Axis跨多个Webservice进行的Session管理

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


1.  服务端代码,有俩个,需要编写俩个service

     

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信息的
 * @author linwei
 *
 */
public class SearchSessionService {

	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 + ">的数据";
		}
	}

}

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

2. services.xml配置文件的内容如下,编写完毕后,在把对应的classes以及其路径放置对应文件夹,打包

<serviceGroup>
	<service name="LoginSessionService" scope="application">
		<description>
			Web Service Session例子
		</description>
		<parameter name="ServiceClass">
			server.perfect.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">
			server.perfect.SearchSessionService  
		</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. 客户端代码:

   

package client;

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;

public class LoginSessionServiceTest {

	
	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://perfect.server", "login");
		//指定调用的方法和传递参数数据,及设置返回值的类型
		Object[] result = client.invokeBlocking(qname, new Object[] { "admin", "123456" }, new Class[] { boolean.class });
		System.out.println(result[0]);
		
		qname = new QName("http://perfect.server", "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://perfect.server", "findSessionMessage");
		result = client.invokeBlocking(qname, new Object[] { "userName" }, new Class[] { String.class });
		System.out.println(result[0]);
		
		qname = new QName("http://perfect.server", "findSessionMessage");
		result = client.invokeBlocking(qname, new Object[] { "msg" }, new Class[] { String.class });
		System.out.println(result[0]);
		
		qname = new QName("http://perfect.server", "findSessionMessage");
		result = client.invokeBlocking(qname, new Object[] { "password" }, new Class[] { String.class });
		System.out.println(result[0]);
	}
	
}

运行后结果如下:
true
admin#登陆成功
找到的数据<userName, admin>
找到的数据<msg, 登陆成功>
找到的数据<password, 123456>

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

     







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值