Quartz(09) quartz spring web 项目的整合(终极版)

上一节(Quartz(08) quartz spring web 项目的整合(方法二))
这一章我们将采用最简单的一种方式整合quartz spring web. 达到的效果是,我们只需要编写自己的job类,关于job,trigger 的配置信息都存放到数据库.(注:我们公司的的项目就是这么配置的,非常方便)

源码下载地址

1.编写我们自定义的job类Q1,Q2

package com.quartz.job;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class Q1 implements Serializable, Job {

    private static final long serialVersionUID = 6890216263057956690L;

    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("------------------------");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
        System.out.println(sdf.format(new Date()));
        System.out.println("------------------------");
    }

}
package com.quartz.job;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;

public class Q2 implements Serializable, Job {

    private static final long serialVersionUID = 5004730246347558783L;

    public void execute(JobExecutionContext arg0) {
        System.out.println("************************");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
        System.out.println(sdf.format(new Date()));
        System.out.println("************************");
    }

}
  1. 配置quartz.properties 文件(并无特殊之处)
# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#

org.quartz.scheduler.instanceName=DefaultQuartzScheduler
org.quartz.scheduler.rmi.export=false
org.quartz.scheduler.rmi.proxy=false
org.quartz.scheduler.wrapJobExecutionInUserTransaction=false

org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=10
org.quartz.threadPool.threadPriority=5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true

#jobStoreTX TEST
org.quartz.jobStore.misfireThreshold=60000
# TX method
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
#jdbc Delegate
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#table prefix
org.quartz.jobStore.tablePrefix=qrtz_
#
org.quartz.jobStore.dataSource=myDS

org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver   
org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/test
org.quartz.dataSource.myDS.user=root   
org.quartz.dataSource.myDS.password=123456   
org.quartz.dataSource.myDS.maxConnections=10

  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"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <!-- 配置Quartz第三种方式,完全基于数据库,无需配置自定义的job类-->
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <!-- 以下配置实现了job的持久化 (JobStoreTX)-->
        <!-- 事先在数据库里面配置好quartz的信息,然后quartz自动去读取,并实例化 -->
        <!-- 运行资源文件myQuartzTest.sql文件,就配置好了相关的信息三张表 -->
        <property name="configLocation" value="classpath:quartz.properties" />  
       <property name="applicationContextSchedulerContextKey"    
           value="applicationContextKey" />    
       <property name="autoStartup" value="true" /> 
    </bean>
</beans>

那么我们的关于job和trigger的信息到哪里去了? 去了数据库,配置如下:

DELETE from qrtz_cron_triggers;
DELETE from qrtz_triggers;
DELETE from qrtz_fired_triggers;
DELETE from qrtz_job_details;
COMMIT;

INSERT INTO `qrtz_job_details` VALUES ('scheduler', 'myJobDetail1', 'DEFAULT', NULL, 'com.quartz.job.Q1', '1', '0', '0', '0', null);
INSERT INTO `qrtz_job_details` VALUES ('scheduler', 'myJobDetail2', 'DEFAULT', NULL, 'com.quartz.job.Q2', '1', '0', '0', '0', null);
INSERT INTO `qrtz_triggers` VALUES ('scheduler', 'my_job1', 'my_group1', 'myJobDetail1', 'DEFAULT', NULL, 
0, 0, 0, 'ACQUIRED', 'CRON', 1470241748000, 0, NULL, 0, null);
INSERT INTO `qrtz_triggers` VALUES ('scheduler', 'my_job1', 'my_group2', 'myJobDetail2', 'DEFAULT', NULL, 
0, 0, 0, 'ACQUIRED', 'CRON', 1470241748000, 0, NULL, 0, null);
INSERT INTO `qrtz_cron_triggers` VALUES ('scheduler', 'my_job1', 'my_group1', '0/5 * * * * ?', 'Asia/Shanghai');
INSERT INTO `qrtz_cron_triggers` VALUES ('scheduler', 'my_job1', 'my_group2', '0/5 * * * * ?', 'Asia/Shanghai');
COMMIT;

这样在启动web项目的时候,Quartz就可以正常工作了.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值