首页 > 其他 > 详细

Quartz 定时器任务调度

时间:2016-02-05 02:03:23      阅读:254      评论:0      收藏:0      [点我收藏+]


Job:是一个接口只有一个方法void execute(JobExecutionContext context),开发者实现该接口定义运行任务,JobExecutionContext类提供了调度上下文的各种信息。Job运行时的信息保存在 JobDataMap实例中


第一种,作业类继承自特定的基类:org.springframework.scheduling.quartz.QuartzJobBean。

java类代码

package com.ncs.hj;  

import java.util.Date;

import org.quartz.JobExecutionContext;  
import org.quartz.JobExecutionException;  
import org.springframework.scheduling.quartz.QuartzJobBean;  

public class SpringQtz extends QuartzJobBean{  
    private static int counter = 0;  
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {  
         System.out.println(Thread.currentThread().getName());
        long ms = System.currentTimeMillis();  
        System.out.println( new Date(ms));  
        System.out.println(ms);  
        System.out.println("(" + counter++ + ")");  
        String s = (String) context.getMergedJobDataMap().get("service");  
        System.out.println(s);  
     
    }  
}


spring配置文件


<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
       default-autowire="byName" default-lazy-init="false">
    

   
   <!--  配置调度程序quartz ,其中配置JobDetail有两种方式  
        方式一:使用JobDetailBean,任务类必须实现Job接口    -->
        <bean id="myjob" class="org.springframework.scheduling.quartz.JobDetailBean">  
        
         <property name="jobClass" value="com.ncs.hj.SpringQtz"></property> 
         <property name="jobDataAsMap">
                <map>
                    <entry key="service"><value>simple is the beat</value></entry>
                </map>
        </property>
        </bean> 
        

    <!-- ======================== 调度触发器 ======================== -->
    <bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="myjob"></property>
        <property name="cronExpression" value="0/5 * * * * ?"></property>
    </bean>

    <!-- ======================== 调度工厂 ======================== -->
    <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="CronTriggerBean"/>
            </list>
        </property>
    </bean>  

    
</beans>




第二种,作业类不继承特定基类。


java类

    package com.ncs.hj;  
      

      
    import java.util.Date;  
      
    public class SpringQtz2 {  
        private static int counter = 0;  
        protected void execute()  {  
            System.out.println(Thread.currentThread().getName());
            long ms = System.currentTimeMillis();  
            System.out.println( new Date(ms));  
            
            System.out.println("(" + counter++ + ")");  
        }  
    }



配置文件

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
       default-autowire="byName" default-lazy-init="false">
    
        
      <!--   方式二:使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,
      通过targetMethod指定调用方法定义目标bean和bean中的方法 -->
        <bean id="SpringQtzJob" class="com.ncs.hj.SpringQtz2"/>
        <bean id="SpringQtzJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="SpringQtzJob"/>
        </property>
        <property name="targetMethod"> <!--  要执行的方法名称 -->
            <value>execute</value>
        </property>
   <!--  禁止并发   -->
 <!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 --> 
        <property name="concurrent" value="false"/> 
    </bean>

    <!-- ======================== 调度触发器 ======================== -->
    <bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="SpringQtzJobMethod"></property>
        <property name="cronExpression" value="0/5 * * * * ?"></property>
    </bean>

    <!-- ======================== 调度工厂 ======================== -->
    <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="CronTriggerBean"/>
            </list>
        </property>
    </bean>  

    
</beans>



quartz默认是多线程的


参考文章:

http://my.oschina.net/u/559635/blog/389558

http://kevin19900306.iteye.com/blog/1397744

http://blog.csdn.net/huihuimimi17/article/details/8215779



本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1741169

Quartz 定时器任务调度

原文:http://tianxingzhe.blog.51cto.com/3390077/1741169

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!