import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.huawei.huaweibus.operate.service.impl.LineOperateServiceImpl;
/**
* 后台定时任务
* @author chenhw
*
*/
public class SimpleJob implements Serializable,Job{
/**
* xuliehua
*/
private static final long serialVersionUID = 1L;
/**
* LOGGER
*/
private static final Logger LOGGER = LoggerFactory.getLogger(LineOperateServiceImpl.class);
/**
* to do
*/
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 统计每日评论任务
* @param arg0 canshu
* @exception JobExecutionException exception
*/
public void execute(JobExecutionContext arg0) throws JobExecutionException {
Date date = new Date();
String now = sdf.format(date);
System.out.println("--------------------- "+now+"开始执行任务 ------------------------------- ");
}
}
<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="commentJobdoTime" /> <ref bean="noticeSpplierQuote" /> <ref bean="busplanTriggerTimer" /> <ref bean="testSimpleJob"/> </list> </property> <!--<property name="applicationContextSchedulerContextKey" value="applicationContextKey"/> --> <property name="configLocation" value="classpath:config/quartz.properties" /> </bean> <!-- 定义调用对象和调用对象的方法 --> <bean id="simpleJob" class="org.springframework.scheduling.quartz.JobDetailBean"> <!-- 调用的类 --> <property name="jobClass" value="com.huawei.huaweibus.job.SimpleJob"/> <!-- 调用类中的方法 <property name="targetMethod"> <value>execute</value> </property>--> </bean> <bean id="testSimpleJob" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="simpleJob" /> </property> <!-- cron表达式 --> <property name="cronExpression"> <value>0 0/1 15 * * ?</value> </property> </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://1.1.1.1/xxx org.quartz.dataSource.myDS.user=postgres org.quartz.dataSource.myDS.password=000000 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
多个tomcat 连接一个数据库后,各tomcat会分别执行,但不会重复执行。关闭一个或多个,其他tomcat会执行。修改配置文件的时间无效,需要删除数据库里的实例:
delete from qrtz_cron_triggers where trigger_name = 'testSimpleJob';
delete from qrtz_triggers where trigger_name = 'testSimpleJob';
delete from qrtz_job_details where job_name = 'simpleJob';
删除quartz 在 mysql 存储的数据 :
delete from qrtz_cron_triggers
delete from qrtz_fired_triggers
delete from qrtz_triggers
delete from qrtz_job_details