1 下载quartz对应版本jar包
2 初始化对应数据库sql(版本需要对应,不然会出现少字段的情况) ,下载地址 https://github.com/quartz-scheduler/quartz/tree/quartz-1.8.x/docs/dbTables
讲sql在数据库中执行,12张表。 其余版本自己在git找。
3.配置文件 quartz.properties这个要配,不然会加载jar包中默认的quartz.properties文件。路径视情况而定,一般配置在classpath下;
#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配置文件
<!-- 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:
public class OpJob extends ScsBaseJob { @Override protected void doExecuteInternal(JobExecutionContext context) { System.out.println("OpSelfcabStationJob.doExecuteInternal()业务方法正在执行+++++++++++++++********************+++++++++++++++******************"); } }
SchedulerFactoryBean:
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); } } }