Spring4 多种定时器详解

注意:spring4已经不推荐org.springframework.scheduling.timer.ScheduledTimerTask这个类,所以不能用spring3以前的timerTask方法。现spring4定时器方法推荐一下两只方法:(需要单独导入quartz包,只能是1.8.+)

spring代码如下:

<span style="white-space:pre">	</span><!-- 方式1 -->
	<bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass" value="com.spring.task.TaskOne" />
		<property name="jobDataAsMap">
			<map>
				<entry key="timeout" value="5" />
			</map>
		</property>
	</bean>
	<bean id="cronTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="exampleJob" />
		<!-- run every morning at 6 AM -->
		<!-- <property name="cronExpression" value="0 0 6 * * ?" /> -->
		<!-- <property name="cronExpression" value="0 0/1 * * * ?" /> --> <!-- 每分钟 -->
		<property name="cronExpression" value="0/2 * * * * ?" /> <!-- 每秒 -->
	</bean>

	<!-- 方式2 -->
	<bean id="exampleBusinessObject" class="com.spring.task.TaskTwo" />
	<bean id="jobDetail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="exampleBusinessObject" />
		<property name="targetMethod" value="doIt" />
		<property name="concurrent" value="false" />
	</bean>
	<bean id="simpleTrigger"
		class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
		<!-- see the example of method invoking job above -->
		<property name="jobDetail" ref="jobDetail" />
		<!-- 10 seconds -->
		<property name="startDelay" value="5000" />
		<!-- repeat every 50 seconds -->
		<property name="repeatInterval" value="3000" />
	</bean>

	<!-- 总调度用于启动Spring定时器 -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="cronTrigger" />
				<ref bean="simpleTrigger" />
			</list>
		</property>
	</bean>

JAVA代码如下(方式1):

package com.spring.task;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class TaskOne extends QuartzJobBean {
	
	protected static final Logger log=Logger.getLogger(TaskOne.class);
	
	private int timeout;

    /**
     * Setter called after the ExampleJob is instantiated
     * with the value from the JobDetailBean (5)
     */
    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
	
	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		// TODO Auto-generated method stub
		log.info("-----定时任务执行-----");
		
	}

	
}


JAVA代码如下(方式2 ):


package com.spring.task;

import org.apache.log4j.Logger;

public class TaskTwo {
	protected static final Logger log=Logger.getLogger(TaskTwo.class);
	
	public void doIt(){
		log.info("-----定时任务执行-----");
		
	}
}




  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值