December 19, 2014
Hot Topics:

Web Services-Not Always the Best Solution

  • May 28, 2009
  • By Liviu Tudor
  • Send Email »
  • More Articles »

Communicating via Web Services

The web service-based implementation of the address book service uses Apache Axis2, because it lets you deploy simple Java classes as web services using a manifest-like file (services.xml) that defines the web service.

Author's Note: I won't go into the structural details of the services.xml file or how to deploy the web service itself, as that's outside the scope of this article. You can find detailed information on the Apache Axis2 website.

The services.xml file should look like this:

<service name="addressbook">
    <description>
        This is a sample AddressBook Web Service 
        with one operation: findFriend
    </description>
   <messageReceivers>
     <messageReceiver mep=
       "http://www.w3.org/2004/08/wsdl/in-only" 
       class="org.apache.axis2.rpc.receivers.
          RPCInOnlyMessageReceiver"/>
     <messageReceiver mep=
       "http://www.w3.org/2004/08/wsdl/in-out" 
       class="org.apache.axis2.rpc.receivers.
          RPCMessageReceiver"/>
    </messageReceivers>
    
    <parameter name="ServiceClass" 
      locked="false">bl.AddressBook</parameter>
 </service>

The "specialized" client for this service is based on the Axis2 packages. You can find it in the AddressBookWSClient class (you may need to change the URL depending on your Axis2 and/or Tomcat configuration):

public class AddressBookWSClient 
   extends SimpleMeasurementImpl 
   implements AddressBookContract
{
   @Override
   public AddressBean findFriend(String name) 
   {
      timeStart = System.nanoTime();
      try
      {
         RPCServiceClient client = new RPCServiceClient();
         Options options = client.getOptions();
         EndpointReference targetEPR = new EndpointReference(
            "http://localhost/axis2/services/addressbook");
         options.setTo(targetEPR);
         QName op = new QName("http://bl", "findFriend");
         Object[] opFindArgs = new Object[] { name };
         Class[] returnTypes = new Class[] { AddressBean.class };
         Object[] response = client.invokeBlocking(op, 
            opFindArgs, returnTypes);
         AddressBean result = (AddressBean) response[0];
         return result;
      }
      catch( Exception e )
      {
         e.printStackTrace();
         return null;
      }
      finally
      {
         timeEnd = System.nanoTime();
      }
   }
}

Note that this is a synchronous rather than an asynchronous client, which means that when the client.invokeBlocking call finishes, communication with the web service is complete, and all the data has been sent, received, and processed.

Assuming that you deployed the web service to Tomcat and everything is ready, you can now run the web service client look at the output:

class webservice.client.AddressBookWSClient found 
   Liv : Oxford Street , London , SW1 in 1,257,869,940

Remember that the time at the end of the output is in nanoseconds, so the above number translates to about 1.25 seconds.

Author's Note: It's worth mentioning that I ran both Tomcat and the client on the same machine. While it's possible that having both processes running on the same machine might slow each other down, running them on the same machine also eliminates all other network traffic, because all the communication happens via localhost, so one should compensate for the other. Also, as discussed in my previous article, there are faster PC's out there than mine that will produce sub-second results; however, the speed of the test machine is not relevant, because the execution times are compared only against each other. I believe that the comparisons will show similar relative results on both faster (and slower) PC's.





Page 2 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel