您的位置:首页 > 编程语言 > Java开发

quartz与spring的整合使用

2017-08-21 00:00 330 查看
maven依赖:

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.0.2</version>
</dependency>

Spring配置文件applicationContext-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-3.0.xsd"> 
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean name="quartz_scheduler" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="configLocation" value="classpath:quartz.properties" />
<property name="triggers">
<list>
<ref bean="updateAccountTrigger" />
<!-- other Trigger -->
</list>
</property>
</bean>

<!-- 定义调用对象和调用对象的方法 -->
<bean id="updateAccountTask"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="sysTimerServiceImpl" />
<property name="targetMethod" value="updateAllAccounts" />
<property name="concurrent" value="false" />
</bean>
<!-- 9-19点每隔一个小时触发 -->
<bean id="updateAccountTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="updateAccountTask" />
<property name="cronExpression" value="0 0 9-19 * * ?" />
</bean>

</beans>

quartz.properties:

#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName: quartz_scheduler_jobs
org.quartz.scheduler.instanceId: AUTO
org.quartz.scheduler.skipUpdateCheck: true
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 5
org.quartz.threadPool.threadPriority: 5

定时任务的内容:

// 调用对象
@Service("sysTimerServiceImpl")
public class SysTimerServiceImpl implements SysTimerService {

// 调用方法
[@Override](https://my.oschina.net/u/1162528)
public void updateAllAccounts() {
// ...
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: