Spring 石英调度

一、继承式Quartz定时器

1.创建一个继承自QuartzJobBean的类,并重写executeInternal方法
public class MyJob extends QuartzJobBean{
 
   private int timeout;
    @Override
    protected void executeInternal(JobExecutionContext arg0)
            throws JobExecutionException {
        System.out.println("现在时间为: " + (new Date()));
    }
    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
}

2.创建调度器JobDetailBean
<bean id="jobDetail"
    class="org.springframework.scheduling.quartz.JobDetailBean">
    <!--指定调度器的jobClass属性的值为QuartzJobBean的子类-->
    <property name="jobClass">
        <value>com.my.pojo.MyJob</value>
    </property>
    <!--设置开启时间-->
    <property name="jobDataAsMap">
        <map>
            <entry key="timeout" value="0"></entry>
        </map>
    </property>
</bean>

3.创建调度触发器CronTriggerBean
<bean id="trigger"
    class="org.springframework.scheduling.quartz.CronTriggerBean">
    <!--将调度器赋值给触发器的jobDetail属性-->
    <property name="jobDetail" ref="jobDetail"></property>
    <!--设置定时时间-->
    <property name="cronExpression">
        <value>0-59 * * * * ?</value>
    </property>
</bean>

4.创建触发器调度工厂SchedulerFactoryBean
<bean
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref local="trigger"/>
        </list>
    </property>
</bean>

####测试类
public static void main(String[] args) throws SchedulerException {
    ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:container.xml");
    JobDetail job = (JobDetail) ac.getBean("jobDetail");
    CronTriggerBean trigger = (CronTriggerBean) ac.getBean("trigger");
    Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    scheduler.scheduleJob(job, trigger);
    scheduler.start();
}



二、非继承式Quartz定时器

1.声明作业类MyJob
<bean id="job" class="com.my.pojo.MyJob"></bean>
public class MyJob {
    public void doJob(){
        System.out.println("现在时间为: " + (new Date()));
    }
}
此类可以是任意类型的类

2.配置作业类MethodInvokingJobDetailFactoryBean
<bean id="jobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <!--将之前声明的要运行的类赋值给此类的targetObject属性-->
    <property name="targetObject" ref="job"></property>
    <!--指定目标类中被管理的方法,对应属性名为targetMethod-->
    <property name="targetMethod" value="doJob"></property>
</bean>

3.配置触发器CronTriggerBean
<bean id="trigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
    <!--将作业类管理器交与触发器管理-->
    <property name="jobDetail" ref="jobDetail"></property>
    <!--设置触发器运行的时间-->
    <property name="cronExpression">
        <value>1-50 * * * * ?</value>
    </property>
</bean>

4.配置调度工厂SchedulerFactoryBean
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <!--将触发器交与调度工厂管理-->
    <property name="triggers">
        <list>
            <ref local="trigger"/>
        </list>
    </property>
</bean>

####测试类
public static void main(String[] args) throws SchedulerException {
    ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:container.xml");
    JobDetail job = (JobDetail) ac.getBean("jobDetail");
    CronTriggerBean trigger = (CronTriggerBean) ac.getBean("trigger");
    Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    scheduler.start();
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。