介绍
1 | 通常情况下,Spring Quartz定时在一台服务器的情况下配置比较简单,这里就不做多余的阐述, |
Spring Quartz版本
1 | <dependency> |
Quartz官网下载地址
不同版本要引入的pom文件不一样,下载下来的dbtables的文件也不一样。
导入初始化SQL文件
1 | 不同版本的SQL文件的内容和位置不一样,找一个符合自己数据库版本的文件, |
配置quartz.properties
1 | # Default Properties file for use by StdSchedulerFactory |
配置定时任务
配置job
1
2提供了两种方式来配置job,分别是:MethodInvokingJobDetailFactoryBean和JobDetailFactoryBean,
JobDetailFactoryBean是序列化的,集群中必须使用这个。配置调度使用的触发器
1
2SimpleTriggerFactoryBean和CronTriggerFactoryBean,第一种是自动间隔一定时间触发定时任务,第二个种
是在特定的时间点触发请示任务。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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop-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"
default-lazy-init="true">
<description>hualife applicationContext.xml</description>
<!-- 属性配置文件 -->
<bean id="propertyConfigeurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<!-- 数据库配置文件 -->
<value>classpath:database.properties</value>
<!-- Hibernate配置文件 -->
<value>classpath:hibernate.properties</value>
</list>
</property>
</bean>
<!-- 启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能 -->
<context:component-scan base-package="com.tohours.hualife" />
<jee:jndi-lookup jndi-name="jdbc/hualife" id="dataSource"></jee:jndi-lookup>
<!-- Hibernate配置 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">
${hibernate.show_sql}
</prop>
<prop key="hibernate.format_sql">
${hibernate.format_sql}
</prop>
</props>
</property>
<property name="packagesToScan" value="com.tohours.hualife" />
</bean>
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<!-- spring quartz 定时 -->
<import resource="applicationContext-schedule.xml"/>
</beans>applicationContext-schedule.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop-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"
default-lazy-init="true">
<!-- 定义要注入的bean,注意不是interface -->
<bean id="messageService" class="com.tohours.hualife.service.impl.MessageServiceImpl"></bean>
<bean id="configService" class="com.tohours.hualife.service.impl.ConfigServiceImpl"></bean>
<bean id="wxService" class="com.tohours.hualife.service.impl.WxServiceImpl"></bean>
<!-- 消息定时作业类 -->
<bean id="messageJob"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.tohours.hualife.schedule.MessageJob" />
</bean>
<!-- 消息定时的触发方式 -->
<bean id="messageSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="messageJob" />
<property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->
<property name="repeatInterval" value="300000" /><!-- 每5分钟调度一次 -->
</bean>
<!-- token作业类 -->
<bean id="tokenJob"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.tohours.hualife.schedule.TokenJob" />
</bean>
<!-- token的触发方式 -->
<bean id="tokenSimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="tokenJob" />
<property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->
<property name="repeatInterval" value="3600000" /><!-- 每1小时调度一次 -->
</bean>
<!-- 配置调度工厂 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 定时任务方法中为了为了service层 -->
<property name="schedulerContextAsMap">
<map>
<!-- spring 管理的service需要放到这里,才能够注入成功 -->
<description>schedulerContextAsMap</description>
<entry key="messageService" value-ref="messageService" />
<entry key="configService" value-ref="configService" />
<entry key="wxService" value-ref="wxService"></entry>
</map>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<!-- 加载quartz的配置信息 -->
<property name="configLocation" value="classpath:quartz.properties" />
<!-- 系统启动后延迟5秒执行定时 -->
<property name="startupDelay" value="5" />
<property name="triggers">
<list>
<ref bean="messageSimpleTrigger" />
<ref bean="tokenSimpleTrigger" />
</list>
</property>
</bean>
</beans>目录层级
相关类
- Token定时任务类
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
49package com.tohours.hualife.schedule;
import java.net.InetAddress;
import java.net.UnknownHostException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerContext;
import org.quartz.SchedulerException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.tohours.hualife.service.WxService;
public class TokenJob extends QuartzJobBean{
private WxService wxService;
private Logger log = Logger.getLogger(MessageJob.class);
public void doJob() throws UnknownHostException{
String ip = InetAddress.getLocalHost().getHostAddress();
log.info("开始执行AccessToken定时任务,"+ip);
String accessToken = this.wxService.reloadAccessToken();
this.wxService.reloadJsapiTicket(accessToken);
}
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
try {
//获取JobExecutionContext中的service对象
SchedulerContext skedCtx = context.getScheduler().getContext();
//获取SchedulerContext中的service
//这里的service就是通过配置文件 配置的
this.wxService = (WxService)skedCtx.get("wxService");
doJob();
} catch (SchedulerException | UnknownHostException e) {
e.printStackTrace();
}
}
}
测试
1 | 为了测试集群,我这边开了三个tomcat做测试,分别启动三个服务,看console打印的log日志。 |
附件
*************感谢您的阅读*************