Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to make URLConnection request run in parallel?

843841May 10 2003 — edited May 12 2003
Hi all,

Thank in advance.

I am writing a servlet, it will trigger two other different servlets using the URLConnection. Each of these two servlets will immediately get the PrintWriter from the http response object and send some words to the caller. They will flush the output and close the writer. After that they will continue some other simple processing.

At first, we assume the two called servlets can run in parallel after replying the caller servlet. However, the fact is not. They are running in serial. That is:

1) Servlet A -> URLConnection -> Servlet B -> response -> Servlet A
2) Servlet A -> URLConnection -> Servlet C -> response -> Servlet A

We assume servlets B and C can continue the process concurrently after responsing A. In each step of the servlet, we add some display statements to indicate the processing. However, we always find that Servlet C will run after Servlet B completing all the tasks, even, we let B sleep for 1 min.

My question is: how to make Servlet B/C running in parallel after replying servlet A??

The sample code is:

Servlet A triggers servlet B/C by:
:
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
if (connection instanceof HttpURLConnection) {
HttpURLConnection httpConnection = (HttpURLConnection)connection;
httpConnection.connect();
InputStream inputStream = httpConnection.getInputStream();
byte[] buffer = new byte[256];
while (inputStream.read(buffer) != -1) {
String s = new String(buffer);
System.out.println("read string =" + s);
}
inputStream.close();
}//end-if
:

Servlet B/C will reply Servlet A by:
:
response.setContentType ("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>" + this.programID + " reply: "
+ new Timestamp(System.currentTimeMillis()) + "</body></html>");
out.flush();
out.close();
response.flushBuffer();
:
//do something else like sleep or etc

Please let us know if you have any idea. Many many thanks!
Sherlane Lam
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 9 2003
Added on May 10 2003
2 comments
512 views