spring定时器quartz源码demo配置文件版

基于spring quartz实现定时器功能,写死在配置文件中

准备

使用的是quartz-2.2.2.jar,理论可以使用1.8以上的quartz

quartz在线解析 左侧连接 教学了cron表达式基础

1
2
3
4
5
6
7
常用表达式例子
  (00/20 * * * * ? 表示每20秒 调整任务
  (10 0 2 1 * ? 表示在每月的1日的凌晨2点调整任务
  (20 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业
  (30 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作
  (40 0 10,14,16 * * ? 每天上午10点,下午2点,4
  (50 0/30 9-17 * * ? 朝九晚五工作时间内每半小时

applicationContext.xml

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


<!-- 配置事务切面 -->
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* com.cter.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config>

<!-- 自动加载package下面指定的bean -->
<context:component-scan base-package="com.cter">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />

<!-- 定义调用对象和调用对象的方法 -->
<bean id="startWorkJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject" ref="startWorkJob" />
<!-- 调用类中的方法 -->
<property name="targetMethod" value="startWork" />
<!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
<property name="concurrent" value="false"/>
</bean>

<!-- quartz-2.x的配置 -->
<!-- 定义触发时间 -->
<bean id="startWorkJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="startWorkJobDetail" />
</property>
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
</bean>

<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序 -->
<bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- <ref bean="startWorkJobTrigger" /> -->
</list>
</property>
</bean>

</beans>

StartWorkJob.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.cter.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Component;

/**
* 测试定时器
* @author op1768
*/
//使用 @Component注入指定的bean名称
@Component("startWorkJob")
public class StartWorkJob {

//配置文件中的加载的方法,可以在方法中写需要的定时任务
public void startWork(){
Date date=new Date();
String dateStr=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
System.out.println(dateStr+"\t我是定时器。我启动了");
}
}

动态加载 quartz 可以参考

https://github.com/xkcoding/spring-boot-demo/tree/master/spring-boot-demo-task-quartz 是springboot-demo系列

0%