Axis2 series of WebService tutorials (6) cross-service session management

Source: Internet
Author: User

This article describes how to use axis2 to manage Sessions of the same service. However, for a complex system, it is impossible to have only one WebService. For example, there will be at least one WebService that manages the user (user login and registration) and processes the business. In this case, the session state must be shared among multiple WebService services, also known as cross-service session management. The steps for implementing cross-service session management are similar to those for implementing session management for the same service, but there are still some differences. The steps for implementing cross-service session management are as follows:

The following three steps are required to implement cross-service session management:

1. Use messagecontext andServicegroupcontextObtain and set the key-value pair.

2. Add a scope attribute for the <service> element corresponding to the WebService class for session management, and set the attribute valueApplication.

3. Use setmanagesession (true) on the client to enable the session management function.

From the above steps, we can see that there are differences between implementing cross-service session management and implementing session management for the same service in the first two steps, while Step 2 is the same. The following is an instance of cross-service session management. In this example, there are two WebService classes: loginservice and searchservice. The Code is as follows:
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) {// Step 1: Set the key-value pair to messagecontext MC = messagecontext. getcurrentmessagecontext (); servicegroupcontext SGC = MC. getservicegroupcontext (); SGC. Setproperty ("login", "Successful Logon"); Return true;} else {return false;} Public String getloginmsg () {// step 1st: obtain the value messagecontext MC = messagecontext in the key-value pair. getcurrentmessagecontext (); servicegroupcontext SGC = MC. getservicegroupcontext (); Return (string) SGC. getproperty ("login") ;}} searchservice. java package service; import Org. apache. axis2.context. messagecontext; import Org. apache. axis2.c Ontext. servicegroupcontext; public class searchservice {Public String findbyname (string name) {// Step 4: obtain value messagecontext MC = messagecontext in the key-value pair. getcurrentmessagecontext (); servicegroupcontext SGC = MC. getservicegroupcontext (); If (SGC. getproperty ("login ")! = NULL) Return "found Data <" + name + ">"; else return "user not logged on ";}}

The configuration code in the services. xml file is as follows:

<ServiceGroup> <! -- Step 2: add the scope attribute, set the property value to Application --> <service name = "loginservice" Scope = "application"> <description> logon service </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> <! -- Step 2: add the scope attribute, set the property value to Application --> <service name = "searchservice" Scope = "application"> <description> Search Service </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>

Step 2 is similar to the method described above.

The following shows how to use two stub class object instances to access the client code of the preceding two WebServices:

       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);        }

After the above code is executed, the following information will be output:

Logon successful
Data found <ABC>

You can change the scope attribute value to transportsession to see what will be output!
In fact, session management of axis2 is also implemented through cookies, similar to session management in Web applications. If you use C # to access a WebService that supports session management in the same service, you must specify a cookiecontainer object. The Code is as follows:

       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);       }

To access a cross-service WebService that supports sessions, you do not need to specify the cookiecontainer object. The Code is as follows:

       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"));       }

If you use Delphi (delphi2009 is used in this article, you can test it for other Delphi versions), there are some differences when calling WebServices that support sessions. I tested that using Delphi to call WebService, setting the scope attribute value to transportsession and application can achieve cross-service session management, which is different from Java and C, java and C # must set the scope attribute value to application to support cross-service session management. In Delphi, you do not need to specify a cookiecontainer or other similar objects like C #. Instead, you only need to access the WebService that supports sessions like accessing common WebServices.

 

 

 

 

 

Download the tutorial source code:

Axistest

Axisspring

Axisproject

 

Reprint please indicate the source http://blog.csdn.net/shimiso

Welcome to our technical exchange group: 173711587

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.