This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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..
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.
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.