Axis2发布webservice(4)—利用XML文件同时发布多个webservice和跨多个WebService管理Session
我们需要ServiceGroupContext保存跨越多个webservice的session信息;同时需要设置services.xml文件的中service的scope属性为application
一、编写两个webservice:
LoginServiceApplication.java代码如下:
package com.hoo.service; import org.apache.axis2.context.MessageContext; import org.apache.axis2.context.ServiceGroupContext; public class LoginServiceApplication { //登陆方法 public boolean login(String userName,String password){ MessageContext context = MessageContext.getCurrentMessageContext(); //跨越多个WebService管理session第一步是实例化一个ServiceGroupContext对象,而不是ServiceContext ServiceGroupContext ctx = context.getServiceGroupContext(); if("admin".equals(userName)&&"123456".equals(password)){ ctx.setProperty("username", userName); ctx.setProperty("password", password); ctx.setProperty("msg","登陆成功"); return true; }else{ ctx.setProperty("msg", "登陆失败"); return false; } } public String getLoginMessage(){ MessageContext context = MessageContext.getCurrentMessageContext(); ServiceGroupContext ctx = context.getServiceGroupContext(); String msg = ctx.getProperty("username")+","+ctx.getProperty("msg"); return msg; } }
SearchSessionService.java代码如下:
package com.hoo.service; import org.apache.axis2.context.MessageContext; import org.apache.axis2.context.ServiceGroupContext; public class SearchSessionService { public String findSessionMessage(String key){ MessageContext context = MessageContext.getCurrentMessageContext(); ServiceGroupContext ctx = context.getServiceGroupContext(); if(ctx.getProperty(key)!=null){ return "找到session:" + key + "," +ctx.getProperty(key); }else{ return "没有找到<" + key + ">的数据"; } } }
上面两个webservice都用到了ServiceGroupContext的对象。
二、 编写services.xml,同时发布2个webservice,并且将scope设为application,services.xml的内容如下:
<serviceGroup> <service name="LoginSessionService" scope="application"> <description> Web Service Session例子 </description> <parameter name="ServiceClass"> com.hoo.service.LoginServiceApplication </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.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>
在eclipse里面,file->new->other->axis2 service archiver->next
选择webservice的class文件存放路径,一般选到bin文件夹就行了
然后,next->next->next,出现选择XML的界面:
取消勾选“generate the service XML automatically”,在上面的文件浏览框中选中刚才保存的services.xml文件,next
选择输出路径和文件名(这里的文件名和webservice的名字没有关系,webservice的名字在XML文件中定义),next->finish,出现创建成功的提示即可
然后利用前面教程中的方法将此AAR文件发不成webservice即可
三、编写客户端代码:
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 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(); //打开对session的支持 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,登陆成功
找到session:username,admin
找到session:msg,登陆成功
找到session:password,123456
如果将services.xml文件<service name="SearchSessionService" scope="application">的内容改成scope=transportsession,就会出现找不到session中的内容错误。
Axis2发布webservice(4)—利用XML文件同时发布多个webservice和跨多个WebService管理Session,古老的榕树,5-wow.com
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。