三、搭建环境
1、配置Mevan节点,下载Jar包。在pom.xml文件里加上如下代码:
目前最新版本是2.2.1(2016.11)
org.quartz-scheduler
quartz
2.2.1
2、在applicatonContext.xm里增加资源引用加入如下代码:
(quartz.xml为定时任务配置文件,Quartz可以使用配置文件形式管理触发,也可以完全使用Java代码控制触发。使用配置文件好处是可以减少代码开发量,如有修改不需要编译Java类。)
3.编写quartz.xml,每个节点有详细的解释,看代码即可,将其放到src\main\resources
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">
com.myTest.quartz.CommJobBean
4.编写quartz.properties文件。将其放到src\main\resources,如果每有特别的需求只需要更改数据库连接属性就可以了。
因为要部署到集群当中,一套服务程序要部署到多态服务器上,所以定时更新时容易造成冲突。比如,服务器1开始了定时任务TASK1,但是服务器2也开始了定时任务TASK1,这就造成了任务冲突,为了解决这个问题Quartz引入了几张数据表作为执行标记。
大家可以去Quartz管网下载数据库结构Sql脚本,执行一下建表就可以。目前最新版本是2.2.3。下面是下载地址
http://www.quartz-scheduler.org/downloads/
有其他博主研究了quartz.properties中的各个属性,下面是地址。
http://blog.youkuaiyun.com/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
packagecom.inspur.ljts.quartz;importjava.lang.reflect.Method;importorg.quartz.JobExecutionContext;importorg.quartz.JobExecutionException;importorg.springframework.context.ApplicationContext;importorg.springframework.scheduling.quartz.QuartzJobBean;/*** @ClassName CommJobBean
* @Description 公用任务*@version1.0*/
public class CommJobBean extendsQuartzJobBean {privateString targetObject;privateString targetMethod;privateApplicationContext ctx;
@Overrideprotected void executeInternal(JobExecutionContext context) throwsJobExecutionException {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 newJobExecutionException(e);
}
}public voidsetApplicationContext(ApplicationContext applicationContext) {this.ctx =applicationContext;
}public voidsetTargetObject(String targetObject) {this.targetObject =targetObject;
}public voidsetTargetMethod(String targetMethod) {this.targetMethod =targetMethod;
}
}
6.编写具体业务类CommJob.java
packagecom.xxx.task;importjava.io.UnsupportedEncodingException;importjava.text.DateFormat;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.List;importjava.util.Map;importorg.quartz.JobExecutionContext;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;importcom.alibaba.rocketmq.client.exception.MQBrokerException;importcom.alibaba.rocketmq.client.exception.MQClientException;importcom.alibaba.rocketmq.common.message.Message;importcom.alibaba.rocketmq.remoting.exception.RemotingException;importcom.fasterxml.jackson.core.JsonProcessingException;importcom.xxx.comm.base.ICommService;importcom.xxx.comm.base.SjcqException;importcom.xxx.comm.consts.CommConsts;importcom.xxx.comm.context.SjcqContext;importcom.xxx.comm.sjcqTask.bean.SjcqTask;importcom.xxx.comm.sjcqTask.service.SjcqTaskService;importcom.xxx.comm.utils.JsonUtil;/*** @ClassName TaskJob
* @Description 数据抽取定时任务*@version1.0*/@Component
@SuppressWarnings("rawtypes")public classCommJob {public voidsjcq() {
System.out.println("Hello World!!!");
}
}
7.将项目发布部署到Tomcat,启动Tomcat,定时任务就会自动运行了。完毕!!!!