Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

EJB 3.1 will have improved timer services, as well as application lifecycle hooks that remove the need to use servlets to start tasks without user interaction.

This may answer the question title, but for the "real" question concerning a legacy application (written more than 6 months ago ;)) running on websphere I'd recommend to go with the start-up servlet and the EJB timer service.

Timer Service in J2EE 1.4 (EJB 2.1)

For EJB 3.0 (and 3.1 as soon as available), there are some nice annotations ;)

I'd not introduce another library unless you REALLY need it. The timer service should suffice for performing an arbitrary job on a daily basis.

HTH,
Martin

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 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top