quartz 定時調度持久化數據庫配置文件

1 下載quartz對應版本jar包java

2 初始化對應數據庫sql(版本須要對應,否則會出現少字段的狀況) ,下載地址  https://github.com/quartz-scheduler/quartz/tree/quartz-1.8.x/docs/dbTablesgit

  講sql在數據庫中執行,12張表。 其他版本本身在git找。github

3.配置文件  quartz.properties這個要配,否則會加載jar包中默認的quartz.properties文件。路徑視狀況而定,通常配置在classpath下;spring

#Created by xiaoshuai 
#2016-6-2 10:37:35
#============================================================================
# Configure JobStore  
#============================================================================
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.isClustered=true
org.quartz.jobStore.maxMisfiresToHandleAtATime=1 
org.quartz.jobStore.misfireThreshold=60000
org.quartz.jobStore.tablePrefix=T_SCS_QRTZ_

#============================================================================
# Configure Main Scheduler Properties  
#============================================================================
org.quartz.scheduler.instanceName=scsSchedule
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.rmi.export=false
org.quartz.scheduler.rmi.proxy=false
org.quartz.scheduler.wrapJobExecutionInUserTransaction=false
org.quartz.scheduler.interruptJobsOnShutdown=true
org.quartz.scheduler.interruptJobsOnShutdownWithWait=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

4.因與spring集成,beans配置文件sql

<!-- JOBdetail -->
   	<bean id="opSelfcabStationJob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="name">
			<value>opSelfcabStationJob</value>
		</property>
		<property name="jobClass">
			<value>cn..scsjob.scheduler.quartz.OpSelfcabStationJob</value>
		</property>
		<property name="jobDataAsMap">
			<map>
				<entry key="jobName">
					<value>opSelfcabStationJob</value>
				</entry>
				<entry key="jobDesc">
					<value>opSelfcabStationJob</value>
				</entry>	
			</map>
		</property>
	</bean>
	   <!-- ======================== 調度觸發器 ======================== -->  
	<bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">  
	    <property name="jobDetail" ref="opSelfcabStationJob"></property>  
	    <property name="cronExpression" value="0/30 * * * * ?"></property>  
	</bean>  
	  
	<!-- ======================== 調度工廠 ======================== -->  
	<bean id="SpringJobSchedulerFactoryBean" class="cn..scsjob.scheduler.quartz.task.ScsInitSchedulerFactoryBean" 
	 		lazy-init="false" autowire="no" destroy-method="destroy"> 
		<property name="configLocation" value="classpath:quartz.properties" /> 
	    <property name="dataSource"> <ref bean="core_oracle_ds_rw" /> </property>
        <property name="applicationContextSchedulerContextKey" value="applicationContext" />
         <!-- 延時啓動,應用先啓動,scheduler在90s後執行--> 
  	    <property name="startupDelay" value="90"/> 
	    <property name="triggers">  
	        <list>  
	            <ref bean="CronTriggerBean"/>  
	        </list>  
	    </property>  
	</bean>    

5.java代碼數據庫

executeInternal()方法執行先後能夠作些日誌記錄工做
public abstract class ScsBaseJob extends QuartzJobBean{
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	protected void executeInternal(JobExecutionContext context)
			throws JobExecutionException {
		logger.info("ScsBaseJob執行 executeInternal() ,Job Start Time : " + new Date());
		this.doExecuteInternal(context);
		logger.info("ScsBaseJob執行 executeInternal() ,Job End Time : " + new Date());
	}
	/**
	 * 
	 * 業務執行方法
	 * @param context
	 */
	protected  abstract void doExecuteInternal(JobExecutionContext context);
}

 具體的業務執行方法job:oracle

public class OpJob extends ScsBaseJob {
	@Override
	protected void doExecuteInternal(JobExecutionContext context) {
		System.out.println("OpSelfcabStationJob.doExecuteInternal()業務方法正在執行+++++++++++++++********************+++++++++++++++******************");
	}

}

  

 SchedulerFactoryBean:app

 

public class ScsInitSchedulerFactoryBean extends SchedulerFactoryBean{
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	public void destroy() {
		logger.info("destroy quartz schedule...");
		
		try {
			this.getScheduler().shutdown();
			super.destroy();
		} catch (SchedulerException e) {
			logger.error(e.getMessage(), e);
		}
		
	}
}
相關文章
相關標籤/搜索