quartz mysql例子_Quartz定时任务简单实例

本文详细介绍了如何在Spring中配置和使用Quartz实现分布式定时任务,包括配置Maven依赖、设置quartz.xml和quartz.properties文件、编写JobBean以及具体的业务类。在Quartz.properties中配置了数据库连接和集群相关参数,确保在多服务器部署时避免任务冲突。最后,通过发布到Tomcat并启动,定时任务会自动运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

三、搭建环境

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/

f4bbd99068ff1c4ea2116f8016baca64.png

有其他博主研究了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,定时任务就会自动运行了。完毕!!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值