Quartz结合Spring框架在真实项目中的使用

jopen 11年前

需求:在某个时间做某件事,任务是动态的,到点就要执行

在spring的配置文件中加入如下配置

 
    <!-- quartz配置 -->        <bean id="scheduler"            class="org.springframework.scheduling.quartz.SchedulerFactoryBean">            <property name="schedulerContextAsMap">                <map>                    <entry key="timeout" value="30" />                </map>            </property>            <property name="quartzProperties">                <props>                    <prop key="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.JobStoreTX</prop>                    <prop key="org.quartz.jobStore.tablePrefix">QRTZ_</prop>                    <prop key="org.quartz.jobStore.dataSource">qzDS</prop>                    <prop key="org.quartz.dataSource.qzDS.driver">${driverClass}</prop>                    <prop key="org.quartz.dataSource.qzDS.URL">${jdbcUrl}</prop>                    <prop key="org.quartz.dataSource.qzDS.user">${username}</prop>                    <prop key="org.quartz.dataSource.qzDS.password">${password}</prop>                    <prop key="org.quartz.dataSource.qzDS.maxConnections">30</prop>                    <prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>                    <prop key="org.quartz.threadPool.threadCount">10</prop>                </props>             </property>          </bean>   
</div> </div> 自定义一个listener监听器必须放在spring监听器后面,待spring容器启动完成后执行
 
    **         * Quartz任务检测监听器         * 在Spring容器启动后检测是否有未完成的任务         * 如果有的话,就继续执行         * @author winter.liu         *         */        public class QuartzListener implements ServletContextListener{                    @Override            public void contextDestroyed(ServletContextEvent event) {                            }                    @Override            public void contextInitialized(ServletContextEvent event) {                try{                    Scheduler scheduler=(Scheduler)SpringUtil.getWebBean("scheduler");                    //获取调度器中所有的触发器组                    String[] tiggerGroups=scheduler.getTriggerGroupNames();                    for(int i=0;i<tiggerGroups.length;i++){                        String[] trigges=scheduler.getTriggerNames(tiggerGroups[i]);                        for(int j=0;j<trigges.length;j++){                                                        Trigger tg=scheduler.getTrigger(trigges[j],tiggerGroups[i]);                                                        if(tg instanceof SimpleTrigger){                                scheduler.rescheduleJob(trigges[j],tiggerGroups[i], tg);                            }                        }                    }                    //开启容器                    scheduler.start();                }catch (Exception e) {                    e.printStackTrace();                }                            }                }  
</div> </div> 编写一个简单的测试job 
    /**        * 测试job        * @author winter.liu        *        */        public class SimpleJob implements Job{                    @Override            public void execute(JobExecutionContext jobCtx) throws JobExecutionException {                System.out.println(jobCtx.getTrigger().getName()+" triggered.time is: "+(new Date()));                    }                }  
    /**        * 自定义发布job工具类        * @author winter.liu        *        */        public class ScheduleJob {                        /**            * @param job 任务源对象            * @param cronExpression  Cron表达式 例如:0 07 14 2 12 ? 2013            */            public static void scheduleJobBean(Class<?> job,String cronExpression){                try {                     JobDetail jobDetail=new JobDetail(UUID.randomUUID().toString(),UUID.randomUUID().toString(),job);                     CronTrigger cronTrigger=new CronTrigger(UUID.randomUUID().toString(),UUID.randomUUID().toString());                     CronExpression cexp=new CronExpression(cronExpression);                     cronTrigger.setCronExpression(cexp);                     Scheduler scheduler=(Scheduler)SpringUtil.getWebBean("scheduler");                     scheduler.scheduleJob(jobDetail,cronTrigger);                } catch (Exception e) {                    e.printStackTrace();                }                            }                }  
</div> </div> 使用的lib版本是spring3.05和quartz 1.8.6的