axis2开发webservice之跨服务会话(Session)管理(二)

2014-11-24 10:18:51 · 作者: · 浏览: 1
(lss.getLoginMsg(glm).get_return());

SearchSessionStub sss = new SearchSessionStub();
SearchSessionStub.FindByName fbn = new SearchSessionStub.FindByName();
fbn.setName("bill");
System.out.println(sss.findByName(fbn).local_return);
}
}
}

在执行上面的代码后,将输出如下的信息:

如果将scope属性值改成transportsession,我们看看会输出什么!

第一:改变loginService的

正常输出,也就是说loginService这个服务只能在同一个服务中起作用,在这里我们只是调用了登陆服务,所以没影响。

第二:改变searchService的

居然还是正常输出,不对啊,如下代码需要跨服务的

[java]
if (sgc.getProperty("login") != null)
return "找到的数据<" + name + ">";
else
return "用户未登录";
这是怎么回事呢,难道问题在这里?

[java]
// 第1步:获得key-value对中的value
MessageContext mc = MessageContext.getCurrentMessageContext();
ServiceGroupContext sgc = mc.getServiceGroupContext(); //ServiceGroupContext而不是ServiceContext?
[java] view plaincopy
//ServiceGroupContext与scope冲突?
好吧,既然冲突,那咱们就一个一个的试,结果更加出乎意料,把ServiceGroupContext改成ServiceContext,居然还是正常输出,什么情况?

这让我开始怀疑这个程序是不是跨session的,不会是披着跨session的外衣吧,继续研究!

先看一下scope中transportsession的问题,如下是官方文档

Transport Session Scope
In the case of a Transport session, Axis2 uses transport-related session management techniques to manage session. As an example, in the case of HTTP, it uses HTTP cookies to manage the session. The lifetime of the session is controlled by the transport, not by Axis2; Axis2 stores the service context and serviceGroupContext in the transport session object so that the service can access those contexts as long as the session lives.
One of the key advantages of the Transport session over other sessions is that you can talk to multiple service groups within one transport session.In a SOAP session, you don't have a way to communicate between two service groups, but with the transport session you have that capability. In this case, the number of service instances created depends on the number of transport sessions created.
Deploying a service in a transport session requires you to change services.xml as follows:


If you are using Axis2 nightly builds or planning to use them the next version, deploying a service in a transport session requires additional changes to axis2.xml. That is mainly to improve the memory usage; otherwise, whether you deploy the service in a transport session or not Axis2 tries to create a session object at the transport level; with these changes, it will not create unnecessary objects. To manage the transport-level session, you need to set the manageTransportSession parameter value to true in axis2.xml:
看到这段话的时候我才感觉到transportsession不仅是单个服务那么简单的。

至于ServiceGroupContext和ServiceContext,我查了一下API文档

可以看出来他们都是继承自同一个类,而这个抽象类的setProperty()方法如下,

从这里我们可以看到这个方法是用来存储值的,至于存储的值的访问权限,我们只有搞清楚ServiceGroupContext和ServiceContext才能知道。

下面是ServiceContext的介绍

在这里我们看到它的生命周期是不明确的,不推荐使用,而且其构造方法是可外部化的,关于这个问题,可以查看这篇文章点击打开链接

下面是ServiceGroupContext的介绍

其实到这里我还是没多大明白ServiceGroupContext和ServiceContext的主要区别,于是我又看了MessageContext,结果又多了一个疑惑


为什么MessageContext只持有ServiceGroupContext,而没有提及ServiceContext呢?

真是一波未平一波又起,看来还需要大家的智慧了,如果以上的这几个问题有懂的高手,还请留言解决一下。