Quartz任务监控管理

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


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

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


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


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

1.配置applicationContext.xml文件

<?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


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 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类中

暂停任务

@Override  
public void pauseTrigger(String triggerName, String group) {  
    try {  
        scheduler.pauseTrigger(new TriggerKey(triggerName, group));// 停止触发器  
    } catch (SchedulerException e) {  
        throw new RuntimeException(e);  
    }  
}  


恢复任务

@Override  
public void resumeTrigger(String triggerName, String group) {  
    try {  
        scheduler.resumeTrigger(new TriggerKey(triggerName, group));// 重启触发器  
    } catch (SchedulerException e) {  
        throw new RuntimeException(e);  
    }  
}  

移除任务

@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任务都可进行分组管理。

猜你喜欢

转载自kangrui.iteye.com/blog/2160966
今日推荐