方式1:添加注解
在定时类上添加注解
@Component
@EnableScheduling
在定时事件上添加注解
@Scheduled(fixedRate = 1000 * 30) 或 @Scheduled(cron="0/5 * * * * *")
完整实例
在定时类上添加注解
@Component
@EnableScheduling
在定时事件上添加注解
@Scheduled(fixedRate = 1000 * 30) 或 @Scheduled(cron="0/5 * * * * *")
完整实例
@Component
@Configurable
@EnableScheduling
public class ScheduledTasks {
@Scheduled(fixedRate = 1000 * 30)
public void printCurrentTime() {
System.out.println("current"+dateFormat().format(new Date()));
}
@Scheduled(cron="0/5 * * * * *")
public void printCurrentByCron() {
System.out.println("corn" + dateFormat().format(new Date()));
}
private SimpleDateFormat dateFormat() {
return new SimpleDateFormat("HH:mm:ss");
}
}
方式2:配置
第一步:添加依赖
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency> <dependency><!-- 该依赖必加,里面有sping对schedule的支持 --> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency>
第二步:定义配置文件 quartz.xml,名字随意,暂定:spring-mvc(启动时需要注入,见第6步)
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <aop:config proxy-target-class="true"/> <context:annotation-config/> <!-- 利用import引入定时器的文件 --> <import resource="spring-quartz.xml"/> </beans>
第四部:添加定时器文件spring-quartz.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法--> <bean id="taskJob" class="com.example.quartz.test.TestTask"/> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="group" value="job_work"/> <property name="name" value="job_work_name"/> <!--false表示等上一个任务执行完后再开启新的任务--> <property name="concurrent" value="false"/> <property name="targetObject"> <ref bean="taskJob"/> </property> <property name="targetMethod"> <value>run</value> </property> </bean> <!-- 调度触发器 --> <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="name" value="work_default_name"/> <property name="group" value="work_default"/> <property name="jobDetail"> <ref bean="jobDetail" /> </property> <property name="cronExpression"> <value>0/10 * * * * ?</value> </property> </bean> <!-- 调度工厂 --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="myTrigger"/> </list> </property> </bean> </beans>
第五步:创建定时器需要的类 TestTaskpackage com.example.quartz.test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestTask { /** 日志对象 */ private static final Logger LOG = LoggerFactory.getLogger(TestTask.class); public void run() { if (LOG.isInfoEnabled()) { LOG.info("测试任务线程开始执行"); } System.out.println("quarty run ....."); } }
第6步:启动时扫描quartz文件主要是这个注解 @ImportResource(locations={"classpath:spring-mvc.xml"}),这样springboot就可以扫描到我们的配置文件了
package com.example; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; import org.springframework.jms.core.JmsTemplate; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootApplication @ImportResource(locations={"classpath:spring-mvc.xml"}) @EnableScheduling public class DemoApplication{ @Autowired private JmsTemplate jmsTemplate; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }