java spring 的quartz任务 和 集群

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值