File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Thread from servlet ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread from servlet ?" Watch "Thread from servlet ?" New topic

Thread from servlet ?

Jimm Norton

Joined: Jul 18, 2011
Posts: 2
I have a servlet, that calls a web service, and gets data to populate the servlet response.
I also don't want to wait too long.
Can I just spawn a new thread and call the web service? If this is not recommended, what is a better way ?

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63838

How long are you talking? And if you were to spawn off a thread, what would you do with the data when the web service returns it?

[Asking smart questions] [About Bear] [Books by Bear]
Jimm Norton

Joined: Jul 18, 2011
Posts: 2
I may not have been clear.
http request ->
servlet ->
web service
servlet response

I don't want to wait more than 10 seconds or so, if the web service doesn't respond, I just want to send back a msg to client.
Hussein Baghdadi
clojure forum advocate

Joined: Nov 08, 2003
Posts: 3479

Why not using Ajax in your view page?
Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

I would not spawn the thread in the servlet itself

A better approach would be to spawn off a daemon thread before the servlet action. Have it just wait for work. In the servlet, you can send a "request" to your daemon to do whatever you want. Just create a small Message class with needed information, and call a method in the daemon to process it.

You will need a semaphore to block the servlet's thread until it gets a response from the daemon or times out.
Pazhanikanthan Periasamy

Joined: Jul 18, 2011
Posts: 9
Why dont you set a Timeout on Web Service call as 10 seconds?

What Web Service API are you using?
Raghu Shank

Joined: Jul 19, 2011
Posts: 3
The very purpose of application/web servers is to alleviate the problems of threads and related stuff from developers as the container takes care of them. If you start a thread on your own in a container environment and it goes to hung status then you have one less thread in the container. In a multi user environment this may lead to exhausting all the resources (threads). Hence it is not recommended to start the thread in containers.

With reference to your issue for waiting the ideal situation is to use ajax with timed intervals.
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1341

just set the timeout variable in web service. That should work for you

~ abhay

Oracle certified Java 8 Programmer I (1Z0-808), 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, CIW Certified Ecommerce specialist
I agree. Here's the link:
subject: Thread from servlet ?
It's not a secret anymore!