首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

CronTrigger的未触发授命学习

2012-10-17 
CronTrigger的未触发指令学习CronTrigger的未触发指令MISFIRE_INSTRUCTION_FIRE_ONCE_NOW与默认的MISFIRE_

CronTrigger的未触发指令学习

CronTrigger的未触发指令MISFIRE_INSTRUCTION_FIRE_ONCE_NOW与默认的MISFIRE_INSTRUCTION_SMART_POLICY指令功能一致,另外还有一种指令.MISFIRE_INSTRUCTION_DO_NOTHING,实例区别如下:


CronTrigger的未触发授命学习

MyQuartzJobBean.java:

?

package quartz.example.example5.test1;

?

import java.util.Date;

?

import org.quartz.JobDataMap;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.quartz.Trigger;

import org.springframework.scheduling.quartz.QuartzJobBean;

?

?

public class MyQuartzJobBean extends QuartzJobBean {

?

?

@SuppressWarnings("unused")

@Override

protected void executeInternal(JobExecutionContext jobexecutioncontext) throws JobExecutionException {

Trigger trigger = jobexecutioncontext.getTrigger();

JobDataMap ?map=jobexecutioncontext.getJobDetail().getJobDataMap();

String triggerName = trigger.getName();

String group = trigger.getGroup();

Date lastalivetime=new Date();

System.out.println("testMethod1.......1"+lastalivetime);

}

?

}



MisFireTest.java:

?

package quartz.example.example5.test1;

?

import org.quartz.CronTrigger;

import org.quartz.JobDetail;

import org.quartz.Scheduler;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

?

public class MisFireTest {

public ApplicationContext app;

public Scheduler sched;

public JobDetail job;

?

public void run() throws Exception {

?

System.out.println("------- Initializing -------------------");

?

System.out.println("------- Initialization Complete -----------");

?

System.out.println("------- Scheduling Jobs -----------");

?

CronTrigger trigger = new CronTrigger("trigger11", "group", job.getName(),

Scheduler.DEFAULT_GROUP);

trigger.setCronExpression("0/5 * * ? * * *");

trigger.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);

//trigger.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW);

sched.scheduleJob(job, trigger);

System.out.println("------- Starting Scheduler ----------------");

sched.start();

System.out.println("------- Started Scheduler -----------------");

try {

Thread.sleep(10L * 1000L);

} catch (Exception e) {

}

System.out.println("------- Shutting Down ---------------------");

sched.pauseTrigger("trigger11", "group");

try {

Thread.sleep(10L * 1000L);//暂停10秒

} catch (Exception e) {

}

sched.resumeTrigger("trigger11", "group");

System.out.println("------- Shutdown Complete -----------------");

try {

Thread.sleep(10L * 1000L);

} catch (Exception e) {

}

sched.shutdown(true);

}

?

public static void main(String[] args) ?throws Exception {

MisFireTest example = new MisFireTest();

example.app = new ClassPathXmlApplicationContext(new String[] {

"classpath:applicationContext.xml",

"classpath:applicationContext-quartz.xml" });

example.sched = (Scheduler) example.app.getBean("quartzScheduler");

example.job = (JobDetail) example.app.getBean("jobDetail");

example.run();

}

?

}

?

applicationContext-quartz.xml:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>? ? <bean name="quartzScheduler" value="applicationContextKey"/>? ? ? ? <property name="configLocation" value="classpath:quartz.properties"/>? ? </bean>? ??? ? ?<bean id="jobDetail" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"? ? xmlns:aop="http://www.springframework.org/schema/aop"? ? xmlns:tx="http://www.springframework.org/schema/tx"? ? xmlns:context="http://www.springframework.org/schema/context"? ? ?xmlns:jee="http://www.springframework.org/schema/jee"? ? xsi:schemaLocation="? ? http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd? ? http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd? ? http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd? ? http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd? ? ?http://www.springframework.org/schema/jee? ? ? ?http://www.springframework.org/schema/jee/spring-jee-2.5.xsd" ?>??? ?<context:component-scan base-package="com.sundoctor"/><!-- 属性文件读入 --><bean id="propertyConfigurer" destroy-method="close"><property name="driverClass" value="${jdbc.driverClassName}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="initialPoolSize" value="5" /><property name="minPoolSize" value="5" /><property name="maxPoolSize" value="20" /><property name="acquireIncrement" value="2" /><property name="maxIdleTime" value="3600" /><property name="idleConnectionTestPeriod" ?value="180"/> ?<property name="automaticTestTable" value="C3P0TESTTABLE"/>?</bean>
</beans>
jdbc.properties:jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&autoReconnect=truejdbc.username=rootjdbc.password=
quartz.properties:#============================================================== ?#Configure Main Scheduler Properties ?#============================================================== ??org.quartz.scheduler.instanceName = TestScheduler1 ??org.quartz.scheduler.instanceId = AUTO ?
#============================================================== ?#Configure ThreadPool ?#==============================================================?org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPoolorg.quartz.threadPool.threadCount = 10org.quartz.threadPool.threadPriority = 5org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
#============================================================== ?#Configure JobStore ?#==============================================================#注意改参数如果大于10秒则策略失效,重新启动时会补上暂停时段的job?org.quartz.jobStore.misfireThreshold = 5000org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTXorg.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegateorg.quartz.jobStore.tablePrefix = QRTZ_org.quartz.jobStore.maxMisfiresToHandleAtATime=10org.quartz.jobStore.isClustered = true ?org.quartz.jobStore.clusterCheckinInterval = 20000 ?
执行MisFireTest类,结果如下:------- Initializing -------------------------- Initialization Complete ------------------ Scheduling Jobs ------------------ Starting Scheduler ----------------------- Started Scheduler -----------------testMethod1.......1Tue Sep 27 18:44:15 CST 2011testMethod1.......1Tue Sep 27 18:44:20 CST 2011------- Shutting Down ---------------------------- Shutdown Complete -----------------testMethod1.......1Tue Sep 27 18:44:35 CST 2011testMethod1.......1Tue Sep 27 18:44:40 CST 2011
如果改成MISFIRE_INSTRUCTION_FIRE_ONCE_NOW策略,结果如下:------- Initializing -------------------------- Initialization Complete ------------------ Scheduling Jobs ------------------ Starting Scheduler ----------------------- Started Scheduler -----------------testMethod1.......1Tue Sep 27 18:46:55 CST 2011testMethod1.......1Tue Sep 27 18:47:00 CST 2011------- Shutting Down ---------------------------- Shutdown Complete -----------------testMethod1.......1Tue Sep 27 18:47:14 CST 2011testMethod1.......1Tue Sep 27 18:47:15 CST 2011testMethod1.......1Tue Sep 27 18:47:20 CST 2011
如果org.quartz.jobStore.misfireThreshold = 20000,这无论使用那种策略结果均如下:------- Initializing -------------------------- Initialization Complete ------------------ Scheduling Jobs ------------------ Starting Scheduler ----------------------- Started Scheduler -----------------testMethod1.......1Tue Sep 27 18:50:00 CST 2011testMethod1.......1Tue Sep 27 18:50:05 CST 2011testMethod1.......1Tue Sep 27 18:50:10 CST 2011------- Shutting Down ---------------------------- Shutdown Complete -----------------testMethod1.......1Tue Sep 27 18:50:20 CST 2011 ?//补上服务暂停时的任务testMethod1.......1Tue Sep 27 18:50:20 CST 2011testMethod1.......1Tue Sep 27 18:50:25 CST 2011testMethod1.......1Tue Sep 27 18:50:30 CST 2011

热点排行