文章纲要:
初步搭建一个由Quartz为引擎集群的定时任务模块,功能为每隔30秒打印一条信息(Hello World!!!)
一、环境
- Spring MVC
- Mevan
- Quartz 2.2.1
二、简介
Quartz是一个完全由java编写的开源作业调度框架。不要让作业调度这个术语吓着你。尽管Quartz框架整合了许多额外功能, 但就其简易形式看,你会发现它易用得简直让人受不了!。简单地创建一个实现org.quartz.Job接口的java类。Job接口包含唯一的方法:
public void execute(JobExecutionContext context)throws JobExecutionException;
三、搭建环境
1、配置Mevan节点,下载Jar包。在pom.xml文件里加上如下代码:
目前最新版本是2.2.1(2016.11)
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency>
2、在applicatonContext.xm里增加资源引用加入如下代码:
(quartz.xml为定时任务配置文件,Quartz可以使用配置文件形式管理触发,也可以完全使用Java代码控制触发。使用配置文件好处是可以减少代码开发量,如有修改不需要编译Java类。)
<import resource="quartz.xml"/>
3.编写quartz.xml,每个节点有详细的解释,看代码即可,将其放到srcmain esources
<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 将任务加载到quartz配置中 --> <bean id="taskJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <!-- 任务完成之后是否依然保留到数据库,默认false --> <property name="durability" value="true" /> <property name="requestsRecovery" value="true" /> <!-- 任务的实现类,必须 --> <property name="jobClass"> <value>com.myTest.quartz.CommJobBean</value> </property> <!-- 用来给作业提供数据支持的数据结构 --> <property name="jobDataAsMap"> <map> <entry key="targetObject" value="commJob" /><!-- 调用的类 --> <entry key="targetMethod" value="sjcq" /><!-- 调用类中的方法 --> </map> </property> <property name="description" value="通用信息任务" /> </bean> <!-- 定义触发时间 --> <bean id="taskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!-- 触发器与任务绑定 --> <property name="jobDetail" ref="taskJob" /> <property name="cronExpression" value="0/30 * * * * ?" /><!-- 规则表达式 每隔30秒执行一次 --> </bean> <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --> <bean id="taskScheduler" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy"> <property name="overwriteExistingJobs" value="true" /> <!-- 启动延时 --> <property name="startupDelay" value="5" /> <!-- 是否自动启动 --> <property name="autoStartup" value="true" /> <!-- 启动的定时器 --> <property name="triggers"> <list> <ref bean="taskTrigger" /> </list> </property> <property name="applicationContextSchedulerContextKey" value="applicationContext" /> <property name="configLocation" value="classpath:quartz.properties" /> </bean> </beans>
4.编写quartz.properties文件。将其放到srcmain esources,如果每有特别的需求只需要更改数据库连接属性就可以了。
因为要部署到集群当中,一套服务程序要部署到多态服务器上,所以定时更新时容易造成冲突。比如,服务器1开始了定时任务TASK1,但是服务器2也开始了定时任务TASK1,这就造成了任务冲突,为了解决这个问题Quartz引入了几张数据表作为执行标记。
大家可以去Quartz管网下载数据库结构Sql脚本,执行一下建表就可以。目前最新版本是2.2.3。下面是下载地址
http://www.quartz-scheduler.org/downloads/
有其他博主研究了quartz.properties中的各个属性,下面是地址。
http://blog.csdn.net/yixiaoping/article/details/10476817
#==============================================================
#Configure Main Scheduler Properties
#==============================================================
org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.instanceId = AUTO
#==============================================================
#Skip Check Update
#update:true
#not update:false
#==============================================================
org.quartz.scheduler.skipUpdateCheck = true
#==============================================================
#Configure ThreadPool
#==============================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
#==============================================================
#Configure JobStore
#==============================================================
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.maxMisfiresToHandleAtATime = 1
org.quartz.jobStore.misfireThreshold = 120000
org.quartz.jobStore.txIsolationLevelSerializable = true
org.quartz.jobStore.selectWithLockSQL = SELECT * FROM {0}LOCKS WHERE LOCK_NAME = ? FOR UPDATE
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
#===========================================================
# Configure Datasources
#===========================================================
#org.quartz.dataSource.myDS.driver = oracle.jdbc.driver.OracleDriver
#org.quartz.dataSource.myDS.URL = jdbc:oracle:thin:@10.19.22.79:1521:ljts
org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver #注意:这里改成自己的数据库驱动
org.quartz.dataSource.myDS.URL = jdbc:mysql://10.19.22.53:3306/test_db #注意:这里改成自己的数据库地址
org.quartz.dataSource.myDS.user = root #注意:这里改成自己的数据库用户名
org.quartz.dataSource.myDS.password = root #注意:自己的数据库密码
org.quartz.dataSource.myDS.maxConnections = 100
org.quartz.dataSource.myDS.validationQuery=select 1 from dual
#==============================================================
# Configure Plugins
#==============================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true
5编写CommJobBean.java
package com.inspur.ljts.quartz; import java.lang.reflect.Method; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.context.ApplicationContext; import org.springframework.scheduling.quartz.QuartzJobBean; /** * @ClassName CommJobBean * @Description 公用任务 * @version 1.0 */ public class CommJobBean extends QuartzJobBean { private String targetObject; private String targetMethod; private ApplicationContext ctx; @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { try { Object otargetObject = ctx.getBean(targetObject); Method m = null; try { m = otargetObject.getClass().getMethod(targetMethod); m.invoke(otargetObject); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } catch (Exception e) { throw new JobExecutionException(e); } } public void setApplicationContext(ApplicationContext applicationContext) { this.ctx = applicationContext; } public void setTargetObject(String targetObject) { this.targetObject = targetObject; } public void setTargetMethod(String targetMethod) { this.targetMethod = targetMethod; } }
6.编写具体业务类CommJob.java
package com.xxx.task; import java.io.UnsupportedEncodingException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Map; import org.quartz.JobExecutionContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.alibaba.rocketmq.client.exception.MQBrokerException; import com.alibaba.rocketmq.client.exception.MQClientException; import com.alibaba.rocketmq.common.message.Message; import com.alibaba.rocketmq.remoting.exception.RemotingException; import com.fasterxml.jackson.core.JsonProcessingException; import com.xxx.comm.base.ICommService; import com.xxx.comm.base.SjcqException; import com.xxx.comm.consts.CommConsts; import com.xxx.comm.context.SjcqContext; import com.xxx.comm.sjcqTask.bean.SjcqTask; import com.xxx.comm.sjcqTask.service.SjcqTaskService; import com.xxx.comm.utils.JsonUtil; /** * @ClassName TaskJob * @Description 数据抽取定时任务 * @version 1.0 */ @Component @SuppressWarnings("rawtypes") public class CommJob { public void sjcq() { System.out.println("Hello World!!!"); } }
7.将项目发布部署到Tomcat,启动Tomcat,定时任务就会自动运行了。完毕!!!!