We need servicegroupcontext to save session information that spans multiple webservice, and the scope property of the service in which the Services.xml file needs to be set is application
First, write two webservice:
The Loginserviceapplication.java code is as follows:
PackageCom.hoo.service;ImportOrg.apache.axis2.context.MessageContext;ImportOrg.apache.axis2.context.ServiceGroupContext; Public classloginserviceapplication {//Login Method Public BooleanLogin (String username,string password) {messagecontext context=Messagecontext.getcurrentmessagecontext (); //managing a session across multiple WebService the first step is to instantiate a Servicegroupcontext object instead of ServicecontextServicegroupcontext CTX =Context.getservicegroupcontext (); if("admin". Equals (UserName) && "123456". Equals (password)) {Ctx.setproperty ("Username", UserName); Ctx.setproperty ("Password", password); Ctx.setproperty ("MSG", "Landing success"); return true; }Else{Ctx.setproperty ("MSG", "Login Failed"); return false; } } PublicString getloginmessage () {Messagecontext context=Messagecontext.getcurrentmessagecontext (); Servicegroupcontext CTX=Context.getservicegroupcontext (); String msg= Ctx.getproperty ("username") + "," +ctx.getproperty ("MSG")); returnmsg; }}
The Searchsessionservice.java code is as follows:
PackageCom.hoo.service;ImportOrg.apache.axis2.context.MessageContext;ImportOrg.apache.axis2.context.ServiceGroupContext; Public classSearchsessionservice { Publicstring Findsessionmessage (String key) {Messagecontext context=Messagecontext.getcurrentmessagecontext (); Servicegroupcontext CTX=Context.getservicegroupcontext (); if(Ctx.getproperty (key)! =NULL){ return"Find session:" + key + "," +Ctx.getproperty (key); }Else{ return"Not found <" + key + "> data"; } }}
The above two webservice all use the Servicegroupcontext object.
Second, write the Services.xml, publish 2 webservice at the same time, and set scope to application,services.xml content as follows:
<Servicegroup> <Servicename= "Loginsessionservice"Scope= "Application"> <Description>Web Service Session Example</Description> <parametername= "ServiceClass">com.hoo.service.LoginServiceApplication</parameter> <messagereceivers> <MessagereceiverMEP= "Http://www.w3.org/2004/08/wsdl/in-out"class= "Org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> <MessagereceiverMEP= "Http://www.w3.org/2004/08/wsdl/in-only"class= "Org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </messagereceivers> </Service> <Servicename= "Searchsessionservice"Scope= "Application"> <Description>Web Service Search Session Example</Description> <parametername= "ServiceClass">Com.hoo.service.SearchSessionService</parameter> <messagereceivers> <MessagereceiverMEP= "Http://www.w3.org/2004/08/wsdl/in-out"class= "Org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> <MessagereceiverMEP= "Http://www.w3.org/2004/08/wsdl/in-only"class= "Org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </messagereceivers> </Service></Servicegroup>
Inside Eclipse, File->new->other->axis2 service Archiver->next
Choose the WebService class file to store the path, the general selection to the Bin folder is OK
Then, Next->next->next, the interface for selecting XML appears:
Uncheck "Generate the service XML automatically" and select the Services.xml file you just saved in the file browse box above, next
Select the output path and file name (there is no relationship between the filename and WebService's name, WebService's name is defined in the XML file), Next->finish, the prompt to create a successful
Then use the method in the previous tutorial to send this AAR file WebService
Third, write the client code:
ImportJavax.xml.namespace.QName;ImportOrg.apache.axis2.AxisFault;Importorg.apache.axis2.addressing.EndpointReference;Importorg.apache.axis2.client.Options;Importorg.apache.axis2.rpc.client.RPCServiceClient; Public classloginsessionserviceclient { Public Static voidMain (string[] args)throwsAxisfault {String target= "Http://localhost:8080/axis2/services/LoginSessionService"; Rpcserviceclient Client=Newrpcserviceclient (); Options Options=client.getoptions (); //Open the support for the sessionOptions.setmanagesession (true); EndpointReference EPR=Newendpointreference (target); Options.setto (EPR); QName QName=NewQName ("http://service.hoo.com", "Login"); //specify the method to invoke and pass the parameter data, and set the type of the return valueObject[] result = client.invokeblocking (QName,NewObject[] {"admin", "123456"},NewClass[] {Boolean.class }); System.out.println (result[0]); QName=NewQName ("http://service.hoo.com", "Getloginmessage"); Result= Client.invokeblocking (QName,NewObject[] {NULL},NewClass[] {String.class }); System.out.println (result[0]); Target= "Http://localhost:8080/axis2/services/SearchSessionService"; EPR=Newendpointreference (target); Options.setto (EPR); QName=NewQName ("http://service.hoo.com", "Findsessionmessage"); Result= Client.invokeblocking (QName,NewObject[] {"username"},NewClass[] {String.class }); System.out.println (result[0]); QName=NewQName ("http://service.hoo.com", "Findsessionmessage"); Result= Client.invokeblocking (QName,NewObject[] {"MSG"},NewClass[] {String.class }); System.out.println (result[0]); QName=NewQName ("http://service.hoo.com", "Findsessionmessage"); Result= Client.invokeblocking (QName,Newobject[] {"Password"},NewClass[] {String.class }); System.out.println (result[0]); }}
After the run, the results are as follows:
True
Admin, Login successful
Find Session:username,admin
Find Session:msg, login successful
Find session:password,123456
If you change the contents of the Services.xml file <service name= "Searchsessionservice" scope= "Application" > to Scope= Transportsession, there will be no content errors found in the session.