Professional Documents
Culture Documents
- Stack Overflow
Page Saved!
Add Tags
x Dismiss
Sign up
I need to send multiple requests to many different web services and receive the results. The problem is that, if I send the requests one by one
it takes so long as I need to send and process all individually.
I am wondering how I can send all the requests at once and receive the results.
As the following code shows, I have three major methods and each has its own sub methods. Each sub method sends request to its
associated web service and receive the results;therefore, for example, to receive the results of web service 9 I have to wait till all web
services from 1 to 8 get completed, it takes a long time to send all the requests one by one and receive their results.
As shown blew none of the methods nor sub-methods are related to each other, so I can call them all and receive their results in any order,
the only thing which is important is to receive the results of each sub-method and populate their associated lists.
main (){
retrieveAllLists();
}
retrieveAllLists(){
retrieveStudents();
retrieveDoctors();
retrievePatients();
}
retrieveStudents(){
retrieveDoctors(){
retrievePatients(){
https://stackoverflow.com/questions/21670451/how-to-send-multiple-asynchronous-requests-to-different-web-services 1/8
7/10/2017 java - How to send multiple asynchronous requests to different web services? - Stack Overflow
edited Feb 19 '14 at 6:32 asked Feb 10 '14 at 6:31
J888
Page Saved!
523 4 22 61
Add Tags
You might need to add some context to your question. At first glance, a Publish-Subscribe model here might
have been advisable but depending on your use case, that might not be advisable. For example, do you
have to wait for a particular event to execute retrieveAllLists or can you preemptively retrieve the
results? How would caching the results work out for you? – kolossus Feb 18 '14 at 19:40
Loading Recommendations
@kolossus question is updated hope it answers your quesitons – J888 Feb 19 '14 at 6:32
Bit sad you let the bounty expire, I think you got plenty high quality response. – flup Feb 27 '14 at 12:52
6 Answers
That is a simple fork-join approach, but for clarity, you can start any number of threads and
retrieve the results later as they are available, such as this approach.
});
List<Future<String>> results = pool.invokeAll(tasks);
UPDATE, COMPLETE:
Here's a verbose, but workable solution. I wrote it ad hoc, and have not compiled it. Given the
three lists have diffent types, and the WS methods are individual, it is not really modular, but
try to use your best programming skills and see if you can modularize it a bit better.
stasks.add(new Callable<List<StudentsResults>>() {
public List<StudentsResults> call() throws Exception {
return retrieveStdWS1();
}
});
stasks.add(new Callable<List<StudentsResults>>() {
public List<StudentsResults> call() throws Exception {
return retrieveStdWS2();
}
});
stasks.add(new Callable<List<StudentsResults>>() {
public List<StudentsResults> call() throws Exception {
return retrieveStdWS3();
}
});
dtasks.add(new Callable<List<DoctorsResults>>() {
public List<DoctorsResults> call() throws Exception {
return retrieveDocWS4();
}
});
dtasks.add(new Callable<List<DoctorsResults>>() {
public List<DoctorsResults> call() throws Exception {
return retrieveDocWS5();
}
});
dtasks.add(new Callable<List<DoctorsResults>>() {
public List<DoctorsResults> call() throws Exception {
return retrieveDocWS6();
}
});
ptasks.add(new Callable<List<PatientsResults>>() {
public List<PatientsResults> call() throws Exception {
https://stackoverflow.com/questions/21670451/how-to-send-multiple-asynchronous-requests-to-different-web-services 2/8
7/10/2017 java - How to send multiple asynchronous requests to different web services? - Stack Overflow
return retrievePtWS7();
}
Page Saved!
});
ptasks.add(new Callable<List<PatientsResults>>() {
public List<PatientsResults> call() throws Exception {
Add Tags
return retrievePtWS8();
}
});
ptasks.add(new Callable<List<PatientsResults>>() { Loading Recommendations
public List<PatientsResults> call() throws Exception {
return retrievePtWS9();
}
});
Each Callable returns a list of results, and is called in its own separate thread.
When you invoke the Future.get() method you get the result back onto the main thread.
The result is NOT available until the Callable have finished, hence there is no concurrency
issues.
thanks how to put the tasks in the Callable task ? – J888 Feb 19 '14 at 21:18
Have updated the answer to match your case. – Niels Bech Nielsen Feb 20 '14 at 7:34
1 Oh, and to add credibility. I used to teach java concurrent programming at university level. –
Niels Bech Nielsen Feb 25 '14 at 9:12
1 he means that asking whether this is thread safe is asking if a hammer is a hammer. These examples use
the creation of threads. Both his and mine (which are basically the same). So these ARE threads. Hard to
explain lol. – D-Klotz Feb 25 '14 at 22:04
1 Although Camel is a great tool for integration purposes and especially if you have profound knowledge of
integration patterns it also has a set of complex use cases you will have to deal with, so espcially for this
task I find it over-complicated. It is an architectural decision to use Camel, not a task selection criteria –
Niels Bech Nielsen Feb 26 '14 at 6:12
So just for fun I am providing two working examples. The first one shows the old school way of
doing this before java 1.5. The second shows a much cleaner way using tools available within
java 1.5:
import java.util.ArrayList;
@Override
public void run()
{
}
}
https://stackoverflow.com/questions/21670451/how-to-send-multiple-asynchronous-requests-to-different-web-services 3/8
7/10/2017 java - How to send multiple asynchronous requests to different web services? - Stack Overflow
public static class MyThread extends Thread
{
private MyRunnable myRunnable; Page Saved!
MyThread(MyRunnable runnable)
{
Add Tags
super(runnable);
setMyRunnable(runnable);
}
/**
* @param myRunnable the myRunnable to set
*/
public void setMyRunnable(MyRunnable myRunnable)
{
this.myRunnable = myRunnable;
}
}
public ThreadingExample()
{
myThreads = new ArrayList <MyThread> ();
}
if (isComplete() == false)
{
// Sadly we aren't done
return (null);
}
return (allmyData);
}
https://stackoverflow.com/questions/21670451/how-to-send-multiple-asynchronous-requests-to-different-web-services 4/8
7/10/2017 java - How to send multiple asynchronous requests to different web services? - Stack Overflow
for (MyThread x : myThreads)
{
x.start(); Page Saved!
}
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
callables.add(new Callable<String>()
{
@Override
public String call() throws Exception
{
return "This is where I make the call to web service A, and put its
results here";
}
});
callables.add(new Callable<String>()
{
@Override
public String call() throws Exception
{
return "This is where I make the call to web service B, and put its
results here";
}
});
callables.add(new Callable<String>()
{
@Override
public String call() throws Exception
{
return "This is where I make the call to web service C, and put its
results here";
}
});
https://stackoverflow.com/questions/21670451/how-to-send-multiple-asynchronous-requests-to-different-web-services 5/8
7/10/2017 java - How to send multiple asynchronous requests to different web services? - Stack Overflow
try
{
List<Future<String>> futures = service.invokeAll(callables); Page Saved!
for (Future<String> future : futures)
{
System.out.println (future.get());
Add Tags
}
}
catch (InterruptedException e)
{
e.printStackTrace(); Loading Recommendations
}
catch (ExecutionException e)
{
e.printStackTrace();
}
}
}
thanks how about if I use apache camel? – J888 Feb 25 '14 at 21:02
I've never used Camel. But if this descriptions is accurate "Apache Camel is messaging technology glue with
routing. It joins together messaging start and end points allowing the transference of messages from different
sources to different destinations. For example: JMS -> JSON, HTTP -> JMS or funneling FTP -> JMS, HTTP
-> JMS, JSON -> JMS" then as long as you have an api call that will return to you a java object within the
above "call" methods, then sure. – D-Klotz Feb 25 '14 at 22:00
Looking at the problem, you need to integrate your application with 10+ different
webservices.While making all the calls asynchronous. This can be done easily with Apache
Camel. It is a prominent framework for enterprise integration and also supports async
processing. You can use its CXF component for calling webservices and its routing engine for
invocation and processing results. Look at the following page regarding camel's async routing
capability. They have also provided a complete example invoking webservices async using
CXF, it available at its maven repo. Also see the following page for more details.
You can ask your jax-ws implementation to generate asynchronous bindings for the web
service.
1. As discussed in Asynchronous web services calls with JAX-WS: Use wsimport support for
asynchrony or roll my own? , jax-ws will generate well-tested (and possibly fancier) code
for you, you need not instantiate the ExecutorService yourself. So less work for you! (but
also less control over the threading implementation details)
2. The generated bindings include a method where you specify a callback handler, which
may suit your needs better than synchronously get() ting all response lists on the thread
calling retrieveAllLists() . It allows for per-service-call error handling and will process the
results in parallel, which is nice if processing is non-trivial.
An example for Metro can be found on the Metro site. Note the contents of the custom
bindings file custom-client.xml :
<bindings ...>
<bindings node="wsdl:definitions">
<enableAsyncMapping>true</enableAsyncMapping>
</bindings>
</bindings>
When you specify this bindings file to wsimport , it'll generate a client which returns an object
that implements javax.xml.ws.Response<T> . Response extends the Future interface that others
also suggest you use when rolling your own implementation.
So, unsurprisingly, if you go without the callbacks, the code will look similar to the other
answers:
https://stackoverflow.com/questions/21670451/how-to-send-multiple-asynchronous-requests-to-different-web-services 6/8
7/10/2017 java - How to send multiple asynchronous requests to different web services? - Stack Overflow
Response<List<StudentsResults>> students2 = ws2.getStudents();
Response<List<StudentsResults>> students3 = ws3.getStudents();
Page Saved!
Response<List<DoctorsResults>> doctors1 = ws4.getDoctors();
Response<List<DoctorsResults>> doctors2 = ws5.getDoctors();
Response<List<DoctorsResults>> doctors3 = ws6.getDoctors();
Add Tags
doctorsResults.addAll(doctors1.get());
doctorsResults.addAll(doctors2.get());
doctorsResults.addAll(doctors3.get());
patientsResults.addAll(patients1.get());
patientsResults.addAll(patients2.get());
patientsResults.addAll(patients3.get());
}
...
// await completion
for( Future<?> response: responses ) {
response.get();
}
Note that the studentResults collection needs to be thread safe now, since results will get
added concurrently!
thanks, how about if I use apache camel ? – J888 Feb 25 '14 at 21:02
Apache Camel is an integration framework. You'd have to give a bit more background information if you want
to know if it'd be worth your trouble to learn and use it. Like: * How often do the sources for your data
change? Is it these services and will it still be these services next year, or do the sources come and go? *
What are you writing? a client? Or a service that combines data from multiple services? Or a service bus? *
Are there more steps in the chain after you have collected this information? Do you need to export to excel?
Write to database? Print on the user's screen? Save to file? – flup Feb 25 '14 at 21:21
Or in short: Depends on how much more integration you are doing. If this is all, I'd not bother with Camel. If
there's more, Camel may well be a very interesting option. – flup Feb 25 '14 at 21:29
You might consider the following paradigm in which you create work (serially), but the actual
work is done in parallel. One way to do this is to: 1) have your "main" create a queue of work
items; 2) create a "doWork" object that queries the queue for work to do; 3) have "main" start
some number of "doWork" threads (can be same number as number of different services, or a
smaller number); have the "doWork" objects put add their results to an object list (whatever
construct works Vector, list...).
https://stackoverflow.com/questions/21670451/how-to-send-multiple-asynchronous-requests-to-different-web-services 7/8
7/10/2017 java - How to send multiple asynchronous requests to different web services? - Stack Overflow
Each "doWork" object would mark their queue item complete, put all results in the passed
container and check for new work (if no more on the queue, it would sleep and try again). Page Saved!
Of course you will want to see how well you can construct your class model. If each of the
Add Tags
webservices is quite different for parsing, then you may want to create an Interface that each
of your "retrieveinfo" classes promises to implement.
thanks, would you give me an example of that? – J888 Feb 25 '14 at 21:04
1. JMS : quality of service and management, e.g. redelivery attempt, dead message queue,
load management, scalability, clustering, monitoring, etc.
2. Simply using the Observer pattern for this. For more details OODesign and How to solve
produce and consumer follow this Kodelog**
How does JMS make this asynchronous? – user1907906 Feb 10 '14 at 9:49
Your each method goes as one JMS component – Kumar Feb 10 '14 at 10:02
@LutzHorn whats your suggestion, Thanks @ Kumar – J888 Feb 10 '14 at 13:43
Regarding the JMS , just hit the JMS Component and do your other stuff which is not dependent on the
response and in the JMS after completing the response store that in the database with transaction id. So,
independently you run all your services and when you get all your response back(Some kind of Observer
pattern but more reliable when using with JMS should be used to collate all your response) and do the rest of
the stuff. – Kumar Feb 11 '14 at 5:27
@Kumar thanks whats your idea about Hussain Pirosha's answer – J888 Feb 19 '14 at 21:20
https://stackoverflow.com/questions/21670451/how-to-send-multiple-asynchronous-requests-to-different-web-services 8/8