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.
The moose likes Web Services and the fly likes Making Asynchronous Web Service Calls Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Making Asynchronous Web Service Calls" Watch "Making Asynchronous Web Service Calls" New topic
Author

Making Asynchronous Web Service Calls

Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 220
Hi All,

I want one simple client and server side java class which makes Asynchronous Web Service call. Please suggest me a link.

Thanks:
Ramakrishna K.C
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1000
    
    1

http://docs.oracle.com/cd/E24329_01/web.1211/e24969/asynch.htm.
http://java.boot.by/scdjws5-guide/ch11s02.html

Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 220
Thanks for the reply.

I links which you posted have concept with half example. I want full program for reference(any simple program like hello-world/calculator) with explanation.

Thanks:
Ramakrishna K.C
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1000
    
    1

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.

Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1000
    
    1

Above example uses Dispatch API which allows you to work at XML level.

If you want to deal at Object level (High level API) then follow below mentioned steps.

Server side code remains same.

We need to run wsimport command to generate JAX-WS artifacts.
We need to make sure that we have async methods being generated from wsimport command. For that , we need to enable async mapping.

For that, Create bindings.xml file



Now run wsimport command and use this binding.xml there.

wsimport -keep http://localhost:8848/mathService?wsdl -verbose -p generated -b ..\bindings.xml


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


Happy Learning !!
Ramakrishna Udupa
Ranch Hand

Joined: Aug 09, 2013
Posts: 220
Thanks a lot Abhay Agarwal.
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1000
    
    1

Nice to know that you liked the solution

~ abhay
 
jQuery in Action, 2nd edition
 
subject: Making Asynchronous Web Service Calls
 
Similar Threads
regarding Asynchronous request-response feature
Asynchronous Web service call
Can any web service be called Asynchronously
calling asynchronous web service synchronously
Asynchronous communication between Services