24

In a java web application (servlets/spring mvc), using tomcat, is it possible to run a cron job type service?

e.g. every 15 minutes, purge the log database.

Can you do this in a way that is container independent, or it has to be run using tomcat or some other container?

Please specify if the method is guaranteed to run at a specific time or one that runs every 15 minutes, but may be reset etc. if the application recycles (that's how it is in .net if you use timers)

1
  • 2
    Not sure why this was downvoted? +1 Commented Jan 11, 2010 at 1:11

4 Answers 4

12

As documented in Chapter 23. Scheduling and Thread Pooling, Spring has scheduling support through integration classes for the Timer and the Quartz Scheduler (http://www.quartz-scheduler.org/). For simple needs, I'd recommend to go with the JDK Timer.

Note that Java schedulers are usually used to trigger Java business oriented jobs. For sysadmin tasks (like the example you gave), you should really prefer cron and traditional admin tools (bash, etc).

Sign up to request clarification or add additional context in comments.

Comments

2

If you're using Spring, you can use the built-in Quartz or Timer hooks. See http://static.springsource.org/spring/docs/2.5.x/reference/scheduling.html

Comments

0

It will be container-specific. You can do it in Java with Quartz or just using Java's scheduling concurrent utils (ScheduledExecutorService) or as an OS-level cron job.

Every 15 minutes seems extreme. Generally I'd also advise you only to truncate/delete log files that are no longer being written to (and they're generally rolled over overnight).

1 Comment

cletus, the log file was just an example, but point taken. Would the ScheduledExecutorService run at a fixed/gauranteed interval, or it depends on app recycling etc.?
0

Jobs are batch oriented. Either by manual trigger or cron-style (as you seem to want).

Still I don't get your relation between webapp and cron-style job? The only webapp use-case I could think of is, that you want to have a HTTP endpoint to trigger a job (but this opposes your statement about being 'cron-style').

Generally use a dedicated framework, which solves the problem-area 'batch-jobs'. I can recommend quartz.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.