这篇文章将为大家详细讲解有关quartz中怎么整合spring,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
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() {
// ...
}
}
关于quartz中怎么整合spring就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。