Spring 任务调度(部分源码)
Quartz的三个核心概念:任务,触发器,调度器
调度器:
org.springframework.scheduling.quartz.SchedulerFactoryBean
Spring提供的FactoryBean,能够感知Spring容器的生命周期,完成自动
启动和关闭的操作。
功能:
1、以更具bean风格的方式为Scheduler提供配置信息
2、让Scheduler和Spring容器的生命周期建立关联,相生相息
3、通过属性配置部分或全部代替Quartz自身的配置文件
<bean autowire="no"name="code">public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBean, BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean, Lifecycle {public static final String PROP_THREAD_COUNT = "org.quartz.threadPool.threadCount";public static final int DEFAULT_THREAD_COUNT = 10;......public abstract class SchedulerAccessor implements ResourceLoaderAware {protected final Log logger = LogFactory.getLog(getClass());private boolean overwriteExistingJobs = false;private String[] jobSchedulingDataLocations;private List jobDetails;private Map calendars;private List triggers;......
?
任务:
<!--任务-->
<bean id="testManager" value="false"/><!--true表示任务没有状态,false表示任务有状态,有状态的任务不能并发执行,无状态的任务可并发执行--></bean>?
org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBeanpublic class MethodInvokingJobDetailFactoryBean extends ArgumentConvertingMethodInvoker implements FactoryBean, BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, InitializingBean {private String name;private String group = Scheduler.DEFAULT_GROUP;private boolean concurrent = true;private String targetBeanName;private String[] jobListenerNames;private String beanName;private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();private BeanFactory beanFactory;private JobDetail jobDetail;......public class ArgumentConvertingMethodInvoker extends MethodInvoker {private TypeConverter typeConverter;private boolean useDefaultConverter = true;......public class MethodInvoker {private Class targetClass;private Object targetObject;private String targetMethod;private String staticMethod;private Object[] arguments = new Object[0];/** The method we will call */private Method methodObject;......
?
触发器:
<bean id="testTrigger" /> </property><!-- 60*10*1000 十分钟 延迟多少时间开始触发,单位为毫秒 --> <property name="startDelay"> <value>600000</value> </property> <property name="repeatInterval"> <value>600000</value> </property></bean>?
org.springframework.scheduling.quartz.SimpleTriggerBeanpublic class SimpleTriggerBean extends SimpleTrigger implements JobDetailAwareTrigger, BeanNameAware, InitializingBean {/** Constants for the SimpleTrigger class */private static final Constants constants = new Constants(SimpleTrigger.class);private long startDelay = 0;private JobDetail jobDetail;private String beanName;......
?
或者
<bean id="testTrigger" /> </property> <property name="cronExpression" value="0 11 11 11 11 ?"/></bean>?
public class CronTriggerBean extends CronTrigger implements JobDetailAwareTrigger, BeanNameAware, InitializingBean {/** Constants for the CronTrigger class */private static final Constants constants = new Constants(CronTrigger.class);private JobDetail jobDetail;......