• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running a thread in background in servlet

 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67748
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly do you expect this thread to do when the event happens?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Kousik Majumder
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does it need to be a Servlet? What's wrong with a simple Timer & TimerTask?
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic