Popular Posts

Wednesday, December 12, 2007

Deploying an axis2 service in application scope

If you deploy a service without specifying a scope, it will be considered as sessionless and will be deployed in request session scope. In other words, request scope is the default session scope of an axis2 web service.
When a service is deployed in request session scope, an instance of the service implementation class is created for every invocation. Suppose you want to get rid of that and maintain a single instance of service class throughout the server life time.
Then you should deploy the service in application session scope. Lets see how it can be achieved and tested the service invocation using WSO2 WSAS.

First, create a simple java bean as follows.

public class Employee {

private int Age;
private String name;
private String emp_id;
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmp_id() {
return emp_id;
}
public void setEmp_id(String emp_id) {
this.emp_id = emp_id;
}
}

Now create the service descriptor (services.xml) for the above class.

<service name="EmployeeService" scope="application">
<messagereceivers>
<messagereceiver mep="http://www.w3.org/2006/01/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver">
<messagereceiver mep="http://www.w3.org/2006/01/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver">
</messagereceiver>
<parameter name="ServiceClass" locked="false">
org.wso2test.Employee
</parameter>
</messagereceiver>

Here, we define the session scope of the service as 'application', which will create a single instance when server is started.

Now, include these in a deployable service archive. (Create a META-INF directory and add the services.xml in there and save the archive as an aar)

Next, deploy the service archive using WSO2 WSAS management console. In the WSAS management console, go to "Services and Service Group Management' page and select 'EmployeeService'. Then click on 'Tryit' to test the service invocation.
The all getter and setter methods of the service class are displayed there. Click on setName and enter a string value in the given text box. You will get a NuLL response since it is an request only invocation. Next, select getName and click on getName button. You will get the name you have set in the previous step.

The above scenario demonstrates the creation of a single service class instance using application scope.

No comments: