aspose file tools
The moose likes Servlets and the fly likes Thread from servlet ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply 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: 56231
    
  13

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?


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About 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: 3359

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

Joined: Aug 11, 2007
Posts: 4422
    
    2

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: 697

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

~ abhay


Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Thread from servlet ?
 
Similar Threads
sending files via web service
Two servlets with same URL?
can I use web service for common servlet web application ?
servlet container and multi threading
long running service call and tracking completion