Harald Schilly
Harald Schilly

Reputation: 1108

run periodic tasks on server in the background

What's the best/easiest way to run periodic tasks (like a daemon thread) on a tomcat/jetty server? How do I start the thread? Is there a simple mechanism or is this a bad idea at all?

Upvotes: 4

Views: 3724

Answers (5)

Nolte
Nolte

Reputation: 1097

If you want to use a cron job but don't have administrative access to the development system, you can do a user crontab by executing the command:

crontab -e

It uses vi by default on most systems, but you can change it to the editor of your choice via:

export EDITOR=/usr/local/bin/my_editor

Then, executing the crontab -e command will launch your crontab file in your editor. Upon saving, the changes will be committed back into the system's cron.

Upvotes: 0

Ry4an Brase
Ry4an Brase

Reputation: 78350

It's okay and effective to stash a java.util.Timer (or better yet ScheduledExecutor) instance in your ServeletContext. Create it in a Servlet's init() call and all your servlets can add TimerTasks to it.

Upvotes: 4

MatthieuP
MatthieuP

Reputation: 1126

If want to keep everything on java side, give a look to Quartz.
It handles failover and fine grained repartition of jobs, with the same flexibility of cron jobs.

Upvotes: 9

Eddie Parker
Eddie Parker

Reputation: 4888

I can't answer the tomcat/jetty stuff, but I've done similar things with Python based web apps.

I normally just run a separate app that does the periodic tasks needed. If interop is needed between the website and the app, that communication can happen through some sort of API (using something like XML-RPC/unix sockets/etc) or even just through the database layer, if that's adequate.

Hope that helps.

Upvotes: 0

Paul Dixon
Paul Dixon

Reputation: 301125

One general purpose way which works for many systems is simply to have a cron job which performs a periodic wget against your app.

Upvotes: 2

Related Questions