Monday, September 29, 2014

Writing a Simple AXIS 2 Service

In this blog post I'm going to discuss on how to write a simple axis2 service. Here I'm using code first approach to write the service. First of all, we can start from writing a java class with a simple method.

import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.http.HTTPConstants;

public class SimpleService {

    public void print(String value) throws PrintException {
         if (value ==null) {
              throw new PrintException("value is null");
         }
        MessageContext context = MessageContext.getCurrentMessageContext();
        context.setProperty(HTTPConstants.RESPONSE_CODE, 200);

        System.out.println("Value = " + value);
    }
}

The PrintException method would be

public class PrintException extends Exception {
    public PrintException(String message) {
        super(message);
    }
}

In order to archive this as a valid axis2 service there should be a services.xml associated with the service inside the service archive file.  It contains the deployment description of the service.

<service>
<parameter name="ServiceClass" locked="false">SimpleService</parameter>
<operation name="echo">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>  
</service>

The class name should be the fully qualified class name. As in my example the SimpleService class in not in any package I have simply used the class name.

Axis2 has a set of built-in message receivers. According to this sample services.xml file it says that operation 'print' of this service should use the Axis2 Java class 'org.apache.axis2.rpc.receivers.RPCMessageReceiver' as its message receiver class.

If an operation is an in-only operation you can use

org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver

Create the archive file using

jar cvf SimpleService.aar *




No comments:

Post a Comment