Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run a cron job in a web application?

Tags:

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)

like image 296
mrblah Avatar asked Jan 11 '10 00:01

mrblah


People also ask

What is cron job in website?

Cron is a utility program that lets users input commands for scheduling tasks repeatedly at a specific time. Tasks scheduled in cron are called cron jobs. Users can determine what kind of task they want to automate and when it should be executed.

When should you not use cron jobs?

A few problems with cron: Smallest resolution is 1 minute—If a task needs to run every 30 seconds, you can't do it with cron. Error handling—If a job fails, what should happen? Solutions have been built to solve this single problem. Developers love adding more band-aids rather than admitting there is a better way.

What is cron in web development?

Cron is a daemon program in the UNIX family of operating systems. It can perform tasks on the server at preset intervals. Thanks to Cron, a web programmer can simplify his work and automate almost any task: from standard backups to more complex ones.

Where do Cronjobs run from?

Cron jobs are typically located in the spool directories. They are stored in tables called crontabs. You can find them in /var/spool/cron/crontabs. The tables contain the cron jobs for all users, except the root user.


1 Answers

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

like image 59
Pascal Thivent Avatar answered Dec 10 '22 20:12

Pascal Thivent