Spring quartz集群配置

9 篇文章 0 订阅

quartz单机模式:http://blog.csdn.net/itjavaer/article/details/77923965


quartz集群要依赖数据库的,不同版本表可能会不一样,建表语句在下载的安装包里的docs\dbTables,都是qrtz_开头的


建完表之后看下qrtz_locks表,这个表里有没有数据,如果没有就手动加上,不然可能报错

insert into `qrtz_locks` (`LOCK_NAME`) values('CALENDAR_ACCESS');
insert into `qrtz_locks` (`LOCK_NAME`) values('JOB_ACCESS');
insert into `qrtz_locks` (`LOCK_NAME`) values('MISFIRE_ACCESS');
insert into `qrtz_locks` (`LOCK_NAME`) values('STATE_ACCESS');
insert into `qrtz_locks` (`LOCK_NAME`) values('TRIGGER_ACCESS');


创建集群的配置文件 quartz.properties

org.quartz.scheduler.instanceName = quartzScheduler
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 = 1
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
org.quartz.jobStore.misfireThreshold = 60000
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
 

#============================================================================
# Configure Main Scheduler Properties  
#============================================================================
org.quartz.scheduler.instanceId = AUTO

#============================================================================
# Configure JobStore  
#============================================================================
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 = myDS
org.quartz.jobStore.tablePrefix = qrtz_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000

org.quartz.threadPool.threadCount = 1    线程设置成1是为了保证不并发执行,单机模式下设置只要设置<property name="concurrent" value="false" />就行了,但是集群不能设置这个属性,如果有更好的办法请告诉我。


创建spring-quartz.xml

<?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:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p"  
 	xmlns:ws="http://jax-ws.dev.java.net/spring/core" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans   
          http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
          http://www.springframework.org/schema/context   
          http://www.springframework.org/schema/context/spring-context-4.0.xsd   
          http://www.springframework.org/schema/aop   
          http://www.springframework.org/schema/aop/spring-aop-4.0.xsd   
          http://www.springframework.org/schema/tx    
          http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
          http://jax-ws.dev.java.net/spring/core
          http://jax-ws.dev.java.net/spring/core.xsd">
          
    

	<bean name="testTask" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="name" value="testTask"></property>  
        <property name="jobClass" value="com.orange.task.TestTask"></property>  
	</bean>  
	
	<bean id="testTriggerBean"  class="org.springframework.scheduling.quartz.CronTriggerBean">  
	    <property name="jobDetail">  
	        <ref bean="testTask" />  
	    </property>  

	    <property name="cronExpression">  
	        <value>0 * * * * ?</value>  
	    </property>  
	</bean>  
	    
    <bean id = "quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">  
            <list>  
	            <ref bean="testTriggerBean" /> 
            </list>  
        </property>  
         <property name="configLocation" value="classpath:quartz.properties" />  
         <property name="dataSource" ref ="dataSourceQuartz" />
    </bean>  
</beans>

id = "quartzScheduler" 要和quartz.properties文件中的org.quartz.scheduler.instanceName 一样

配置数据源,我是写在了spring-mybatis.xml

<bean name="dataSourceQuartz" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="url" value="jdbc:mysql://localhost:3306/quartz?characterEncoding=UTF-8" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="initialSize" value="10" />
		<property name="maxActive" value="30" />
		<property name="maxIdle" value="20" />
		<property name="minIdle" value="1" />
		<property name="maxWait" value="60000" />
		<property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" />
		<property name="validationQuery" value="${validationQuery}" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="testWhileIdle" value="true" />
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<property name="minEvictableIdleTimeMillis" value="10000" />
		<property name="removeAbandoned" value="false" />
		<property name="removeAbandonedTimeout" value="1800" />
		<property name="logAbandoned" value="true" />
	</bean> 

 

最后在spring.xml引入spring-quartz.xml

<import resource="classpath:spring-quartz.xml"/>

创建任务类,要实现Job接口

package com.orange.task;  
import com.orange.service.TestService;  
  
public class TestTask implements Job{  
    @Autowired  
    private TestService testService;  
     
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {  
        System.out.println(date);  
        testService.test(date);  
    }  
}  


启动两次程序,两个程序不会同时运行一个任务(可能交替,也能连续),一个程序挂了,另一个程序会继续运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值