English 中文(简体)
Is it possible to run a cron job in a web application?
原标题:

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)

最佳回答

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

问题回答

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

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

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签