Pages

Saturday, October 27, 2012

Dispatching Web Service calls (Async, Sync, OneWay)

If you decide to work with Web Services as a possible SOA implementation, you have to read and understand very much specifications. In Java you may start with JAX-WS and JAX-RS as a SOAP and RESTful way to get your things done. In this Blog I jump deep into JAX-WS to use the Dispatch API. You can see the different synchronous and asynchronous ways to send and recieve messages from a Web Service. Most important goal is to unterstand how "OneWay" dispatching works.


Setting up the environment


To keep things as simple as possible I build up a small Web Service Endpoint and Web Service Client using Java SE. (No EE Container needed) A simple JAXB annotation binded message class will be the communication artifact. This leads us to simple Szenario:
  • Java SE Web Server (Endpoint, JAX-WS)
  • Java SE Client (Dispatch, JAX-WS)
  • JAXB compatible Message Class


JAXB compatible Message Class


First we create a class that can deliver our messages. It has to be JAXB friendly, so we use  corresponding annotations to declare Java and XML Binding.

MyJaxbMessage.java


Java SE Web Server (Endpoint, JAX-WS)


Then we create a simple server instance that will respond to our client dispatcher. The server will wait 3 seconds to simulate some work.

MessagingServer.java


Java SE Client (Dispatch, JAX-WS)


Now we build up our simple client that calls the server in four different ways.

MessagingClient.java - Part 1


Dispatch a synchronous call. Blocking and waiting for server response 3 seconds.

MessagingClient.java - Part 2

Result Log

1 - Normal - Start: Sat Oct 27 20:42:44 CEST 2012
1 - Normal - End: Sat Oct 27 20:42:47 CEST 2012 response: Hello, Foo 1 from server: Sat Oct 27 20:42:47 CEST 2012



Dispatch a OneWay call. Important: The call is synchronous!

MessagingClient.java - Part 3

Result Log

2 - OneWay - Start: Sat Oct 27 20:42:47 CEST 2012
2 - OneWay - no response End: Sat Oct 27 20:42:50 CEST 2012


As shown in result log the OneWay call was synchronous. The server responded just an empty message. This is usefull to reduce network communication but will NOT make a call asynchronous. Same counts for the @OneWay annotation.


Asynchronous call with Response

MessagingClient.java - Part 4

Result Log

3 - Asynchronous - Start: Sat Oct 27 20:42:50 CEST 2012
3 - Asynchronous - End: Sat Oct 27 20:42:50 CEST 2012
 - 3.1 simulating async work
 - 3.1 simulating async work
 - 3.1 simulating async work
 - 3.1 simulating async work
 - 3.2 Asynchronous request respondet at: Sat Oct 27 20:42:54 CEST 2012 with Response: Hello, Foo 4 from server: Sat Oct 27 20:42:53 CEST 2012



Asynchronous call with Future

MessagingClient.java - Part 5

Result Log

4 - Asynchronous with Future Start: Sat Oct 27 20:42:54 CEST 2012
4 - Asynchronous with Future End: Sat Oct 27 20:42:54 CEST 2012
 - 4.1 simulating async work
 - 4.1 simulating async work
 - 4.1 simulating async work
 - Future responded asynchronous Hello, Foo 3 from server: Sat Oct 27 20:42:57 CEST 2012
 - 4.1 simulating async work
4 - Asynchronous request with Future respondet at: Sat Oct 27 20:42:57 CEST 2012 with Response: de.hinkel.jaxws.MyJaxbMessage@ee51b2c



As shown in the result logs we sucessfully made 4 diffierent kinds of Web Service calls just by invoking different methods an the Dispatch class. 1. Synchronous with response, 2. Synchronous without response, 3. Asynchronous with response, 4. Asynchronous with future. Moreover we could see how Java SE enables advanced Web Service Endpoint- and Client testing by a programmatically approach of JAXB and JAX-WS usage.

2 comments: