Spring's own scheduled task function, based on annotations and xml configuration
1. Spring configuration file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<task:annotation-driven /> <!-- Timer switch-->
<bean id="myTaskXml" class="com.spring.task.MyTaskXml"></bean>
<task:scheduled-tasks>
<!--
This means that it is executed every five seconds
-->
<task:scheduled ref="myTaskXml" method="show" cron="*/5 * * * * ?" />
<task:scheduled ref="myTaskXml" method="print" cron="*/10 * * * * ?"/>
</task:scheduled-tasks>
<!-- Automatically scanned package name-->
<context:component-scan base-package="com.spring.task" />
</beans>
2. XML-based timer task
package com.spring.task;
/**
* xml based timer
* @author hj
*/
public class MyTaskXml {
public void show(){
System.out.println("XMl:is show run");
}
public void print(){
System.out.println("XMl:print run");
}
}
3. Annotation-based timer tasks
package com.spring.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Annotation-based timer
* @author hj
*/
@Component
public class MyTaskAnnotation {
/**
* Timing calculation. Execute once a day at 01:00 am
*/
@Scheduled(cron = "0 0 1 * * *")
public void show(){
System.out.println("Annotation:is show run");
}
/**
* Heartbeat update. Executed once at startup and every 2 seconds thereafter
*/
@Scheduled(fixedRate = 1000*2)
public void print(){
System.out.println("Annotation:print run");
}
4. Test
package com.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mvc.xml");
}
}
5. Other implementations - quartz
<!-- Define the calling object and the method of calling the object -->
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- Called class -->
<property name="targetObject">
<ref bean="finance.generateDataService"/>
</property>
<!-- call a method in the class -->
<property name="targetMethod">
<value>generateData</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtask"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>0 0 8 1 * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime"/>
</list>
</property>
</bean>