spring-context.xml:
<!-- 定时任务触发器 --> <bean id="commentJobQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false" autowire="no"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="triggers"> <list> <!-- 触发器列表 --> <ref bean="simpleJobTask" /> </list> </property> <property name="configLocation" value="classpath:config/quartz.properties" /> </bean> <!-- 简单定时任务 --> <bean id="simpleJobTask" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="simpleJob" /> </property> <property name="startDelay"> <value>100</value> </property> <!-- cron表达式 示例:0 31 15 ? * * 每天15点31分触发 --> <property name="cronExpression"> <value>0 31 15 ? * * </value> </property> </bean> <bean id="simpleJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <!-- 调用的类 --> <property name="jobClass" value="com.company.task.SimpleJob"/> </bean>
quartz.properties:
#org.quartz.scheduler.instanceName = Mscheduler org.quartz.scheduler.instanceId=AUTO #============================================================================ # Configure ThreadPool #============================================================================ orgorg.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount=3 org.quartz.threadPool.threadPriority=5 #============================================================================ # Configure JobStore #============================================================================ org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreCMT #orgorg.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate org.quartz.jobStore.useProperties=true org.quartz.jobStore.dataSource=myDS org.quartz.jobStore.tablePrefix=QRTZ_ org.quartz.jobStore.isClustered=false org.quartz.jobStore.maxMisfiresToHandleAtATime=1 #============================================================================ # Configure Datasources #============================================================================ #org.quartz.dataSource.myDS.driver=org.postgresql.Driver #org.quartz.dataSource.myDS.URL=jdbc:postgresql://10.10.33.46/huaweibus #org.quartz.dataSource.myDS.user=postgres #org.quartz.dataSource.myDS.password=000000 #org.quartz.dataSource.myDS.maxConnections=5 org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/carsellnet org.quartz.dataSource.myDS.user=root org.quartz.dataSource.myDS.password=123 org.quartz.dataSource.myDS.maxConnections=5 #============================================================================ # Configure Plugins #============================================================================ #orgorg.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin #orgorg.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin #org.quartz.plugin.jobInitializer.fileNames = jobs.xml #org.quartz.plugin.jobInitializer.overWriteExistingJobs = true #org.quartz.plugin.jobInitializer.failOnFileNotFound = true #org.quartz.plugin.jobInitializer.scanInterval = 10 #org.quartz.plugin.jobInitializer.wrapInUserTransaction = false
定时任务测试:
package com.company.task;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SimpleJob implements Job{
public void execute(JobExecutionContext cxt) throws JobExecutionException {
LOGGER.info("SimpleJob is starting ....");
try{
System.out.println("SimpleJob is doing .... ");
}catch(Exception e){
LOGGER.error(e.getMessage(),e);
}
}
/**
* TODO
*/
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleJob.class);
}
集成之后需要用到数据库的quartz表,如果没有会报错找不到表。
quartz-1.8.6.tar.gz 里的docs dbTables 里面有各种数据库的建表语句。