My employer just asked me to run a timed batch process in a Java EE WebSphere application they have running. It's supposed to run a certain class at 11:30 pm everyday.
I'm not very familiar with Java EE nor WebSphere server (or tomcat, in the development environment), and I've been digging around but all I've found is about the java timer class but not how to set it or invoke it.
It seems that editing the web.xml file is required as well.
Any help will be appreciated!
In your web.xml you can configure a servlet to load at startup.
Syntax:
<servlet servlet-name='hello' servlet-class='test.HelloWorld'>
<load-on-startup/>
</servlet>
Do this, then in the init method in the servlet you can set up a Timer / TimerTask to do whatever it is you need to do. TimerTasks are like Threads except you can schedule them when to run.
Quartz is part of the standard JBoss 4.2.x distribution.
And is a really good library, that without much work you can also define simple workflows.
There is support for scheduling included in WebSphere.
WAS v7.0 http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/topic/com.ibm.websphere.base.doc/info/aes/ae/welc6tech_sch.html
WAS v6.1 http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/topic/com.ibm.websphere.base.doc/info/aes/ae/welc6tech_sch.html
In WebSphere, you can use the Scheduler Service to trigger the execution of a method in a java class. The scheduler provides a calendar for scheduling the execution of jobs (similar to cron) or you could develop your own.
Here's a link to the page describing the scheduler in the WAS 6.1 documentation:
http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp
There is no support for scheduling in WebSphere.
If you are on unix you can use crontab to schedule a request to a page of your websphere application. I suppose on windows there is also a possibility to schedule a request to a page. In my crontab I schedule a request to a webpage each day at 8:45
45 8 * * * GET http://www.domain.com/myBatch?securitykey=verysecret
Now every morning the myBatch servlet is called and there I can do whatever needs to be done at that time. To avoid others calling this page and start the batch, I added the securitykey parameter.
You should look at the open-source Quartz library from OpenSymphony. Very easy to use and perfect for this kind of thing.
TimerTasks are best suited for running something in a short time in the future. But for a repeated execution in a large timeframe such as this, Quartz excels. You can even keep your list of upcoming tasks in persistent storage such as a file or database, so upcoming timed jobs are not lost if your application is restarted.
Also, there's a fantastic abstraction for Quartz in the Spring framework.