【转】Quartz任务监控管理 http://sundoctor.iteye.com/blog/441951 Quartz任务监控管理

Quartz任务监控管理,类似Windows任务管理器,可以获得运行时的实时监控,查看任务运行状态,动态增加任务,暂停、恢复、移除任务等。对于动态增加任务,可以参加我的前一篇文章《Quartz如何在Spring动态配置时间》,本文在前文的基础上扩展,增加暂停、恢复、移除任务等功能,实现Quartz任务监控管理。


先看一下最终实现实现效果,只有两个页面 ,如下

在这个页面查看任务实时运行状态,可以暂停、恢复、移除任务等


在这个页面可以动态配置调度任务。


实现任务监控,必须能将数据持久化,这里采用数据库方式,Quartz对任务的数据库持久化有着非常好的支持。我在这里采用quartz 2.2.1,在Quartz发行包的docs\dbTables目录包含有各种数据库对应脚本,我用的是H2,所以选用tables_h2.sql建表。


1.配置applicationContext.xml文件

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:context="http://www.springframework.org/schema/context"     
  7.     xmlns:jdbc="http://www.springframework.org/schema/jdbc"     
  8.     xsi:schemaLocation="   
  9.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd   
  10.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  11.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd   
  12.    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd   
  13.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd   
  14.     " >  
  15.      
  16.    <context:component-scan base-package="com.sundoctor">  
  17.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  18.     </context:component-scan>  
  19.   
  20.        
  21.     <!-- 使用H2内存数据库并创建quartz数据库表 -->  
  22.     <jdbc:embedded-database id="dataSource" type="H2">  
  23.         <jdbc:script location="classpath:db/tables_h2.sql"/>          
  24.     </jdbc:embedded-database>    
  25.        
  26.     <!--Hibernate SessionFatory-->  
  27.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  28.         <property name="dataSource" ref="dataSource"/>  
  29.         <property name="packagesToScan">  
  30.             <list>  
  31.                 <value>com.sundoctor.example.model</value>                 
  32.             </list>  
  33.         </property>      
  34.         <property name="hibernateProperties">  
  35.             <props>  
  36.                 <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>  
  37.                 <prop key="hibernate.show_sql">true</prop>  
  38.                 <prop key="hibernate.format_sql">true</prop>                   
  39.                 <prop key="hibernate.hbm2ddl.auto">update</prop>           
  40.             </props>  
  41.         </property>  
  42.     </bean>      
  43.        
  44.     <!--Hibernate TransactionManager-->  
  45.     <bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  46.         <property name="sessionFactory" ref="sessionFactory"/>  
  47.     </bean>          
  48.   
  49.     <!-- 使用annotation定义事务 -->  
  50.     <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />          
  51.   
  52. </beans>  
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xsi:schemaLocation="
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
   http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    " >
  
   <context:component-scan base-package="com.sundoctor">
   		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

	
	<!-- 使用H2内存数据库并创建quartz数据库表 -->
    <jdbc:embedded-database id="dataSource" type="H2">
        <jdbc:script location="classpath:db/tables_h2.sql"/>       
    </jdbc:embedded-database>	
	
	<!--Hibernate SessionFatory-->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
	    <property name="dataSource" ref="dataSource"/>
		<property name="packagesToScan">
			<list>
				<value>com.sundoctor.example.model</value>				
			</list>
		</property>	
	    <property name="hibernateProperties">
	        <props>
	            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
	            <prop key="hibernate.show_sql">true</prop>
	            <prop key="hibernate.format_sql">true</prop>				
				<prop key="hibernate.hbm2ddl.auto">update</prop>        
	        </props>
	    </property>
	</bean>	
	
	<!--Hibernate TransactionManager-->
	<bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
	    <property name="sessionFactory" ref="sessionFactory"/>
	</bean>		

	<!-- 使用annotation定义事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />		

</beans>

配置Quartz,也分两步
1、配置quartz. properties

Java代码 复制代码  收藏代码
  1. …   
  2. org.quartz.jobStore.misfireThreshold = 60000  
  3. #org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore   
  4. org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX   
  5. org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate   
  6. #org.quartz.jobStore.useProperties = true  
  7. org.quartz.jobStore.tablePrefix = QRTZ_     
  8. org.quartz.jobStore.isClustered = false  org.quartz.jobStore.maxMisfiresToHandleAtATime=1  
…
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

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  5.   
  6.     <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >  
  7.         <property name="dataSource" ref ="dataSource" />          
  8.         <property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>  
  9.         <property name="configLocation" value="classpath:quartz.properties"/>            
  10.     </bean>  
  11.        
  12.     <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean" >  
  13.         <property name="jobClass">  
  14.             <value>com.sundoctor.example.service.MyQuartzJobBean</value>  
  15.         </property>  
  16.        <property name="durability" value="true" />       
  17.     </bean>  
  18.         
  19. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <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.JobDetailFactoryBean" >
        <property name="jobClass">
            <value>com.sundoctor.example.service.MyQuartzJobBean</value>
        </property>
       <property name="durability" value="true" />	
    </bean>
	 
</beans>

到些,相关配置全部完成,对于配置的具体描述,可以参加我的前一篇文章《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类中

暂停任务

Java代码 复制代码  收藏代码
  1. @Override  
  2. public void pauseTrigger(String triggerName, String group) {   
  3.     try {   
  4.         scheduler.pauseTrigger(new TriggerKey(triggerName, group));// 停止触发器   
  5.     } catch (SchedulerException e) {   
  6.         throw new RuntimeException(e);   
  7.     }   
  8. }  
	@Override
	public void pauseTrigger(String triggerName, String group) {
		try {
			scheduler.pauseTrigger(new TriggerKey(triggerName, group));// 停止触发器
		} catch (SchedulerException e) {
			throw new RuntimeException(e);
		}
	}

恢复任务

Java代码 复制代码  收藏代码
  1. @Override  
  2. public void resumeTrigger(String triggerName, String group) {   
  3.     try {   
  4.         scheduler.resumeTrigger(new TriggerKey(triggerName, group));// 重启触发器   
  5.     } catch (SchedulerException e) {   
  6.         throw new RuntimeException(e);   
  7.     }   
  8. }  
	@Override
	public void resumeTrigger(String triggerName, String group) {
		try {
			scheduler.resumeTrigger(new TriggerKey(triggerName, group));// 重启触发器
		} catch (SchedulerException e) {
			throw new RuntimeException(e);
		}
	}


移除任务

Java代码 复制代码  收藏代码
  1. @Override  
  2. public boolean removeTrigdger(String triggerName, String group) {   
  3.     TriggerKey triggerKey = new TriggerKey(triggerName, group);   
  4.     try {   
  5.         scheduler.pauseTrigger(triggerKey);// 停止触发器   
  6.         return scheduler.unscheduleJob(triggerKey);// 移除触发器   
  7.     } catch (SchedulerException e) {   
  8.         throw new RuntimeException(e);   
  9.     }   
  10. }  
	@Override
	public boolean removeTrigdger(String triggerName, String group) {
		TriggerKey triggerKey = new TriggerKey(triggerName, group);
		try {
			scheduler.pauseTrigger(triggerKey);// 停止触发器
			return scheduler.unscheduleJob(triggerKey);// 移除触发器
		} catch (SchedulerException e) {
			throw new RuntimeException(e);
		}
	}



其它类的实现请参加《Quartz如何在Spring动态配置时间》,那里有具体说明。

到此,基本简单实现了Quartz任务监控管理。其实面这里只是实现了Trigger任务的监控管理,没有实现Job任务的监控管理,实现Job任务的监控管理跟Trigger差不多。用Quartz可以很方便实现多样化的任务监控管理,Trigger任务和Job任务都可进行分组管理。

Quartz任务监控管理,类似Windows任务管理器,可以获得运行时的实时监控,查看任务运行状态,动态增加任务,暂停、恢复、移除任务等。对于动态增加任务,可以参加我的前一篇文章《Quartz如何在Spring动态配置时间》,本文在前文的基础上扩展,增加暂停、恢复、移除任务等功能,实现Quartz任务监控管理。


先看一下最终实现实现效果,只有两个页面 ,如下

在这个页面查看任务实时运行状态,可以暂停、恢复、移除任务等


在这个页面可以动态配置调度任务。


实现任务监控,必须能将数据持久化,这里采用数据库方式,Quartz对任务的数据库持久化有着非常好的支持。我在这里采用quartz 2.2.1,在Quartz发行包的docs\dbTables目录包含有各种数据库对应脚本,我用的是H2,所以选用tables_h2.sql建表。


1.配置applicationContext.xml文件

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:context="http://www.springframework.org/schema/context"     
  7.     xmlns:jdbc="http://www.springframework.org/schema/jdbc"     
  8.     xsi:schemaLocation="   
  9.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd   
  10.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  11.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd   
  12.    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd   
  13.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd   
  14.     " >  
  15.      
  16.    <context:component-scan base-package="com.sundoctor">  
  17.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  18.     </context:component-scan>  
  19.   
  20.        
  21.     <!-- 使用H2内存数据库并创建quartz数据库表 -->  
  22.     <jdbc:embedded-database id="dataSource" type="H2">  
  23.         <jdbc:script location="classpath:db/tables_h2.sql"/>          
  24.     </jdbc:embedded-database>    
  25.        
  26.     <!--Hibernate SessionFatory-->  
  27.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  28.         <property name="dataSource" ref="dataSource"/>  
  29.         <property name="packagesToScan">  
  30.             <list>  
  31.                 <value>com.sundoctor.example.model</value>                 
  32.             </list>  
  33.         </property>      
  34.         <property name="hibernateProperties">  
  35.             <props>  
  36.                 <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>  
  37.                 <prop key="hibernate.show_sql">true</prop>  
  38.                 <prop key="hibernate.format_sql">true</prop>                   
  39.                 <prop key="hibernate.hbm2ddl.auto">update</prop>           
  40.             </props>  
  41.         </property>  
  42.     </bean>      
  43.        
  44.     <!--Hibernate TransactionManager-->  
  45.     <bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  46.         <property name="sessionFactory" ref="sessionFactory"/>  
  47.     </bean>          
  48.   
  49.     <!-- 使用annotation定义事务 -->  
  50.     <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />          
  51.   
  52. </beans>  
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xsi:schemaLocation="
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
   http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    " >
  
   <context:component-scan base-package="com.sundoctor">
   		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

	
	<!-- 使用H2内存数据库并创建quartz数据库表 -->
    <jdbc:embedded-database id="dataSource" type="H2">
        <jdbc:script location="classpath:db/tables_h2.sql"/>       
    </jdbc:embedded-database>	
	
	<!--Hibernate SessionFatory-->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
	    <property name="dataSource" ref="dataSource"/>
		<property name="packagesToScan">
			<list>
				<value>com.sundoctor.example.model</value>				
			</list>
		</property>	
	    <property name="hibernateProperties">
	        <props>
	            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
	            <prop key="hibernate.show_sql">true</prop>
	            <prop key="hibernate.format_sql">true</prop>				
				<prop key="hibernate.hbm2ddl.auto">update</prop>        
	        </props>
	    </property>
	</bean>	
	
	<!--Hibernate TransactionManager-->
	<bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
	    <property name="sessionFactory" ref="sessionFactory"/>
	</bean>		

	<!-- 使用annotation定义事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />		

</beans>

配置Quartz,也分两步
1、配置quartz. properties

Java代码 复制代码  收藏代码
  1. …   
  2. org.quartz.jobStore.misfireThreshold = 60000  
  3. #org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore   
  4. org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX   
  5. org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate   
  6. #org.quartz.jobStore.useProperties = true  
  7. org.quartz.jobStore.tablePrefix = QRTZ_     
  8. org.quartz.jobStore.isClustered = false  org.quartz.jobStore.maxMisfiresToHandleAtATime=1  
…
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

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  5.   
  6.     <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >  
  7.         <property name="dataSource" ref ="dataSource" />          
  8.         <property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>  
  9.         <property name="configLocation" value="classpath:quartz.properties"/>            
  10.     </bean>  
  11.        
  12.     <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean" >  
  13.         <property name="jobClass">  
  14.             <value>com.sundoctor.example.service.MyQuartzJobBean</value>  
  15.         </property>  
  16.        <property name="durability" value="true" />       
  17.     </bean>  
  18.         
  19. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <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.JobDetailFactoryBean" >
        <property name="jobClass">
            <value>com.sundoctor.example.service.MyQuartzJobBean</value>
        </property>
       <property name="durability" value="true" />	
    </bean>
	 
</beans>

到些,相关配置全部完成,对于配置的具体描述,可以参加我的前一篇文章《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类中

暂停任务

Java代码 复制代码  收藏代码
  1. @Override  
  2. public void pauseTrigger(String triggerName, String group) {   
  3.     try {   
  4.         scheduler.pauseTrigger(new TriggerKey(triggerName, group));// 停止触发器   
  5.     } catch (SchedulerException e) {   
  6.         throw new RuntimeException(e);   
  7.     }   
  8. }  
	@Override
	public void pauseTrigger(String triggerName, String group) {
		try {
			scheduler.pauseTrigger(new TriggerKey(triggerName, group));// 停止触发器
		} catch (SchedulerException e) {
			throw new RuntimeException(e);
		}
	}

恢复任务

Java代码 复制代码  收藏代码
  1. @Override  
  2. public void resumeTrigger(String triggerName, String group) {   
  3.     try {   
  4.         scheduler.resumeTrigger(new TriggerKey(triggerName, group));// 重启触发器   
  5.     } catch (SchedulerException e) {   
  6.         throw new RuntimeException(e);   
  7.     }   
  8. }  
	@Override
	public void resumeTrigger(String triggerName, String group) {
		try {
			scheduler.resumeTrigger(new TriggerKey(triggerName, group));// 重启触发器
		} catch (SchedulerException e) {
			throw new RuntimeException(e);
		}
	}


移除任务

Java代码 复制代码  收藏代码
  1. @Override  
  2. public boolean removeTrigdger(String triggerName, String group) {   
  3.     TriggerKey triggerKey = new TriggerKey(triggerName, group);   
  4.     try {   
  5.         scheduler.pauseTrigger(triggerKey);// 停止触发器   
  6.         return scheduler.unscheduleJob(triggerKey);// 移除触发器   
  7.     } catch (SchedulerException e) {   
  8.         throw new RuntimeException(e);   
  9.     }   
  10. }  
	@Override
	public boolean removeTrigdger(String triggerName, String group) {
		TriggerKey triggerKey = new TriggerKey(triggerName, group);
		try {
			scheduler.pauseTrigger(triggerKey);// 停止触发器
			return scheduler.unscheduleJob(triggerKey);// 移除触发器
		} catch (SchedulerException e) {
			throw new RuntimeException(e);
		}
	}



其它类的实现请参加《Quartz如何在Spring动态配置时间》,那里有具体说明。

到此,基本简单实现了Quartz任务监控管理。其实面这里只是实现了Trigger任务的监控管理,没有实现Job任务的监控管理,实现Job任务的监控管理跟Trigger差不多。用Quartz可以很方便实现多样化的任务监控管理,Trigger任务和Job任务都可进行分组管理。

猜你喜欢

转载自javahepeng.iteye.com/blog/2160920