6. Manage sessions across multiple WebServices

Source: Internet
Author: User

When there are multiple WebServices, we need to manage their sessions. In this case, we have to rely on ServiceGroupContext to save the session information. Then, when publishing WebService, services. the scope of the service expression of the xml file will not talk about the request or transportsession, but the application; finally, the session management should be enabled, that is, options. setManageSession (True);

1. First, the session management code for multiple WebServices is as follows:

Code package com. hoo. service; import org. apache. axis2.context. messageContext; import org. apache. axis2.context. serviceGroupContext;/*** <B> function: </B> manage multiple Session information * @ author hoojo * @ createDate 2011-3-9 05:11:07 * @ file LoginSessionService. java * @ package com. hoo. service * @ project Axis2WebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */public class LoginSessionService {public boolean login (String userName, String password) {MessageContext context = MessageContext. getCurrentMessageContext (); ServiceGroupContext ctx = context. getServiceGroupContext (); if ("admin ". equals (userName) & "123456 ". equals (password) {ctx. setProperty ("userName", userName); ctx. setProperty ("password", password); ctx. setProperty ("msg", "Login successful"); return true;} ctx. setProperty ("msg", "Logon Failed"); return false;} public String getLoginMessage () {MessageContext context = MessageContext. getCurrentMessageContext (); ServiceGroupContext ctx = context. getServiceGroupContext (); return ctx. getProperty ("userName") + "#" + ctx. getProperty ("msg ");}}

The operation is the same as the previous Session, but the ServiceGroupContext context is used to access the session information.

You also need to use a Service to query the session information. The SearchService code is as follows:

Code package com. hoo. service; import org. apache. axis2.context. messageContext; import org. apache. axis2.context. serviceGroupContext;/*** <B> function: </B> query messages in a Multi-Service Session * @ author hoojo * @ createDate 2011-3-9 05:22:39 * @ file SearchSessionServcie. java * @ package com. hoo. service * @ project Axis2WebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */public class Sear ChSessionServcie {public String findSessionMessage (String key) {MessageContext mc = MessageContext. getCurrentMessageContext (); ServiceGroupContext ctx = mc. getServiceGroupContext (); if (ctx. getProperty (key )! = Null) {return "The data found <" + key + "," + ctx. getProperty (key) + ">" ;}else {return "no data <" + key + "> found ";}}}

2. Write services. xml to publish these two services, which were not the same before. This time, two services are configured with a service. xml file, and two services are released at the same time. The Xml Code is as follows:

Code <serviceGroup> <service name = "LoginSessionService" scope = "application"> <description> Web Service Session example </description> <parameter name = "ServiceClass"> com. hoo. service. loginSessionService </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 example </description> <parameter name = "ServiceClass"> com. hoo. service. searchSessionServcie </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>

3. After the release is complete, you can view the published WebService through http: // localhost: 8080/axis2/services/listServices and write the test code of the client. The code is as follows:

Code package com. hoo. service; 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;/*** <B> function: </B> multi-Session management, WebService client request code * @ author hoojo * @ createDate 05:17:15 * @ file LoginSessionServiceClient. java * @ package com. hoo. service * @ project Axis2WebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */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 (); options. setManageSession (true); EndpointReference epr = new EndpointReference (target); options. setTo (epr); QName qname = new QName ("http://service.hoo.com", "login"); // specify the call method and pass parameter data, and set the type of the returned value 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]) ;}}

The result is as follows:

True

Admin # login successful

Data found <userName, admin>

Data found <msg, login successful>

Data found <password, 123456>

4. If the service. xml file <service name ="SearchSessionService"Scope ="Application"> Change the content to scope = transportsession to see what the situation is. Cannot find the content in the session.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.