aspose file tools*
The moose likes Servlets and the fly likes Running a thread in background in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Running a thread in background in servlet" Watch "Running a thread in background in servlet" New topic
Author

Running a thread in background in servlet

Kousik Majumder
Ranch Hand

Joined: Sep 30, 2007
Posts: 227
Hi all,

I have a requirement to post an alarm when some event happens.

I want to write a servlet which will be called at the time of application start up and will stay active as long as application runs.
A thread in that servlet will always monitor for that event due to which alarm will be generated.

I am actually trying to avoid to use thread in the servlet which may cause the application to run slow.

Is there any better way to do that.
Please suggest some code.

Thank you,
Kousik


Thanks in Advance,
Kousik
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60075
    
  65

What exactly do you expect this thread to do when the event happens?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Yes, a servlet can start a new Thread to accomplish some task.

However, you must avoid having the servlet request/response Thread slowed down or waiting for some event. That would be contrary to the whole servlet architecture and processing model.

Bill


Java Resources at www.wbrogden.com
Kousik Majumder
Ranch Hand

Joined: Sep 30, 2007
Posts: 227
Hi,

I just want the servlet to keep active as long as the application runs so that it can generate the alarm(i.e it will check db for a threshold crossing) and act accordingly.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Why does it need to be a Servlet? What's wrong with a simple Timer & TimerTask?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
I just want the servlet to keep active as long as the application runs


Assuming you really mean you want the request to stay open, that is contrary to the entire intent of servlets as any tutorial will tell you.

This type of job is usually accomplished with a servlet which starts the separate Thread (or even separate application) and then provides for monitoring it. BUT with multiple request/response cycles.

1. start job, return page saying job started - possibly with Javascript that can fire off a new request

2. each request looks at the status of the job and reports (NOT waiting)

3. finally a request determines that the job is finished and reports the results.

Bill
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Running a thread in background in servlet
 
Similar Threads
Scheduling Date
Application that runs in Background and monitors event
Identifying back button press
Swing and the "single thread roule"
Sound in Servlet