quartz有两种注入方式,MethodInvokingJobDetailFactoryBean和JobDetailBean。
这里我用的是JobDetailBean。(MethodInvokingJobDetailFactoryBean也试了下,无奈不成功,看网上有人说重写两个类文件,试了下也不行,只好用JobDetailBean了)
1.下载quartz-1.8.6包,包的的docs文件夹里有数据库建表sql,quartz集群需要将任务信息实例化到数据库中,然后各个节点从库中读取任务信息。
2.在src中添加quartz.properties文件。
org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.selectWithLockSQL=SELECT * FROM {0}LOCKS UPDLOCK WHERE LOCK_NAME \= ?
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = quartzdataSource
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
3.src中添加applicationContext-Quartz.xml文件,内容如下,quartz自带的连接池是DBCP,这个连接池问题很多性能也不好,所以改成了c3p0
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="syncSpOriinfoService" class="com.jxet.quartz.service.sms.SyncSpOriinfoService" />
<bean id="syncSpOriinfoBean" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.jxet.quartz.client.SyncSpOriinfoServiceClient
</value>
</property>
<!--采用jobDataAsMap方式进行quartzService注入 -->
<property name="jobDataAsMap">
<map>
<entry key="targetObject" value="syncSpOriinfoService" />
<entry key="targetMethod" value="syncSpOriinfo" />
</map>
</property>
</bean>
<bean id="syncSpOriinfoTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="syncSpOriinfoBean" />
</property>
<!-- 程序启动10秒后运行 -->
<property name="startDelay">
<value>10000</value>
</property>
<!-- 1分钟启动一次 -->
<property name="repeatInterval">
<value>60000</value>
</property>
</bean>
<bean id="quartzdataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- c3p0连接线程池配置文件 --> <property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="jdbcUrl" value="jdbc:sqlserver://192.168.32.160:1433;databaseName=quartz" />
<property name="user" value="sa" />
<property name="password" value="123" />
</bean>
<bean id="timerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource">
<ref bean="quartzdataSource" /> <!--Spring中对于的数据源-->
</property>
<property name="configLocation" value="classpath:quartz.properties"/>
<property name="triggers">
<list>
<ref bean="syncSpOriinfoTrigger" />
</list>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContext" />
</bean>
</beans>
4.根据 QuartzJobBean 来重写一个自己的类,然后使用 SPRING 把这个重写的类注入 appContext 中后,再使用 AOP 技术反射出原有的 quartzJobx( 就是开发人员原来已经做好的用于执行 QUARTZ 的 JOB 的执行类 ) 。
public class SyncSpOriinfoServiceClient extends QuartzJobBean {
private final Log log = LogFactory.getLog(SyncSpOriinfoServiceClient.class);
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
try {
log.info("execute [" + targetObject + "] at once>>>>>>");
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml","classpath:applicationContext-quartz.xml"});
Object otargetObject = ctx.getBean(targetObject);
Method m = null;
try {
m = otargetObject.getClass().getMethod(targetMethod, new Class[]{});
m.invoke(otargetObject, new Object[]{});
}
catch (SecurityException e) {
log.error(e);
}
catch (NoSuchMethodException e) {
log.error(e);
}
}
catch (Exception e) {
throw new JobExecutionException(e);
}
}
private String targetObject;
private String targetMethod;
public void setTargetObject(String targetObject) {
this.targetObject = targetObject;
}
public void setTargetMethod(String targetMethod) {
this.targetMethod = targetMethod;
}
}
5.写业务service(syncSpOriinfoService),然后启动就ok了
需要注意的地方是,程序第一次启动时,会将任务信息实例化到数据库中,以后修改任务信息必须修改数据库中的任务,直接修改项目中的信息是没有用的。看来以后还得整个项目来维护了.
分享到:
相关推荐
java + quartz实现定时任务,实现集群配置,在集群环境下多节点运行定时Quartz定任务,就会存在重复处理任务的现象,为解决这一问题,下面我将介绍使用 Quartz 的 TASK ( 12 张表)实例化到数据库,基于数据库自动...
Quartz集群配置[定义].pdf
基于内存的Quartz集群配置手册 很讨厌、鄙视那种把官方资料贴出来的人。 到网上搜索就是要快速入门、使用。 以上
NULL 博文链接:https://daoshud1.iteye.com/blog/1955099
NULL 博文链接:https://vista-move.iteye.com/blog/2274246
Quartz,集群,配置,源码,多线程 已经打包成jar包,解压即用,十分方便。 有问题请联系QQ:9710846
spring 集成 quartz ,支持集群,job支持spring@Autowired注入及@Resource注入
OpenSymphony所提供的Quartz自2001年发布版本以来已经被众多项目作为任务调度的解决方案,Quartz在提供巨大灵活性的同时并未牺牲其简单性,它所提供的强大功能使你可以应付绝大多数的调度需求。Quartz 在开源任务...
Quartz,集群,配置,源码,多线程
spring-cloud-alibaba+dubbo+nacos+quartz以yml配置方式实现
本文章是关于springboot集成quartz集群的步骤,LZ亲测。
springboot 集成 quartz 集群 加 sql 等 文章介绍 单机加集群都有 https://blog.csdn.net/weixin_42749765/article/details/88532413
最近公司项目上线,需要把app部署在多台服务器上,但只能让其中一台服务器的job执行,一台...按网上的方法,配置了很多遍,都不成功,我这个保证你成功运行。 运行工程前需求初使化12张表,见工程下tables_oracle.sql
对quartz的集群安装部署提供帮助,可以使其在数据库层面保持仅有一个任务执行
NULL 博文链接:https://fanshuyao.iteye.com/blog/2392350
spring 集成quartz定时任务 用数据库实现quartz的集群
Quartz是Java领域最著名的开源任务调度工具。Quartz提供了极为广泛的特性如持久化任务,集群和分布式任务等,下面这篇文章主要给大家介绍了关于Quartz集群原理以及配置应用的相关资料,需要的朋友可以参考下
springboot+quartz+mybatis:通过页面配置方式,动态配置dubbo job和http job,实现调度与业务完全解耦