Quartz+Spring 自定義做業調度(做業在DB中配置)
Quartz版本爲1.8.3
Spring版本爲2.5
自定義做業表 QRTZ_JOB。
其中定義 做業標識、做業名稱、類名、觸發器名稱、觸發器腳本等。
下面看看在Spring中如何配置Quartz。
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"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init
="false"
>
<
context:component-scan
base-package
="com.jn"
/>
<
tx:advice
id
="txAdvice"
transaction-manager
="txManager"
>
<
tx:attributes
>
<
tx:method
name
="insert*"
propagation
="REQUIRED"
/>
<
tx:method
name
="del*"
propagation
="REQUIRED"
/>
<
tx:method
name
="update*"
propagation
="REQUIRED"
/>
<
tx:method
name
="*"
read-only
="true"
/>
</
tx:attributes
>
</
tx:advice
>
![](http://static.javashuo.com/static/loading.gif)
<
aop:config
>
<
aop:pointcut
id
="allManagerMethod"
expression
="execution(* com.jn.*.*(..))"
/>
<
aop:advisor
advice-ref
="txAdvice"
pointcut-ref
="allManagerMethod"
/>
</
aop:config
>
<
bean
id
="txManager"
class
="org.springframework.jdbc.datasource.DataSourceTransactionManager"
>
<
property
name
="dataSource"
ref
="proxoolDataSource"
/>
</
bean
>
<
bean
id
="propertyConfigurer"
class
="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
>
<
property
name
="locations"
>
<
list
>
<
value
>
classpath:jdbc.properties
</
value
>
</
list
>
</
property
>
</
bean
>
<
bean
id
="proxoolDataSource"
class
="org.apache.commons.dbcp.BasicDataSource"
destroy-method
="close"
>
<
property
name
="driverClassName"
value
="${driver}"
/>
<
property
name
="url"
value
="${dburl}"
/>
<
property
name
="username"
value
="${username}"
/>
<
property
name
="password"
value
="${password}"
/>
</
bean
>
<
bean
id
="sqlMapClient"
class
="org.springframework.orm.ibatis.SqlMapClientFactoryBean"
>
<
property
name
="dataSource"
>
<
ref
local
="proxoolDataSource"
/>
</
property
>
<
property
name
="configLocation"
>
<
value
>
classpath:sql-map-config.xml
</
value
>
</
property
>
</
bean
>
<
bean
name
="quartzScheduler"
lazy-init
="true"
class
="org.springframework.scheduling.quartz.SchedulerFactoryBean"
>
<
property
name
="dataSource"
ref
="proxoolDataSource"
/>
<
property
name
="applicationContextSchedulerContextKey"
value
="applicationContextKey"
/>
<
property
name
="configLocation"
value
="classpath:quartz.properties"
/>
</
bean
>
</
beans
>
Main.java
package
com.jn.common;
![](http://static.javashuo.com/static/loading.gif)
import
com.jn.qrtz.job.JobManager;
import
com.jn.spring.BeanFactory;
![](http://static.javashuo.com/static/loading.gif)
/**
* 啓動類
* @author l
*/
public
class
Main
{
/**
* 啓動函數
* @param args
*/
public static void main(String[] args) {
try {
JobManager mgr = (JobManager)BeanFactory.factory().getBean("jobManager");
mgr.init();
mgr.dispatch();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
JobManager.java
package
com.jn.qrtz.job;
![](http://static.javashuo.com/static/loading.gif)
import
java.sql.SQLException;
import
java.util.ArrayList;
import
java.util.List;
![](http://static.javashuo.com/static/loading.gif)
import
org.apache.log4j.Logger;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.beans.factory.annotation.Qualifier;
import
org.springframework.context.annotation.Scope;
import
org.springframework.stereotype.Component;
![](http://static.javashuo.com/static/loading.gif)
import
com.jn.persistence.QrtzDaoImpl;
import
com.jn.qrtz.JobConfig;
import
com.jn.qrtz.service.SchedulerServiceImpl;
![](http://static.javashuo.com/static/loading.gif)
/**
* 做業管理類
*
* @author l
*/
@Component(
"
jobManager
"
)
@Scope(
"
singleton
"
)
public
class
JobManager
{
private Logger log = Logger.getLogger(JobManager.class);
![](http://static.javashuo.com/static/loading.gif)
@Autowired(required = true)
private QrtzDaoImpl qrtzDao;
![](http://static.javashuo.com/static/loading.gif)
@Autowired(required = true)
private SchedulerServiceImpl schedulerService;
![](http://static.javashuo.com/static/loading.gif)
/** 做業列表 */
private List<JobConfig> allJobs = new ArrayList<JobConfig>();
![](http://static.javashuo.com/static/loading.gif)
/**
* 初始化做業列表
*/
public synchronized void init() {
try {
allJobs = qrtzDao.queryAllJobs();
log.info("做業初始化完成。");
}
catch (SQLException e) {
log.error("初始化做業失敗。" + e.getMessage());
}
}
![](http://static.javashuo.com/static/loading.gif)
/**
* 系統啓動時派發做業
*/
public void dispatch() {
for (JobConfig job : allJobs) {
try {
schedulerService.schedule(job);
}
catch (Exception e) {
e.printStackTrace();
log.error(job.toString() + "派發失敗。" + e.getMessage());
}
}
}
![](http://static.javashuo.com/static/loading.gif)
public SchedulerServiceImpl getSchedulerService() {
return schedulerService;
}
![](http://static.javashuo.com/static/loading.gif)
public void setSchedulerService(
@Qualifier("schedulerService") SchedulerServiceImpl schedulerService) {
this.schedulerService = schedulerService;
}
![](http://static.javashuo.com/static/loading.gif)
public QrtzDaoImpl getQrtzDao() {
return qrtzDao;
}
![](http://static.javashuo.com/static/loading.gif)
public void setQrtzDao(@Qualifier("qrtzDao") QrtzDaoImpl qrtzDao) {
this.qrtzDao = qrtzDao;
}
}
其中QrtzDaoImpl對象是用於從QRTZ_JOB表中取得做業列表,並將做業封裝爲JobConfig對象。
SchedulerServiceImpl對象用於派發Job。
SchedulerServiceImpl.java
package
com.jn.qrtz.service;
![](http://static.javashuo.com/static/loading.gif)
import
org.quartz.CronTrigger;
import
org.quartz.JobDetail;
import
org.quartz.Scheduler;
import
org.quartz.SchedulerException;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.beans.factory.annotation.Qualifier;
import
org.springframework.stereotype.Component;
![](http://static.javashuo.com/static/loading.gif)
import
com.jn.qrtz.JobConfig;
import
com.jn.qrtz.job.WorkDispatcher;
![](http://static.javashuo.com/static/loading.gif)
/**
* 做業派發、移除類
* @author l
*/
@Component(
"
schedulerService
"
)
public
class
SchedulerServiceImpl
{
@Autowired
private Scheduler scheduler;
![](http://static.javashuo.com/static/loading.gif)
/**
* 移除做業
* @param config
* @return
* @throws SchedulerException
*/
public boolean remove(JobConfig config) throws SchedulerException {
if(config == null) {
return false;
}
return removeJob(config.getJobName(), config.getJobGroup());
}
![](http://static.javashuo.com/static/loading.gif)
/**
* 派發做業
* @param config
* @throws Exception
*/
public void schedule(JobConfig config) throws Exception {
String triggerName = config.getTriggerName();
String triggerGroup = config.getTriggerGroup();
String cronStr = config.getTriggerScript();
String jobName = config.getJobName();
String jobGroup = config.getJobGroup();
JobDetail jobDetail = new JobDetail(jobName, jobGroup, WorkDispatcher.class);
jobDetail.getJobDataMap().put(JobConfig.EXEC_INFO, config.cloneInfo());
schedule(triggerName, triggerGroup, cronStr, jobDetail);
}
![](http://static.javashuo.com/static/loading.gif)
/**
* 派發做業
* @param name
* @param group
* @param cronStr
* @param jobDtl
* @throws Exception
*/
private void schedule(String name, String group, String cronStr, JobDetail jobDtl)
throws Exception {
CronTrigger cronTrigger = new CronTrigger(name, group, jobDtl.getName(), jobDtl.getGroup(),
cronStr);
scheduler.scheduleJob(jobDtl, cronTrigger);
}
![](http://static.javashuo.com/static/loading.gif)
/**
* 移除做業
* @param jobName
* @param group
* @return
* @throws SchedulerException
*/
private boolean removeJob(String jobName, String group) throws SchedulerException {
scheduler.pauseJob(jobName, group);
return scheduler.deleteJob(jobName, group);
}
@Autowired
public void setScheduler(@Qualifier("quartzScheduler") Scheduler scheduler) {
this.scheduler = scheduler;
}
}
由這些代碼,即可以經過Quartz框架去調度 咱們定義在QRTZ_JOB表中的做業了。 因爲Quartz框架自己依賴一些表,其中咱們執行的做業,一樣會被框架保存在那些它所依賴的表中, 如:qrtz_job_details表。 當Spring加載quartzScheduler時,Quartz框架會被自動啓動並調度保存在qrtz_job_details表中的做業。 因此再次啓動時,應先將 Quartz依賴的表清空。 固然這個操做也能夠被集成在代碼中。 還有另一種方案可實現此的功能,即是重寫org.springframework.scheduling.quartz.SchedulerFactoryBean類, 自定義類繼承此類,在自定義的類中注入本身的對象,在其中取得 做業列表, 並生成Trigger對象數組,調用 org.springframework.scheduling.quartz.SchedulerFactoryBean 類的setTriggers(Trigger[]) 方法。 設置好Trigger數據。 最後再 調用Scheduler的start()方法,Quartz即可調度這些做業了。
歡迎關注本站公眾號,獲取更多信息