views:

857

answers:

7

Hello, my employer just asked me to run a timed batch process in a J2EE websphere application they have running. It's supposed to run a certain class at 1130 pm everyday.

I'm not very familiar with J2EE nor websphere server (or tomcat, in the develpment 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!

+3  A: 

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.

Cheekysoft
A: 

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.

A: 

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.

feniix
A: 

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.

Frans
Yeah, and you regularly add it to every log file that lists http access... Really, verysecret...
Martin Klinke
It runs on my own server and I am the only one who sees the logfiles. It's just to make sure that other will not start my batch. The point I wanted to make here is that you can use crontab to call a servlet. Secure it the way you think is necessary. Works perfectly for me.
Frans
+1  A: 

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

Martin Klinke
I tried it and found no way to create the timer, because the servlet startup method (init) does not have access to a context which provides the timer service API. I have posted this as a new question in http://stackoverflow.com/questions/1024081/
mjustin
+2  A: 

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