wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread from servlet ?" Watch "Thread from servlet ?" New topic
Author

Thread from servlet ?

Jimm Norton
Greenhorn

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 ?

THanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

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] [Bear's FrontMan] [About Bear] [Books by Bear]
Jimm Norton
Greenhorn

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
Bartender

Joined: Nov 08, 2003
Posts: 3476

Why not using Ajax in your view page?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

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
Greenhorn

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
Greenhorn

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: 1071
    
    1

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

~ abhay


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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Thread from servlet ?
 
Similar Threads
Two servlets with same URL?
long running service call and tracking completion
servlet container and multi threading
sending files via web service
can I use web service for common servlet web application ?