WebService大讲堂之Axis2(6):跨服务会话(Session)管理

本文介绍如何在Axis2中实现跨服务的会话管理,包括设置与获取会话状态的具体步骤,以及通过示例代码展示如何在多个WebService间共享会话。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在 《 WebService大讲堂之Axis2(5):会话(Session)管理 》 一文中介绍了如何使用 Axis2来管理同一个服务的会话,但对于一个复杂的系统,不可能只有一个 WebService服务,例如,至少会有一个管理用户的 WebService(用户登录和注册)以及处理业务的 WebService。象这种情况,就必须在多个 WebService服务之间共享会话状态,也称为跨服务会话 (Session)管理。实现跨服务会话管理与实现同一个服务的会话管理的步骤类似,但仍然有一些差别,实现跨服务会话管理的步骤如下:

实现跨服务的 Session管理需要如下三步:

1. 使用 MessageContext和 ServiceGroupContext 获得与设置 key-value对。

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

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

从上面的步骤可以看出,实现跨服务会话管理与实现同一个服务的会话管理在前两步上存在着差异,而第 3步是完全一样的。下面是一个跨服务的会话管理的实例。在这个例子中有两个 WebService类: LoginService和 SearchService,代码如下:
LoginService.java


package service;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.ServiceGroupContext;
public class LoginService
{
public boolean login(String username, String password)
{
if ( " bill " .equals(username) && " 1234 " .equals(password))
{
// 第1步:设置key-value对
MessageContext mc = MessageContext.getCurrentMessageContext();
ServiceGroupContext sgc = mc.getServiceGroupContext();
sgc.setProperty( " login " , " 成功登录 " );
return true ;
}
else
{
return false ;
}
}
public String getLoginMsg()
{
// 第1步:获得key-value对中的value
MessageContext mc = MessageContext.getCurrentMessageContext();
ServiceGroupContext sgc = mc.getServiceGroupContext();
return (String)sgc.getProperty( " login " );
}
}
SearchService.java

package service;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.ServiceGroupContext;
public class SearchService
{
public String findByName(String name)
{
// 第1步:获得key-value对中的value
MessageContext mc = MessageContext.getCurrentMessageContext();
ServiceGroupContext sgc = mc.getServiceGroupContext();
if (sgc.getProperty( " login " ) != null )
return " 找到的数据< " + name + " > " ;
else
return " 用户未登录 " ;
}
}

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->
services.xml文件中的配置代码如下:


< serviceGroup >
<!-- 第2步:添加scope属性,并设置属性值为application -->
< service name ="loginService" scope ="application" >
< description >
登录服务
</ description >
< parameter name ="ServiceClass" >
service.LoginService
</ parameter >
< messageReceivers >
< messageReceiver mep ="http://www.w3.org/2004/08/wsdl/in-out"
class ="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</ messageReceivers >
</ service >
<!-- 第2步:添加scope属性,并设置属性值为application -->
< service name ="searchService" scope ="application" >
< description >
搜索服务
</ description >
< parameter name ="ServiceClass" >
service.SearchService
</ parameter >
< messageReceivers >
< messageReceiver mep ="http://www.w3.org/2004/08/wsdl/in-out"
class ="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</ messageReceivers >
</ service >
</ serviceGroup >
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->
第 3步与 《WebService大讲堂之Axis2(5):会话(Session)管理 》 一文中介绍的方法类似。
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

下面是使用两个 stub类的对象实例访问上面实现的两个 WebService的客户端代码:


LoginServiceStub stub = new LoginServiceStub();
LoginServiceStub.Login login = new LoginServiceStub.Login();
login.setUsername( " bill " );
login.setPassword( " 1234 " );
if (stub.login(login).local_return)
{
System.out.println(stub.getLoginMsg().local_return);
SearchServiceStub searchStub = new SearchServiceStub();
SearchServiceStub.FindByName fbn = new SearchServiceStub.FindByName();
fbn.setName( " abc " );
System.out.println(searchStub.findByName(fbn).local_return);
}

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

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

成功登录

找到的数据 < abc >

读者可以将scope属性值改成transportsession,看看会输出什么!
实际上,Axis2的会话管理也是通过Cookie实现的,与Web应用中的Session管理类似。如果读者使用C#访问支持会话(在同一个服务中的会话管理)的WebService,需要指定一个CookieContainer对象,代码如下:

service.loginService ls = new service.loginService();
System.Net.CookieContainer cc = new System.Net.CookieContainer();
ls.CookieContainer = cc;
bool r, rs;
ls.login( " bill " , " 1234 " , out @r, out rs);
if (r)
{
MessageBox.Show(ls.getLoginMsg().@return);
}

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> 如果是访问跨服务的支持会话的 WebService,则不需要指定 CookieContainer对象,代码如下:


service.loginService ls = new service.loginService();
bool r, rs;
ls.login( " bill " , " 1234 " , out @r, out rs);
if (r)
{
service1.searchService ss = new service1.searchService();
MessageBox.Show(ss.findByName( " abc " ));
}

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

如果读者使用 delphi(本文使用的是 delphi2009,其他的 delphi版本请读者自行测试)调用支持会话的 WebService时有一些差别。经笔者测试,使用 delphi调用 WebService,将 scope属性值设为 transportsession和 application都可以实现跨服务的会话管理,这一点和 Java与 C#不同, Java和 C#必须将 scope属性值设为 application才支持跨服务会话管理。在 delphi中不需要象 C#指定一个 CookieContainer或其他类似的对象,而只需要象访问普通的 WebService一样访问支持会话的 WebService即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值