I need to create a persistent Java based application that will run at set intervals. If this was strictly running under Linux I could create a CRON job but it needs to run under Windows as well. Obviously I could use some sort of service wrapper but what about using a Servlet that simply never handles any GET/POST requests? The advantage in my mind is that both Windows and Linux has Servlet containers like Tomcat (At least in my environment). One code base that works in both environments and Tomcat 开发者_开发问答itself will ensure that the application runs persistently like a service.
Is this a good use of a Servlet or am I straying outside of it's intended use?
No, don't use servlets. That's not what they were designed for.
What you want is Quartz. It's a library for executing scheduled jobs. It also includes classes for integrating the scheduler with a servlet environment - once you configure it, the scheduler starts when the application is loaded and stops when it's unloaded.
The portability issue is solved almost automatically by using Java... I'd rather not use a servlet if there's no web interface.
Take a look to Quartz Scheduler framework which seems to be a perfect fit for your problem.
To problem of running Quartz as a service under Windows can be addressed by using one of the many possible service wrappers; e.g.
- http://wrapper.tanukisoftware.org/doc/english/introduction.html
- http://commons.apache.org/daemon/
- http://jslwin.sourceforge.net/
if your application not requires container- tomcat, jsp that just use Cron trigger in your application that runs it at interval
see this example
精彩评论