spring4 + quarz2 集群

1下载:

 def springVersion = "4.2.4.RELEASE";

    //core spring  

    compile "org.springframework:spring-context:$springVersion"

    compile "org.springframework:spring-core:$springVersion"

    compile "org.springframework:spring-webmvc:$springVersion"

    compile "org.springframework:spring-aop:$springVersion"

   

   

    compile "org.springframework:spring-expression:$springVersion"

    compile "org.springframework:spring-beans:$springVersion"

    compile "org.springframework:spring-aspects:$springVersion"

compile group: 'org.aopalliance', name: 'com.springsource.org.aopalliance', version: '1.0.0'

compile group: 'org.aspectj', name: 'com.springsource.org.aspectj.weaver', version: '1.6.8.RELEASE'

    

//定时任务集群

compile group: 'org.quartz-scheduler', name: 'quartz', version: '2.2.1'

compile group: 'org.quartz-scheduler', name: 'quartz-jobs', version: '2.2.1'

下载quartz 在docs\dbTables 中可以找到初始化数据库语句。

2.建立定时任务:

package task.spring.test;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.scheduling.quartz.QuartzJobBean;

public class TaskInfoJobEx extends QuartzJobBean{  

    Logger log = LoggerFactory.getLogger(TaskInfoJobEx.class);

@Override

protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

log.info("=========================我的定时任务===========================------------");

}  

}

3.设置spring 配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 配置任务bean类

<bean id="quartzTask" class="com.lqy.spring.task.QuartzTask"></bean>

-->

<!-- 配置方法映射工厂类 -->

<!-- MethodInvokingJobDetailFactoryBean不支持序列化 -->

<!-- <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

<property name="targetObject" ref="quartzTask"></property>

<property name="targetMethod" value="startTask"></property>

<property name="concurrent" value="false"></property>

concurrent : false表示等上一个任务执行完后再开启新的任务

</bean> -->

<!-- 配置方法映射工厂类 -->

<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">

   <property name="jobClass" value="task.spring.test.TaskInfoJobEx"></property>

   <property name="durability" value="true"></property>

   <property name="requestsRecovery" value="true" /> 

</bean>

<!-- 配置任务高度的的时间/周期 -->

<bean id="jobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">

<property name="jobDetail" ref="jobDetail"></property>

<property name="cronExpression" value="0 */1 * * * ?"></property>

<!-- <property name="startDelay" value="3000"></property> -->

</bean>

<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<!-- <property name="dataSource" ref="dataSource"></property>   -->

<!--可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了 -->    

        <property name="overwriteExistingJobs" value="true" />    

         <!--必须的,QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动 -->  

        <property name="startupDelay" value="30" />  

        <!-- 设置自动启动 -->  

        <property name="autoStartup" value="true" />

        <property name="applicationContextSchedulerContextKey" value="applicationContextKey" />  

        <property name="configLocation" value="classpath:spring-quartz.properties" />  

<property name="triggers">

<list>

<ref bean="jobTrigger"/>

</list>

</property>

</bean>

</beans>

4.spring-quartz.properties

#=============================================================  

#Configure Main Scheduler Properties  

#==============================================================   

org.quartz.scheduler.instanceName = defaultScheduler

org.quartz.scheduler.instanceId = AUTO

#==============================================================  

#Configure JobStore  myDS

#============================================================== 

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.isClustered = true

org.quartz.jobStore.clusterCheckinInterval = 20000  

org.quartz.jobStore.dataSource = myDS

org.quartz.jobStore.maxMisfiresToHandleAtATime = 1

org.quartz.jobStore.misfireThreshold = 120000

#\u9632\u6B62\u9AD8\u8D1F\u8F7D\u548C\u573A\u65F6\u95F4\u4E8B\u7269\u65F6\u9501\u8D85\u65F6

org.quartz.jobStore.txIsolationLevelSerializable = true

#==============================================================  

#Configure ThreadPool  

#============================================================== 

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool

##thread count 1-100

org.quartz.threadPool.threadCount = 10

org.quartz.threadPool.threadPriority = 5

org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true

#==============================================================

#Skip Check Update

#update:true

#not update:false

#==============================================================

org.quartz.scheduler.skipUpdateCheck = true 

#============================================================================   

# 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

#============================================================================    

# Configure Datasources      

#============================================================================    

 #数据库配置 

org.quartz.dataSource.myDS.driver =oracle.jdbc.OracleDriver   

org.quartz.dataSource.myDS.URL =jdbc:oracle:thin:@192.168.1.24:1521:dataInstance  

org.quartz.dataSource.myDS.user =shiro    

org.quartz.dataSource.myDS.password =shiro123    

org.quartz.dataSource.myDS.maxConnections = 20  

程序 和 数据库初始语句在附件中.

注意:如果定时任务集群不是在同一台集群时,需要同步服务的时间。

执行结果: 两台机器互斥执行

机器1:

 

机器2:



 

猜你喜欢

转载自gjp014.iteye.com/blog/2364142