services.xml文件中的配置代码如下:
<!-- 第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 >
第 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.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对象,代码如下:
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对象,代码如下:
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即可。