论坛首页 Java企业应用论坛

Quartz任务监控管理 (1)

浏览 73327 次
该帖已经被评为精华帖
作者 正文
   发表时间:2009-08-07   最后修改:2010-02-01
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任务都可进行分组管理。

Quartz很强大,也很简单,只有想不到的,没有做不到的,人有多大胆,地有多高产。
   发表时间:2009-08-09  
<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>

<map>  
                <entry key="simpleService">  
                    <ref bean="simpleService"/>  
                </entry>  
            </map>
这段,如果不用anntion,在配置文件里面怎么配制?因为我quartz调用的时候,simpleService为null,set了也不行。。。我的类似simpleService的serivce实现了序列化接口了。。。为什么还是null?期待解答
0 请登录后投票
   发表时间:2009-08-09  
不用acnntion,这样就行了
<bean name="simpleService" class="com.sundoctor.example.service.SimpleService" />
0 请登录后投票
   发表时间:2009-08-09  
还是null直
0 请登录后投票
   发表时间:2009-08-09   最后修改:2009-08-09
我的配置文件
<?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-2.0.xsd">

<bean id="articleBS" class="com.cms.article.service.impl.ArticleBS">
<property name="tarticleDAO">
<ref bean="article.tarticleDAO" />
</property>
</bean>

<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
</bean>

<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.cms.quartzpublic.job.ArticleQuartzJobBean</value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="articleBS">
<ref local="articleBS" />
</entry>
</map>
</property>

</bean>

<!-- cms 定时发布文章模块service层ioc及事务配置 start -->
<bean id="articleQuartzBS" lazy-init="true"
class="com.cms.quartzpublic.serivce.impl.ArticleQuartzBS">

        <property name="articleQuartzDAO">
<ref bean="article.articleQuartzDAO" />
</property>

       
        <property name="scheduler">
        <ref bean="quartzScheduler" />
        </property>
<property name="jobDetail">
<ref bean="jobDetail" />
        </property>
       
</bean>
    
    <bean id="article.quartzBSTrans" parent="cmsBaseTransationProxy">
<property name="target">
<ref local="articleQuartzBS" />
</property>
</bean>
<!-- cms 定时发布文章模块service层ioc及事务配置 end -->
</beans>

ArticleQuartzJobBean 类如下
package com.cms.quartzpublic.job;

import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Trigger;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.cms.article.service.IArticleBS;
import com.cms.article.service.impl.ArticleBS;
import com.cms.article.vo.TarticleVO;
import com.cms.po.Tarticle;
import com.cms.quartzpublic.serivce.impl.ArticleQuartzBS;

public class ArticleQuartzJobBean extends QuartzJobBean {

private ArticleBS articleBS;


public void setArticleBS(ArticleBS articleBS) {
this.articleBS = articleBS;
}


@Override
protected void executeInternal(JobExecutionContext jobexecutioncontext)
throws JobExecutionException {
Trigger trigger = jobexecutioncontext.getTrigger();
String triggerName = trigger.getName();
String group = trigger.getGroup();
String[] triggerNameArray = triggerName.split("\\$");
String articleId = triggerNameArray[1];
try {
TarticleVO tarticleVO = articleBS.lookArticleById(articleId);
String fileNamePath = articleBS.createHtmlDir(tarticleVO, tarticleVO.getChannleName());
String articlehref = articleBS.jspTOhtml(fileNamePath,"",tarticleVO);
if(articlehref != null && !articlehref.equals("")){
tarticleVO.setArticlehref(articlehref);
Tarticle tarticle2 = articleBS.saveOrUpdateArticle(tarticleVO);
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
private ArticleBS articleBS;这个一直是null值
这个articleBS实现了序列化接口了。。。。
问题在哪里。。。。。郁闷了,盼解决
0 请登录后投票
   发表时间:2009-08-10   最后修改:2009-08-10
引用

<bean id="articleBS" class="com.cms.article.service.impl.ArticleBS">
<property name="tarticleDAO">
<ref bean="article.tarticleDAO" />
</property>
</bean>


不但com.cms.article.service.impl.ArticleBS要实现Serializable序列化接口,其中注入的article.tarticleDAO也必须实现实现Serializable序列化接口。

引用
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.cms.quartzpublic.job.ArticleQuartzJobBean</value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="articleBS">
<ref local="articleBS" />
</entry>
</map>
</property>
</bean>


com.cms.quartzpublic.job.ArticleQuartzJobBean类被序列化保存到数据库表qrtz_job_details的job_class_name字段中,quartz在运行时会读取qrtz_job_details表中的job_class_name将其反序列化。这也是为什么com.cms.article.service.impl.ArticleB和其中注入各属性需要实现Serializable序列化接口的原因,所以你每次修改ArticleQuartzJobBean类或者其中的articleBS都要删除qrtz_job_details表对应的job记录,否则可能会出现空指针异常,因为你如果你没有删除qrtz_job_details表中的记录,你修改的东东并不会自动更新到qrtz_job_details中,你用的还是原来旧版本的ArticleQuartzJobBean类。



0 请登录后投票
   发表时间:2009-08-25  
请教楼主:
我使用的是oracle,为什么会报错呢,是不是哪里配置需要修改呢?

java.lang.RuntimeException: org.quartz.impl.jdbcjobstore.LockException: Failure obtaining db row lock: ORA-01002: 读取违反顺序
[See nested exception: java.sql.SQLException: ORA-01002: 读取违反顺序]
0 请登录后投票
   发表时间:2009-08-25  
bh_nesta 写道
请教楼主:
我使用的是oracle,为什么会报错呢,是不是哪里配置需要修改呢?

java.lang.RuntimeException: org.quartz.impl.jdbcjobstore.LockException: Failure obtaining db row lock: ORA-01002: 读取违反顺序
[See nested exception: java.sql.SQLException: ORA-01002: 读取违反顺序]


oracle 需要修改quartz. properties 文件将
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
改为
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
0 请登录后投票
   发表时间:2009-08-26  
楼主说的,我已经改过来了。但是还是一样的错误!
使用mysql的时候是可以的!
谢谢楼主的答复!
0 请登录后投票
   发表时间:2009-08-26  
我用oracle 10g试了一下,没有什么问题,一切正常呀。我没有修改org.quartz.jobStore.driverDelegateClass也没有问题。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics