quartz 学习整理
内容转载自 https://www.cnblogs.com/yinyi521/p/6054140.html
简单实例 https://www.cnblogs.com/shizhijie/p/8243934.html
- Quartz 2.2.1
Quartz是一个完全由java编写的开源作业调度框架。简单地创建一个实现org.quartz.Job接口的java类。Job接口包含唯一的方法:
public void execute(JobExecutionContext context)throws JobExecutionException;
在你的Job接口实现类里面,添加一些逻辑到execute()方法。一旦你配置好Job实现类并设定好调度时间表,Quartz将密切注意剩余时间。当调度程序确定该是通知你的作业的时候,Quartz框架将调用你Job实现类(作业类)上的execute()方法并允许做它该做的事情。无需报告任何东西给调度器或调用任何特定的东西。仅仅执行任务和结束任务即可。如果配置你的作业在随后再次被调用,Quartz框架将在恰当的时间再次调用它。
需要jar包 maven
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency>
2、在applicatonContext.xml里增加单独quartz.xml
<import resource="quartz.xml"/>
3.编写quartz.xml,每个节点有详细的解释,看代码即可,将其放到src\main\resources
<?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文件。将其放到src\main\resources,如果每有特别的需求只需要更改数据库连接属性就可以了。
因为要部署到集群当中,一套服务程序要部署到多态服务器上,所以定时更新时容易造成冲突。比如,服务器1开始了定时任务TASK1,但是服务器2也开始了定时任务TASK1,这就造成了任务冲突,为了解决这个问题Quartz引入了几张数据表作为执行标记。
大家可以去Quartz管网下载数据库结构Sql脚本,执行一下建表就可以。目前最新版本是2.2.3。下面是下载地址
http://www.quartz-scheduler.org/downloads/
#==============================================================
#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
1 package com.inspur.ljts.quartz; 2 3 import java.lang.reflect.Method; 4 5 import org.quartz.JobExecutionContext; 6 import org.quartz.JobExecutionException; 7 import org.springframework.context.ApplicationContext; 8 import org.springframework.scheduling.quartz.QuartzJobBean; 9 10 /** 11 * @ClassName CommJobBean 12 * @Description 公用任务 13 * @version 1.0 14 */ 15 public class CommJobBean extends QuartzJobBean { 16 17 private String targetObject; 18 private String targetMethod; 19 private ApplicationContext ctx; 20 21 @Override 22 protected void executeInternal(JobExecutionContext context) throws JobExecutionException { 23 24 try { 25 Object otargetObject = ctx.getBean(targetObject); 26 Method m = null; 27 28 try { 29 m = otargetObject.getClass().getMethod(targetMethod); 30 m.invoke(otargetObject); 31 } catch (SecurityException e) { 32 e.printStackTrace(); 33 } catch (NoSuchMethodException e) { 34 e.printStackTrace(); 35 } 36 } catch (Exception e) { 37 throw new JobExecutionException(e); 38 } 39 40 } 41 42 public void setApplicationContext(ApplicationContext applicationContext) { 43 this.ctx = applicationContext; 44 } 45 46 public void setTargetObject(String targetObject) { 47 this.targetObject = targetObject; 48 } 49 50 public void setTargetMethod(String targetMethod) { 51 this.targetMethod = targetMethod; 52 } 53 54 }
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,定时任务就会自动运行了。完毕!!!!