Web Services Asynchronous
Perhaps something very different and weird to see around is an asynchronous web services, but yes! This beast really exists, at least it’s possible to generate an asynchronous Web Services Client with some binding customization. The JAX-WS provides support for invoking a Web Services through an asynchronous client. This asynchronous calling support is provided in two models: Callback and Polling. In the Callback approach we (as the client) just have to wait for a call arrives with our response, and in the Polling approach we have to check once in a while whether our response is already available and then get it.
Asynchronous communication is a feature controlled by WSDL binding customization in JAX-WS. The binding process is instrumented by a tag enableAsyncMapping for asynchrony when generating web service client artifacts. One option is to use an external standalone XML file for the WSDL binding customization. In addition to the external binding file approach, another option would be embed binding declarations directly into a WSDL document, and use this approach to customize the client. In our example, we are going to use the first option, an external XML binding file. Let’s dive into our sample.
THINKING AND DRAWING…
Well, nothing better than a clean blank paper sheet to help you think before execute something. It’s always a good idea have, at least, some sketches of what we are going to implement.
SYNCHRONOUS CALLING
Actually, I was in doubt about add this sketch regarding the synchronous calling of a Web Services, of course, this is so obvious for everybody (I think), but just not to be unfair with the synchronous calling and for the sake of comparison between all the the calling techniques, let’s add its drawing as well.
As mentioned before, there’s nothing new in Picture 1 below, the Consumer fires the request and waits for around fifteen seconds for the response, getting completely blocked at this period of waiting. There’s one big advantage in this case! You have a good excuse to grab a hot cup of coffee. :-)
Picture 1. The synchronous calling scenario
Now let’s start drawing the not so common scenarios, starting with the Asynchronous Web Services Calling using the pooling technique. We fires a request once in a while to check if the response is ready to be sent back. When it does, we call the method that provides the response. While the response is not ready, the client could be doing any other tasks. We will see the coding of this scenario soon in this article. Below, at Picture 2, we have the behaviour of this scenario.
Picture 2. The asynchronous polling calling scenario
At last, but probably the most interesting option, we have the web services asynchronous calling using a callback handler. Again, here the client have the opportunity to work on other tasks while waiting for the response to arrive. As the response comes through a callback, the client doesn’t have to do anything to trigger the receiving, only have to have an interface implemented, called AsyncHandler (JAX-WS 2.0), in order to the server have a point to deliver its response. The Picture 3 below show how this scenario works.
Picture 3. The asynchronous callback scenario
NOW, THE PROOF!
FIRST, ON THE SERVER SIDE…
THE WEB SERVICE IMPLEMENTATION AND “DEPLOYMENT”
On the server side there’s nothing new regarding the implementation, as we stated at the beginning of this article, the asynchronous communication is a feature of the JAX-WS web services clients. Here we are going to implement the server side... Actually not exactly here... ;-)
The whole article, source code and everything else, available at...
https://ualterazambuja.com/2016/04/23/web-services-asynchronous/