Thursday, October 6, 2011

axis2 - Simpe Hello World Service

We will start with a simple Java class which will be turned into a service with the necessary packaging. Next a client will be created using the code generated by the WSDL2Java code which will be used to invoke the service we created. You will have to download Apache Axis2-Nightly build Satandard Binary Distribution to try this out and the source code of both client and service are available here. Now simply follow the steps below.

Step 1: A simple Java class

First we need a Web service and an operation in that service for a client to invoke. Lets develop a Web service with an operation which will echo a string value. The simplest way to do this is with a Java class as shown below:
/**
 * The service implementation class
 */
public class SimpleService {

 /**
  * The echo method which will be exposed as the 
  * echo operation of the web service
  */
 public String echo(String value) {
  return value;
 }
}
---------------------------------------------------------------
save as - SimpleService.java
---------------------------------------------------------------

Step 2: The service descriptor

Each Axis2 service must have a services.xml file which will inform Axis2 about the service. Following is the services.xml file contents for the SimpleService Web service.
<service>
 <parameter name="ServiceClass" 
          locked="false">SimpleService</parameter>
 <operation name="echo">
  <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
 </operation>    
</service>
The "service" element encapsulates the information about a single service. Within the "service" element there should be a parameter specifying the service implementation Java class. The parameter is specified as a "parameter" element as shown below.
<parameter name="ServiceClass" locked="false">SimpleService</parameter>
The second child element of the "service" element "operation" element describes the operation and the message receiver that is to be used for that operation. For this service we set the "name" attribute of the "operation" element to the name of the method that we wish to expose as a Web service operation. Hence we set it to "echo":
<operation name="echo">
Axis2 provides a MessageReceiver based on Java reflection and the "messageReceiver" element declaring that org.apache.axis2.rpc.receivers.RPCMessageReceiver should be used.
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>

Step 3: Packaging the service

Axis2 expects services to be packaged according to a certain format. The package must be a .jar file with the compiled Java classes and a META-INF directory which will hold the services.xml file. The jar file can be name .aar to distinguish it as an Axis2 service archive. It's important to note that the part of the file name before ".aar" is the service name. Create a temp directory in the same location where the SimpleService.java file exists
[Linux]
mkdir temp

[Windows]
md temp
Now compile the SimpleService.java class and move the SimpleService.class file to the temp directory.
javac SimpleService.java -d temp/
Create a META-INF directory within the "temp" directory and copy the services.xml file into the META-INF directory. Change directory to the "temp" directory and use the "jar" command as follows to create the service archive named SimpleService.aar.
jar -cvf SimpleService.aar *
 
to Deploploy 
 
simply put the files in axis2\WEB-INF\services directory or use upload wizard.
Please note that jvm used to compile the class should be matching with tomcat and axis2 version
 

Step 5: Accessing the service with a generated client

Now lets use the WSDL2Java tool generate the client side stubs to interact with the service. [Linux]
$ sh WSDL2Java.sh -uri http://localhost:8080/axis2/services/
SimpleService?wsdl

[Windows]
WSDL2Java.bat -uri http://localhost:8080/axis2/services/
SimpleService?wsdl
This generates two .java files and we will be using the org.apache.axis2.SimpleServiceStub to invoke the "echo" operation of the service. Now lets create a new Client.java class which uses the org.apache.axis2.SimpleServiceStub. For simplicity lets create the Client.java file in the same package as the generated code (i.e.org.apache.axis) and save it along with the other generated code. package org.apache.axis2;

package org.apache.ws.axis2;

import org.apache.ws.axis2.SimpleServiceStub.EchoResponse;

public class Client {

public static void main(String[] args) throws Exception {

SimpleServiceStub stub = new SimpleServiceStub();

//Create the request
SimpleServiceStub.Echo request = new SimpleServiceStub.Echo();
//request.setParam0("Hello world");
request.setArgs0("Hello world");

//Invoke the service
EchoResponse response = stub.echo(request);

System.out.println("Response : " + response.get_return());
}

}----------------------------------------------------------------
save as - Client.java
----------------------------------------------------------------
Now to we can compile and run the client code. Its is important to note that the classpath must have all the jars in the "lib" directory of the axis2-std-1.0-RC1-bin distribution when compiling and running the client.
The following command will compile the client's source.
$ javac -extdirs E:\applications\axis2\axis2-1.5.2-bin\axis2-1.5.2\lib org/apache/ws/axis2/*.java - We can run the client program as shown below.
$ java -Djava.ext.dirs=E:\applications\axis2\axis2-1.5.2-bin\axis2-1.5.2\lib org/apache/ws/axis2/Client The output will be :
Response : Hello world

Step 6: Monitoring the messages

To view the request and response SOAP messages we can use the tcpmon tool. We can start the SimpleHTTPServer on port 9090 and make the tcpmon listen on port 8080 and forward the requests to port 9090. Using "-p9090" as an additional argument in starting the SimpleHTTPServer we can start it on port 9090. Example: sh http-server.sh /path/to/my-axis2-repo -p9090 Now when we run the client once again we can view the messages.


Conclusion

This example is the most simple example one can use to start using Axis2. I hope this will be a starting point for you to explore more and more about Apache Axis2. If you have any questions please feel free to mail the axis-user mailing list, with [Axis2] as the subject prefix.

No comments:

Post a Comment

Blog Archive