• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread from servlet ?

 
Jimm Norton
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jimm Norton
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not using Ajax in your view page?
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you set a Timeout on Web Service call as 10 seconds?

What Web Service API are you using?
 
Raghu Shank
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just set the timeout variable in web service. That should work for you

~ abhay

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic