This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
Just put together an example for you. Below mentioned is the asynchronous web service example using Async Handler callback approach -
Now publish this web service
Below shown is the client code which uses Dispatch API.
Execute this client and we get below shown output
DynamicClient_With_Async_Callback_Source_Type_Mode_PayLoad :: Asynchronous call Starts: Mon Dec 30 21:53:41 IST 2013
DynamicClient_With_Async_Callback_Source_Type_Mode_PayLoad :: Client continues other work
MyHandler :: Async Source Handler called Mon Dec 30 21:53:48 IST 2013
MyHandler :: Async Source Handler ended Mon Dec 30 21:53:48 IST 2013
DynamicClient_With_Async_Callback_Source_Type_Mode_PayLoad :: Client decides to check the result Mon Dec 30 21:53:50 IST 2013 with Response
Asynchronous request with Future responded at: Mon Dec 30 21:53:50 IST 2013 with Response = <ns2:addResponse xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="http://arith/"><return>31</return></ns2:addResponse>
you can notice that I have added 7 sec delay in math service (in server side implementation code). So our client code starts at 21:53:41 IST 2013 and makes aynch call. Request is sent to Math Service. And Control is returned back to Client code resulting "Client continues other work" statement. After 7 second delay , Math Service return response back to Client. Since we have implemented Callback handler, so , response goes to "MyHandler" class. And then next two statements are printed on console at 21:53:48 IST 2013 [ exactly after 7 sec gap] .
Now Client side code have result stored in Future<?> object.
After 9 sec gap (placed in client side through Thread.sleep() method) , Client reads response from Future<?> code and prints Response on Console.
In similiar pattern, Polling based Asynch web service can be written.
Below mentioned is the Client code which uses wsimport generated classes.
Output of this client is
NormalClient :: Server Aysnc call made at Mon Dec 30 22:47:31 IST 2013
NormalClient :: Client continues other work
MyHandler :: Async Source Handler called at Mon Dec 30 22:47:38 IST 2013
Returned value fom Server Side 26
MyHandler :: Async Source Handler ended at Mon Dec 30 22:47:38 IST 2013
NormalClient :: Client decides to check the result Mon Dec 30 22:47:40 IST 2013 with Response
Asynchronous response with Future responded at: Mon Dec 30 22:47:40 IST 2013 with Response = 26