Prior to the company has a special task scheduling framework, need to use a jar package to add a configuration and annotations can be used, there is a dedicated platform to maintain the running of the machine and monitor the status of execution and so on.
Now suddenly without the tool, but also write a timed task, what to do?
For non-Web applications, we can use quartz, which is simple and powerful.
For Java Web Applications, of course, you can also use Quartz (one blog describes the method: http://blog.csdn.net/sadfishsc/article/details/50808027), but there are more convenient tools, That's what spring comes with to support the Scheduled Tasks feature.
Spring's scheduled tasks in Spring-context, the simple configuration template is as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:task= "Http://www.springframework.org/schema/task" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/ Beans/spring-beans-3.2.xsd Http://www.springframework.org/schema/task Http://www.springframework.org/schema /task/spring-task.xsd "> <task:scheduler id=" Scheduler "pool-size="/> <task : Scheduled-tasks> <!--your task-- <task:scheduled ref= "Xxxtask" method= "execute" cron= "0 0 * * *?" /> </task:scheduled-tasks> <task:annotation-driven scheduler= "Scheduler"/></beans>
Where Task:scheduler specifies the scheduler to perform a timed task, using Org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler by default;
Task:annotation-driven allows the use of @async and @scheduled annotations;
A task is defined in Task:scheduler-tasks, where the execution cycle can use a cron expression, and you can specify how the delay or frequency is.
There is a conversion cron online tool very useful, recommend to everyone (note here may show 7 characters, remove the last * can be):
http://cron.qqe2.com/
Then there is a problem, usually our online environment is a clustered environment, there are more than one machine, and these scheduled tasks are usually only on a platform, how to control it?
Now think of two ways to share to everyone:
1. Using the Redis global cache
This is not a detailed introduction, you can refer to the following two articles:
http://blog.csdn.net/lihao21/article/details/49104695
http://blog.csdn.net/liubenlong007/article/details/53782934
2. By judging the way the file
By judging whether a file exists, to decide whether to perform the task (whether to load the spring configuration file for the task), refer to the code:
@Componentpublic class Xxxlistener implements Applicationcontextaware { //Prevent loading multiple private static final Atomicinteger Init_lock = new Atomicinteger (0); @Override public void Setapplicationcontext (ApplicationContext applicationcontext) throws Beansexception { if (Init_lock.incrementandget () > 1) { //class has loaded return; } Resource Resource = Applicationcontext.getresource ("classpath:< identification File >"); if (!resource.exists ()) { //file does not exist, do not start return; } Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext (applicationcontext); Context.setconfiglocations ("Classpath:spring/job.xml"); Context.refresh (); }}
Writing timed tasks in a Java Web project