quartz学习2
转自:http://sundoctor.iteye.com/blog/441951
?
Quartz任务监控管理,类似Windows任务管理器,可以获得运行时的实时监控,查看任务运行状态,动态增加任务,暂停、恢复、移除任务等。对于动态增加任务,可以参加我的前一篇文章《Quartz如何在Spring动态配置时间》,本文在前文的基础上扩展,增加暂停、恢复、移除任务等功能,实现Quartz任务监控管理。?
先看一下最终实现实现效果,只有两个页面,如下??
在这个页面查看任务实时运行状态,可以暂停、恢复、移除任务等??
在这个页面可以动态配置调度任务。?
实现任务监控,必须能将数据持久化,这里采用数据库方式,Quartz对任务的数据库持久化有着非常好的支持。我在这里采用quartz 1.6.5,在Quartz发行包的docs\dbTables目录包含有各种数据库对应脚本,我用的是MySql 5.0,所以选用tables_mysql_innodb.sql建表。?
建表完成后,配置数据库连接池,分两步:?
1、配置jdbc.properties文件?

- jdbc.driverClassName=com.mysql.jdbc.Driver??
- jdbc.url=jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true??
- jdbc.username=root??
- jdbc.password=kfs??
2.配置applicationContext.xml文件?

- <bean?id="propertyConfigurer"?class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">??
- ????<property?name="locations">??
- ????????????<list>??
- ????????????????<value>classpath:jdbc.properties</value>??
- ????????????</list>??
- ????????</property>??
- </bean>??
- ??????
- ????<!--?数据源定义,使用c3p0?连接池?-->??
- <bean?id="dataSource"?class="com.mchange.v2.c3p0.ComboPooledDataSource"?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="${cpool.minPoolSize}"/>?????
- ????<property?name="minPoolSize"?value="${cpool.minPoolSize}"?/>????
- ????<property?name="maxPoolSize"?value="${cpool.maxPoolSize}"?/>????
- ????<property?name="acquireIncrement"?value="${cpool.acquireIncrement}"?/>???
- ????<property?name="maxIdleTime"?value="${cpool.maxIdleTime}"/>?????
- </bean>??
配置Quartz,也分两步?
1、配置quartz. properties?

- …??
- org.quartz.jobStore.misfireThreshold?=?60000??
- #org.quartz.jobStore.class?=?org.quartz.simpl.RAMJobStore??
- org.quartz.jobStore.class?=?org.quartz.impl.jdbcjobstore.JobStoreTX??
- org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate??
- #org.quartz.jobStore.useProperties?=?true??
- org.quartz.jobStore.tablePrefix?=?QRTZ_????
- org.quartz.jobStore.isClustered?=?false??org.quartz.jobStore.maxMisfiresToHandleAtATime=1??
在这里采用JobStoreTX,将任务持久化到数据中,而不再是简单的内存方式:RAMJobStore?
2、配置applicationContext-quartz.xml?

- <bean?name="quartzScheduler"?class="org.springframework.scheduling.quartz.SchedulerFactoryBean">??
- ????????<property?name="dataSource"?ref?="dataSource"?/>?????????
- ????????<property?name="applicationContextSchedulerContextKey"?value="applicationContextKey"/>??
- ????????<property?name="configLocation"?value="classpath:quartz.properties"/>??
- ????</bean>??
- ??????
- <bean?id="jobDetail"?class="org.springframework.scheduling.quartz.JobDetailBean">??
- ????????<property?name="jobClass">??
- ????????????<value>??
- ????????????????com.sundoctor.example.service.MyQuartzJobBean??
- ????????????</value>??
- ????????</property>??
- ????????<property?name="jobDataAsMap">??
- ????????????<map>??
- ????????????????<entry?key="simpleService">??
- ????????????????????<ref?bean="simpleService"/>??
- ????????????????</entry>??
- ????????????</map>??
- ????????</property>??????
- </bean>??
到些,相关配置全部完成,对于配置的具体描述,可以参加我的前一篇文章《Quartz如何在Spring动态配置时间》?
实现任务动态添加配置?
请参考com.sundoctor.quartz.service.SchedulerServiceImpl.java中的各种schedule方法,在《Quartz如何在Spring动态配置时间》有具体描述。在这里说一下:?
添加一个Job在表qrtz_job_details插入一条记录?
添加一个Simple Trigger在表qrtz_simple_triggers插入一条记录?
添加一个Cron Trigger 在表qrtz_cron_triggers插入一条记录?
添加Simple Trigger和Cron Trigger都会同进在表qrtz_triggers插入一条记录,开始看的第一个页面调度任务列表数据就是从qrtz_triggers表获取?
实现任务实时监控,暂停、恢复、移除任务等?
在com.sundoctor.quartz.service.SchedulerServiceImpl.java类中?
暂停任务?

- public?void?pauseTrigger(String?triggerName,String?group){????????
- ????????try?{??
- ????????????scheduler.pauseTrigger(triggerName,?group);//停止触发器??
- ????????}?catch?(SchedulerException?e)?{??
- ????????????throw?new?RuntimeException(e);??
- ????????}??
- }??
恢复任务?

- public?void?resumeTrigger(String?triggerName,String?group){???????
- ????????try?{??
- ????????????scheduler.resumeTrigger(triggerName,?group);//重启触发器??
- ????????}?catch?(SchedulerException?e)?{??
- ????????????throw?new?RuntimeException(e);??
- ????????}??
- ????}??
移除任务?

- public?boolean?removeTrigdger(String?triggerName,String?group){???????
- ????????try?{??
- ????????????scheduler.pauseTrigger(triggerName,?group);//停止触发器??
- ????????????return?scheduler.unscheduleJob(triggerName,?group);//移除触发器??
- ????????}?catch?(SchedulerException?e)?{??
- ????????????throw?new?RuntimeException(e);??
- ????????}??
- ????}??
其它类的实现请参加《Quartz如何在Spring动态配置时间》,那里有具体说明。?
到此,基本简单实现了Quartz任务监控管理。其实面这里只是实现了Trigger任务的监控管理,没有实现Job任务的监控管理,实现Job任务的监控管理跟Trigger差不多。用Quartz可以很方便实现多样化的任务监控管理,Trigger任务和Job任务都可进行分组管理。?