maven依赖:spring
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.0.2</version> </dependency>
Spring配置文件applicationContext-quartz.xml:app
<?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
定时任务的内容:maven
// 调用对象 @Service("sysTimerServiceImpl") public class SysTimerServiceImpl implements SysTimerService { // 调用方法 [@Override](https://my.oschina.net/u/1162528) public void updateAllAccounts() { // ... } }