step1 添加依赖
< dependency>
< groupId> org. quartz- scheduler< / groupId>
< artifactId> quartz< / artifactId>
< version> 2.3 .2 < / version>
< exclusions>
< exclusion>
< artifactId> slf4j- api< / artifactId>
< groupId> org. slf4j< / groupId>
< / exclusion>
< / exclusions>
< / dependency>
< dependency>
< groupId> org. quartz- scheduler< / groupId>
< artifactId> quartz- jobs< / artifactId>
< version> 2.3 .2 < / version>
< / dependency>
step2 配置文件
< ? 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: context= "http://www.springframework.org/schema/context"
xmlns: mvc= "http://www.springframework.org/schema/mvc" xmlns: task= "http://www.springframework.org/schema/task"
xsi: schemaLocation= "http: / / www. springframework. org/ schema/ mvc http: / / www. springframework. org/ schema/ mvc/ spring- mvc- 4.3 . xsd
http: / / www. springframework. org/ schema/ beans http: / / www. springframework. org/ schema/ beans/ spring- beans- 4.3 . xsd
http: / / www. springframework. org/ schema/ context http: / / www. springframework. org/ schema/ context/ spring- context- 4.3 . xsd
http: / / www. springframework. org/ schema/ task http: / / www. springframework. org/ schema/ task/ spring- task- 4.3 . xsd">
< bean id= "jobFactory" class = "com.eastwit.task.TaskFactory" / >
< ! -- 读取数据库相关文件配置 -- >
< context: property- placeholder location= "classpath:custom.properties"
ignore- unresolvable= "true" / >
< bean id= "taskExecutor"
class = "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >
< ! -- 核心线程数 -- >
< property name= "corePoolSize" value= "10" / >
< ! -- 最大线程数 -- >
< property name= "maxPoolSize" value= "100" / >
< ! -- 队列最大长度 -- >
< property name= "queueCapacity" value= "15000" / >
< ! -- 线程池维护线程所允许的空闲时间,默认为60 s -- >
< property name= "keepAliveSeconds" value= "180" / >
< / bean>
< task: executor id= "myexecutor" pool- size= "50" / >
< ! -- 任务 -- >
< bean id= "SyncDataTask" class = "com.eastwit.task.SyncDataTask" / >
< ! -- 日志同步 延迟 60 s执行 60 min执行一次 -- >
< bean id= "SyncDataTiming" class = "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >
< property name= "targetObject" ref= "SyncDataTask" / >
< property name= "targetMethod" value= "syncData" / >
< property name= "concurrent" value= "false" / >
< / bean>
< bean id= "SyncDataTimingTrigger" class = "org.springframework.scheduling.quartz.SimpleTriggerFactoryBean" >
< property name= "jobDetail" ref= "SyncDataTiming" / >
< ! -- 延迟60 s启动 -- >
< property name= "startDelay" value= "60000" / >
< ! -- 每30 min查询一次-- >
< property name= "repeatInterval" value= "${changzhou_syncTime}" / >
< / bean>
< ! -- 设置调度 -- >
< bean class = "org.springframework.scheduling.quartz.SchedulerFactoryBean" >
< property name= "jobFactory" ref= "jobFactory" / >
< property name= "triggers" >
< list>
< ref bean= "SyncDataTimingTrigger" > < / ref>
< / list>
< / property>
< property name= "taskExecutor" ref= "taskExecutor" / >
< / bean>
< / beans>
step3 spring管理Been
package com. eastwit. task;
import org. quartz. spi. TriggerFiredBundle;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. beans. factory. config. AutowireCapableBeanFactory;
import org. springframework. scheduling. quartz. AdaptableJobFactory;
public class TaskFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
@Override
protected Object createJobInstance ( TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super . createJobInstance ( bundle) ;
capableBeanFactory. autowireBean ( jobInstance) ;
return jobInstance;
}
}