Home Spring集成Quartz
Post
Cancel

Spring集成Quartz

依赖

1
2
3
4
5
6
<!-- 定时任务 -->
<dependency>
 <groupId>org.quartz-scheduler</groupId>
 <artifactId>quartz</artifactId>
 <version>2.2.1</version>
</dependency>

配置文件

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
<?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">

 <!-- 需要执行的类 -->
 <bean id="testJob" class="com.sky.test.TestImpl" />

 <!-- 配置job -->
 <bean id="testJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="testJob" />
  <!-- 定时的方法名 -->
  <property name="targetMethod" value="test" />
  <!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
  <property name="concurrent" value="false" />
 </bean>
    
 <!-- CronTrigger表达式触发器 -->
 <bean id="testJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
  <property name="jobDetail" ref="testJobDetail" />
  <!-- 1000ms -->
  <property name="startDelay" value="1000" />
  <!-- 设置定时的时间 -->
  <property name="cronExpression" value="0 0 4 * * ?" />
 </bean>
    
 <!--配置调度工厂 -->
 <bean id="schedulerFactoryNews" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
   <list>
    <ref bean="testJobTrigger" />
   </list>
  </property>
 </bean>

</beans>
This post is licensed under CC BY 4.0 by the author.