aspose file tools*
The moose likes Servlets and the fly likes Are Servlets good for the following task Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Are Servlets good for the following task" Watch "Are Servlets good for the following task" New topic
Author

Are Servlets good for the following task

Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
Hi,

One requirement that I have in one of my projects is that I need a process (thread) which will continuously query a DB table and look for a particular record. If and when it finds this record it needs to call a Spring Batch job.

Just wondering if a Servlet would work here. Basically the Servlet would keep doing the above as long as the container is running.

If Servlet is a good choice where would I put the above code. I am guessing doGet is not the best option because that only gets fired when a request comes in. What about the init() method with something like the below..



Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Nope, not a good fit. Servlets are intended to respond to a request made from a client, and to do so quickly.

It sounds as if a standalone program, initiated as a cron job is what might be more suitable.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
Thanks for the response. But I am just wondering why I need to go to the trouble of setting up a chron and invoking a standalone program when I already have a JVM up and running (web container).

Thansk,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Separation of concerns.

Cramming everything into the same bucket isn't always the best idea.

Regardless, whether you run something in the web app JVM or not, servlets are not the choice for long-running processes, or anything that isn't focused on handling a request with a response.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
If you're looking for simplicity, and you don't want to use cron, you can just run a simple java program with a thread or a thread pool which will do exactly what you need, query the database and call the batch job if needed.
Naveen Kumar Kumar
Greenhorn

Joined: Jul 28, 2010
Posts: 19
Agreed with Emanuel and Bear, for a continuously running job, it is always better to have a java program with a simple while(true) for continuous read to the DB. Here DB connection can be created once the service is started.

Below could be add-on reasons for not using webcontainer or servlet:

1: This requirement would not demand an entire Webcontainer to be used.
2: In case whenever a restart is required, simple java terminator program can be written to stop the service and restart by invoking through the java command.
3: This saves lot of cost in restarting Webcontainer, which otherwise, could be hosting some real-time web applications, leading to unnecessary system downtime.


Push the limits to push yourself..!!
Debal Guha
Greenhorn

Joined: Feb 21, 2013
Posts: 9

Since you are using Spring already, why don't you consider using Spring's task scheduler support. It is efficient and will reduce much of your boilerplate code. You an check it here.


You just need to know where to find it!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are Servlets good for the following task