完整的定时任务解决方案Spring集成+定时任务本身管理+DB持久化+集群

Posted 太白的技术博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完整的定时任务解决方案Spring集成+定时任务本身管理+DB持久化+集群相关的知识,希望对你有一定的参考价值。

完整的定时任务解决方案Spring集成+定时任务本身管理+DB持久化+集群

maven依赖

<dependency>

<groupId>org.quartz-scheduler</groupId>

<artifactId>quartz</artifactId>

<version>2.2.1</version>

</dependency>

<dependency>

<groupId>open-source.missing.com.oracle</groupId>

<artifactId>ojdbc6</artifactId>

<version>11.2.0.3.0</version>

</dependency>

<dependency>

<groupId>commons-dbcp</groupId>

<artifactId>commons-dbcp</artifactId>

<version>1.4</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>3.2.16.RELEASE</version>

</dependency>

 

执行从官网下载的gz文件中的sql语句(http://www.quartz-scheduler.org/),注意使用的jar版本和sql脚本必须保持一致。不然会出现各种奇葩问题。比如启动程序后,定时任务不执行。

 

quartz.properties配置

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX

org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate

org.quartz.jobStore.tablePrefix = QRTZ_

org.quartz.jobStore.dataSource = myDS

org.quartz.jobStore.isClustered=true

org.quartz.jobStore.clusterCheckinInterval = 60000   //集群模式下检测节点失败的轮询间隔,默认15秒。如果定时间隔为10秒,检测间隔为60秒,如果从节点失败到检测到过去了45秒,则检测到那一刻,会同时触发5个实例,这里一定要注意。

# org.quartz.plugins.history.LoggingJobHistoryPlugin

# org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingTriggerHistoryPlugin

# org.quartz.plugin.triggHistory.triggerFiredMessage = Trigger \{1\}.\{0\} fired job \{6\}.\{5\} at: \{4, date, HH:mm:ss MM/dd/yyyy}  

# org.quartz.plugin.triggHistory.triggerCompleteMessage = Trigger \{1\}.\{0\} completed firing job \{6\}.\{5\} at \{4, date, HH:mm:ss MM/dd/yyyy\}.

org.quartz.scheduler.instanceName = MyClusteredScheduler

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool    

org.quartz.threadPool.threadCount = 5   

org.quartz.threadPool.threadPriority = 5

org.quartz.scheduler.instanceId=AUTO

org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin  

org.quartz.plugin.shutdownhook.cleanShutdown = true

 

org.quartz.dataSource.myDS.driver = oracle.jdbc.OracleDriver   

org.quartz.dataSource.myDS.URL = jdbc:oracle:thin:@172.23.11.49:1522:fcdb   

org.quartz.dataSource.myDS.user = hs_fund   

org.quartz.dataSource.myDS.password = Hundsun123   

org.quartz.dataSource.myDS.maxConnections = 5

org.quartz.dataSource.myDS.validationQuery=select 1 from dual

org.quartz.dataSource.myDS.validateOnCheckout=true

 

spring配置文件

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

  <property name="username" value="hs_fund"></property>

  <property name="password" value="Hundsun123"></property>

  <property name="url" value="jdbc:oracle:thin:@172.23.11.49:1522:fcdb"></property>

  <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>

  <property name="maxActive" value="10"></property>

  <property name="maxIdle" value="10"></property>

  <property name="minIdle" value="10"></property>

  <property name="maxWait" value="10000"></property>

  <property name="poolPreparedStatements" value="true" />

      <property name="maxOpenPreparedStatements" value="50" />

</bean>

 

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

<property name="dataSource">

<ref bean="dataSource"/>

</property>

<property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>

<!--applicationContextSchedulerContextKey: 是org.springframework.scheduling.quartz.SchedulerFactoryBean这个类中把spring上下 文以key/value的方式存放在了quartz的上下文中了,可以用applicationContextSchedulerContextKey所定义的key得到对应的spring上下文-->

<property name="triggers">

<list>

<ref bean="cronTrigger"/>

<!-- <ref bean="simpleTrigger"/> -->

</list>

</property>

<property name="configLocation" value="classpath:quartz.properties"/>

<!--configLocation:用于指明quartz的配置文件的位置 -->

</bean>

 

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">

<property name="jobDetail" ref="exampleJob"/>

<!-- run every morning at 6 AM -->

<property name="cronExpression" value="*/10 * * * * ?"/>

</bean>

 

<bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">

<property name="jobClass" value="examples.ExampleJob"/>

<property name="durability" value="true" />

<property name="requestsRecovery" value="true" />

<property name="jobDataAsMap">

<map>

<entry key="timeout" value="5"/>

</map>

</property>

</bean>

 

examples.ExampleJob

/**  

* @Title: ExampleJob.java

* @Package example

* @Description: TODO(用一句话描述该文件做什么)

* @author [email protected]  

* @date 2016年3月11日 上午11:48:28

* @version V1.0  

*/

package examples;

 

import java.text.SimpleDateFormat;

import java.util.Date;

 

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.springframework.scheduling.quartz.QuartzJobBean;

 

/**

 * @author zjhua

 *

 */

public class ExampleJob extends QuartzJobBean {

private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");

private int timeout;

/**

* Setter called after the ExampleJob is instantiated

* with the value from the JobDetailFactoryBean (5)

*/

public void setTimeout(int timeout) {  //会自动从bean配置中注入

this.timeout = timeout;

}

protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {

String dt = sdf.format(new Date());

System.out.println(ctx.getJobDetail().getJobDataMap().get("timeout"));  //会自动从bean配置中设置进来

System.out.println(dt + "ExampleJob===========");

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(dt + "ExampleJob定时任务结束");

}

}

 

//测试类

/**  

 * @Title: SpringTest.java

 * @Package com.cyl

 * @Description: TODO(用一句话描述该文件做什么)

 * @author [email protected]  

 * @date 2016年3月10日 上午9:12:27

 * @version V1.0  

 */

package com.cyl;

 

import javax.sql.DataSource;

 

import org.quartz.JobKey;

import org.quartz.Scheduler;

import org.quartz.SchedulerException;

import org.quartz.impl.StdSchedulerFactory;

import org.quartz.impl.matchers.GroupMatcher;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.scheduling.quartz.SchedulerFactoryBean;

 

/**

 * @author zjhua

 *

 */

public class SpringTest {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext(

new String[] { "services.xml" });

/*

TaskExecutorExample taskExecutor = context

.getBean(com.cyl.TaskExecutorExample.class);

taskExecutor.printMessages();

DataSource dataSource = context.getBean(DataSource.class);

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

long begin = System.currentTimeMillis();

for (int i=0;i<2000;i++) {

int countOfActorsNamedJoe = jdbcTemplate.queryForObject("select count(*) from t1 where a = ?",Integer.class,new String[] {"1"});

System.out.print(countOfActorsNamedJoe);

}

System.out.println("");

long end = System.currentTimeMillis();

System.out.println(end-begin);

*/

SchedulerFactoryBean bean = context.getBean(SchedulerFactoryBean.class);

 

try {

//Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

//System.out.println(scheduler.getSchedulerName());

System.out.println(bean.getScheduler().getSchedulerName());

for(String group : bean.getScheduler().getJobGroupNames()) {

GroupMatcher<JobKey> groupMatcher = GroupMatcher.groupEquals(group);

for(JobKey jobKey : bean.getScheduler().getJobKeys(groupMatcher)) {

        System.out.println("Found job identified by: " + jobKey);

    }

}

try {

//Thread.sleep(60);

//System.out.println("定时任务暂停");

//bean.getScheduler().pauseAll();  //可以scheduler级别、触发器级别、任务级别,可以查询状态等各种信息

Thread.sleep(60);

//bean.getScheduler().resumeAll();

//System.out.println("定时任务重新开启");

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

//Thread.sleep(20000);

//scheduler.shutdown();

//System.out.println(scheduler.getSchedulerName() + "已关闭!");

} catch (SchedulerException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

 

其他需要注意的点:更改了quartz.properties或者spring配置文件,重启时,现有数据库中的quartz任务定义不会被更新,如果希望更新它们的话,需要通过quartz API进行更新,或者重建quartz表并重启应用。

 

其他定时任务以及触发器操作

调度器暂停

bean.getScheduler().pauseAll();

删除触发器

bean.getScheduler().unscheduleJob(new TriggerKey("cronTrigger","DEFAULT"));

删除任务

bean.getScheduler().deleteJob(new JobKey("exampleJob","DEFAULT"));

创建任务

JobDataMap jobDataMap = new JobDataMap();

jobDataMap.put("timeout", 5);

JobDetail jd = JobBuilder.newJob(examples.ExampleJob.class)

.setJobData(jobDataMap).withIdentity(new JobKey("exampleJob","DEFAULT")).requestRecovery(true).storeDurably(true).build();

创建触发器

CronTriggerImpl trigger = new CronTriggerImpl();

try {

trigger.setCronExpression("*/20 * * * * ?");

trigger.setKey(new TriggerKey("cronTrigger","DEFAULT"));

 

开始调度触发器

bean.getScheduler().scheduleJob(jd,trigger);

 

知道上述API之后,就可以在spring的配置文件中不包含任何的trigger以及jobdetail定义,而是根据数据库中的配置进行动态管理(当然,我们也是采用这种方法)。

以上是关于完整的定时任务解决方案Spring集成+定时任务本身管理+DB持久化+集群的主要内容,如果未能解决你的问题,请参考以下文章

如何用shell脚本定时启动Tomcat服务

SpringBoot入门 定时任务

Spring Boot 入门:集成Quartz定时任务

Spring Boot集成quartz实现定时任务并支持切换任务数据源

Spring和Quarz集成,实现定时任务

Spring集成Quartz定时任务