复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45第一步:pom文件添加: <!-- Quartz定时任务--> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency> 第二步:新建配置文件spring-quartz.xml: <?xml version="1.0" encoding="UTF-8"?> <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.xsd"> <!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法--> <bean id="taskJob" class="com.TestTask"> </bean> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!--false表示等上一个任务执行完后再开启新的任务,禁止并发--> <property name="concurrent" value="false"/> <property name="targetObject"> <ref bean="taskJob"/> <!-- 对应TestTask类 --> </property> <property name="targetMethod"> <value>task</value> <!-- 对应TestTask中的task方法 --> </property> </bean> <!-- 调度触发器 --> <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="jobDetail"/> </property> <property name="cronExpression" value="0 56 17 ? * * *"/> </bean> <!-- 调度工厂 --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="myTrigger"/> </list> </property> </bean> </beans>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13第三步:注入配置文件: @Configuration @ImportResource(locations = { "classpath:spring-quartz.xml" }) public class QuartzConfig { } 第四步:新建任务类: public class TestTask { private static final Logger logger = LoggerFactory.getLogger(TestTask.class); public void task(){ logger.info("=======TestTask========"); } }
亲测能正常执行
最后
以上就是单薄哑铃最近收集整理的关于SpringBoot集成Quartz执行定时任务的全部内容,更多相关SpringBoot集成Quartz执行定时任务内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复