Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are Servlets good for the following task

 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Debal Guha
Greenhorn
Posts: 9
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic