How to Write Axis2 Web Service Clients for the Cloud
IntroductionCloud computing has become a buzz word in the IT field, and people are developing application for the Clouds. One of the cool features we can find with Cloud apps like Amazon is that they support the Web service API to configure, run and monitor the instances in the Cloud. Due to various reasons Amazon EC2 has become one of the most commonly use commercial Clouds, so today we are going to discuss how to write a Web service client for Cloud apps like Amazon, Writing a client for other Cloud apps would be much the same. Since most of them will have the same set of API as well as security requirements.
Axis2 can be used both as a server and a client to invoke services. In previous articles we have discussed a number of different accepts of Axis2 on the server side. One of the other most important sides of Axis2 is how to use Axis2 as a client to invoke other Web services. When writing to a client using Axis2 there are two main approaches, the first approach is to write the client by hand, the second is to use tools to generate the client stub to invoke the remote service.
Hand written client vs generated clientIn the first approach, we have to write the code for all the processing, which includes, serialization and de-serialization message, invoking the services and etc.., put simply there are a number of steps we have to perform to invoke the service. However the most important thing is that we can write the most efficient code when we write the client by hand, since we can optimize the code for our particular use case.
On the other hand, in the second approach what we do is use the Axis2 tool to generate the client side stub to invoke the service. With that approach we do not need to worry about the message serialization and de-serialization, setting up the system or invoking the service. Generated code handles all that for us. So the code generation approach is more convenient and time saving, especially to minimize the number of errors.
Setting up Axis2The first step of creating the Axis2 client stub for a remote service is setting up the Axis2 client side, for that we need to download Axis2 binary distribution. We can download Axis2 binary distribution from the Axis2 official web site. Download that and then extract the downloaded zip file. Now we are ready to generate the stub for the Amazon EC2 Web service API. We can find the EC2 WSDL from following URL, http://ec2.amazonaws.com/doc/2009-03-01/AmazonEC2.wsdl.
If you are new to WSDL then you will find it somewhat hard to understand the WSDL, the nice thing is that you do not need to understand the WSDL to generate the Axis2 client. That is one of the cool feature of code generation; it reduces the complexity. The tool can process the WSDL and generate the code for us. Now let's generate client stub for the EC2 WSDL.
First go to the "bin" directory of the extracted Axis2 distribution, there you can find a number of shell scripts and Windows batch files. So depending on the OS platform you have to use either shell scripts or batch files. When we generate the code we can pass a number of options to the code generation tool, however in this particular case we are not going to discuss all of them. Run the following command to generate the code,
./wsdl2java.sh -uri http://ec2.amazonaws.com/doc/2009-03-01/AmazonEC2.wsdl -o out
wsdl2java.bat -uri http://ec2.amazonaws.com/doc/2009-03-01/AmazonEC2.wsdl -o out
Then it will create a directory called "out" inside the bin directory and create the code inside that. If you browse you will able to see a large file. DO NOT try to open that or read through it, it is so complex to read, and there is no reason to read or edit that file. That single class has all the serialization and de-serialization code as well as the service invocation code.
Next we are going to use some other code generation parameters and generate code in a different manner. The idea is to generate data binding (serialization and de-serialization) code into septate classes. Next run the following command. Notice the difference?
./wsdl2java.sh -uri http://ec2.amazonaws.com/doc/2009-03-01/AmazonEC2.wsdl -o out_unpack -u
wsdl2java.bat -uri http://ec2.amazonaws.com/doc/2009-03-01/AmazonEC2.wsdl -o out_ unpack -u
Page 1 of 3