如何停止定时任务@Scheduled

@Scheduled定时任务在Spring Boot中广泛使用,通过cron、fixedDelay等表达式配置执行周期。当需要停止某个定时任务时,不宜直接修改代码。一种解决方式是创建一个继承ThreadPoolTaskScheduler的类,为每个任务定义唯一标识,并重写scheduleAtFixedRate和scheduleWithFixedDelay方法。在@Scheduled注解的方法中,确保表达式与重写方法对应。同时,在启动类上添加@EnableScheduling和@ComponentScan注解,使定时任务生效。若类中只有一个定时任务,也有相应的方法来停止。

@Scheduled 定时任务可配置的表达式有 cron、zone、fixedDelay、fixedRate、initialDelay 等,各表达式含义如下:
1.cron:cron表达式,指定任务在特定时间执行;
2.fixedDelay:表示上一次任务执行完成后多久再次执行,参数类型为long,单位ms;
3.fixedDelayString:与fixedDelay含义一样,只是参数类型变为String;
4.fixedRate:表示按一定的频率执行任务,参数类型为long,单位ms;
5.fixedRateString: 与fixedRate的含义一样,只是将参数类型变为String;
6.initialDelay:表示延迟多久再第一次执行任务,参数类型为long,单位ms;
7.initialDelayString:与initialDelay的含义一样,只是将参数类型变为String;
8.zone:时区,默认为当前时区,一般没有用到。

当定时任务满足某个条件时,我们想停止它,修改代码显然是不适宜的办法,怎么办呢?
一、如果类中有多个定时任务,只想取消其中某一个,可以这么写,先写一个类继承ThreadPoolTaskScheduler类,给每个方法定义一个常量名字,并重写scheduleAtFixedRate(Runnable task, long period)方法,scheduleWithFixedDelay(Runnable task, long delay)方法,具体重写哪些方法,看你自己业务做选择,,这里只是举例。
注意重写不同方法,@Scheduled表达式要跟着对应,否则可能出现功能无法正常实现

package com.phnix.task.test;

import java.util.IdentityHashMap;
import java.util.Map;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.springframework.core.task.TaskRejectedException;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.TaskUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ErrorHandler;

/**
 * 继承 ThreadPoolTaskScheduler
 * <p>Title: CustomTaskScheduler</p>  
 * <p>Description: TODO(类注释)</p>  
 * @author hfuquan
 * @date 2020年9月12日  上午9:24:52
 * @since JDK1.8
 */
@Service
public class CustomTaskScheduler extends ThreadPoolTaskScheduler {
	
	private static final long serialVersionUID = 1L;
	
	private final Map<Object, ScheduledFuture<?>> scheduledTasks = new IdentityHashMap<>();
    @Nullable
	private volatile ErrorHandler errorHandler;
    
	/**
     * <p>Title: cancelTask</p>  
     * <p>Description: 取消定时器执行</p>  
     * @param identifier scheduledTasks.put 的值
     * @author hfuquan
     * @Date 2020年9月11日 上午11:37:06
     */
    void cancelTask(Object identifier) {
        ScheduledFuture<?> future = scheduledTasks.get(identifier);
        if (null != future) {
            future.cancel(true);
        }
    }

    /**
     * 使用 @Scheduled(fixedRate = xxL) 时,项目启动时,会自动调用这个方法,
     * 一个类同时有多个@Scheduled,所产生的
     * call parent method and store the result Future for cancel task,
     * you can expand other method of you used.
     *
     * @param task   the task need to be executed
     * @param period the time between two continues execute
     * @return the result of task
     */
    @Override
    public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period) {
        ScheduledFuture<?> future = super.scheduleAtFixedRate(task, period);
        scheduledTasks.put(ProcessorTask.CANCEL_TASK_METHOD_NAME, future);
        return future;
    }

    
    /**
     * 使用 @Scheduled(fixedDelay = xxL) 时,项目启动时,会自动调用这个方法
     * <p>Title: scheduleWithFixedDelay</p>  
     * <p>Description: </p>  
     * @param task 如:com.phnix.task.ProcessorTask.doFixedDelay
     * @param delay
     * @return  
     * @see org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler#scheduleWithFixedDelay(java.lang.Runnable, long)
     */
    @Override
	public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay) {
		ScheduledExecutorService executor = getScheduledExecutor();
		try {
			ScheduledFuture<?> future = executor.scheduleWithFixedDelay(errorHandlingTask(task, true), 0, delay, TimeUnit.MILLISECONDS);
		    scheduledTasks.put(ProcessorTask.CANCEL_TASK_METHOD_NAME_2, future);
			return future;
		}
		catch (RejectedExecutionException ex) {
			throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
		}
	}
    private Runnable errorHandlingTask(Runnable task, boolean isRepeatingTask) {
		return TaskUtils.decorateTaskWithErrorHandler(task, this.errorHandler, isRepeatingTask);
	}
    
}

第二步,写一个定时方法,注意两个方法的@Scheduled 里面的表达式跟上面重写的两个方法scheduleAtFixedRate(Runnable task, long period)方法,scheduleWithFixedDelay(Runnable task, long delay)一 一对应

package com.phnix.task.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class ProcessorTask {
	//给每个方法定义一个常量名字
	public static final String CANCEL_TASK_METHOD_NAME="doFixedRate";
	public static final String CANCEL_TASK_METHOD_NAME_2="doFixedDelay";
    private static final int MAX_COUNTS = 5;
    private int counts = 0;

    @Autowired
    private CustomTaskScheduler scheduler;

    /**
     * This task will be execute file times and then cancel.
     */
    @Scheduled(fixedRate = 900L)
    public void doFixedRate() {
    	//判断条件、业务处理 根据自己需要做修改
    	if (counts >= MAX_COUNTS) {
    		//cancelTask的值必须和CustomTaskScheduler类 scheduleAtFixedRate()方法 中scheduledTasks.put 的key一样,否则无法取消定时任务
    		scheduler.cancelTask(CANCEL_TASK_METHOD_NAME);
    	}
    	System.out.println("this " + counts++ + " times do processor task doFixedRate");
    }
    
    @Scheduled(fixedDelay = 1000L)
    public void doFixedDelay() {
    	if (counts >= MAX_COUNTS+5) {
    		System.out.println("processor task success,begin to cancel doFixedDelay");
    		scheduler.cancelTask(CANCEL_TASK_METHOD_NAME_2);
    	}
    	System.out.println("this " + counts++ + " times do processor task doFixedDelay");
    }

}

第三步,在springboot的启动类上注解 @EnableScheduling,使@Scheduled生效,当然 启动类上注解@ComponentScan 也要扫描到上面两个类,否则无法托付给spring管理。

启动项目,如果打印以下信息,说明正常了

this 0 times do processor task doFixedRate
this 1 times do processor task doFixedDelay
this 2 times do processor task doFixedRate
this 3 times do processor task doFixedDelay
this 4 times do processor task doFixedRate
this 5 times do processor task doFixedDelay
processor task success,begin to cancel doFixedRate=======
this 6 times do processor task doFixedRate
this 7 times do processor task doFixedDelay
this 8 times do processor task doFixedDelay
this 9 times do processor task doFixedDelay
processor task success,begin to cancel doFixedDelay*******
this 10 times do processor task doFixedDelay

另外,如果类种只有一个定时方法,可以这么写,也能实现取消定时任务

package com.phnix.task.test;

import java.util.IdentityHashMap;
import java.util.Map;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.springframework.core.task.TaskRejectedException;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.ScheduledMethodRunnable;
import org.springframework.scheduling.support.TaskUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ErrorHandler;

/**
 * 继承 ThreadPoolTaskScheduler
 * <p>Title: CustomTaskScheduler</p>  
 * <p>Description: TODO(类注释)</p>  
 * @author hfuquan
 * @date 2020年9月12日  上午9:24:52
 * @since JDK1.8
 */
@Service
public class CustomTaskScheduler extends ThreadPoolTaskScheduler {
	
	private static final long serialVersionUID = 1L;
	
	private final Map<Object, ScheduledFuture<?>> scheduledTasks = new IdentityHashMap<>();
    @Nullable
	private volatile ErrorHandler errorHandler;
    
	/**
     * <p>Title: cancelTask</p>  
     * <p>Description: 取消定时器执行</p>  
     * @param identifier scheduledTasks.put 的值
     * @author hfuquan
     * @Date 2020年9月11日 上午11:37:06
     */
    void cancelTask(Object identifier) {
        ScheduledFuture<?> future = scheduledTasks.get(identifier);
        if (null != future) {
            future.cancel(true);
        }
    }

    /**
     * 使用 @Scheduled(fixedRate = xxL) 时,项目启动时,会自动调用这个方法,
     * 一个类同时有多个@Scheduled,所产生的
     * call parent method and store the result Future for cancel task,
     * you can expand other method of you used.
     *
     * @param task   the task need to be executed
     * @param period the time between two continues execute
     * @return the result of task
     */
    @Override
    public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period) {
        ScheduledFuture<?> future = super.scheduleAtFixedRate(task, period);
        ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task;
//        scheduledTasks.put(ProcessorTask.CANCEL_TASK_METHOD_NAME, future);
        scheduledTasks.put(runnable.getTarget(), future);
        return future;
    }

    
    /**
     * 使用 @Scheduled(fixedDelay = xxL) 时,项目启动时,会自动调用这个方法
     * <p>Title: scheduleWithFixedDelay</p>  
     * <p>Description: </p>  
     * @param task 如:com.phnix.task.ProcessorTask.doFixedDelay
     * @param delay
     * @return  
     * @see org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler#scheduleWithFixedDelay(java.lang.Runnable, long)
     */
    /*@Override
	public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay) {
		ScheduledExecutorService executor = getScheduledExecutor();
		try {
			ScheduledFuture<?> future = executor.scheduleWithFixedDelay(errorHandlingTask(task, true), 0, delay, TimeUnit.MILLISECONDS);
			ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task;
//		    scheduledTasks.put(ProcessorTask.CANCEL_TASK_METHOD_NAME_2, future);
			scheduledTasks.put(runnable.getTarget(), future);
			return future;
		}
		catch (RejectedExecutionException ex) {
			throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
		}
	}
    private Runnable errorHandlingTask(Runnable task, boolean isRepeatingTask) {
		return TaskUtils.decorateTaskWithErrorHandler(task, this.errorHandler, isRepeatingTask);
	}*/
    
}
package com.phnix.task.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class ProcessorTask {
	
//	public static final String CANCEL_TASK_METHOD_NAME="doFixedRate";
//	public static final String CANCEL_TASK_METHOD_NAME_2="doFixedDelay";
    private static final int MAX_COUNTS = 5;
    private int counts = 0;

    @Autowired
    private CustomTaskScheduler scheduler;

    /**
     * This task will be execute file times and then cancel.
     */
    @Scheduled(fixedRate = 900L)
    public void doFixedRate() {
    	if (counts >= MAX_COUNTS) {
    		//cancelTask的值必须和CustomTaskScheduler类 scheduleAtFixedRate()方法 中scheduledTasks.put 的key一样,否则无法取消定时任务
    		System.out.println("processor task success,begin to cancel doFixedRate=======");
//    		scheduler.cancelTask(CANCEL_TASK_METHOD_NAME);
    		scheduler.cancelTask(this);
    	}
    	System.out.println("this " + counts++ + " times do processor task doFixedRate");
    }
    
    /*@Scheduled(fixedDelay = 1000L)
    public void doFixedDelay() {
    	if (counts >= MAX_COUNTS+5) {
    		System.out.println("processor task success,begin to cancel doFixedDelay*******");
//    		scheduler.cancelTask(CANCEL_TASK_METHOD_NAME_2);
    		scheduler.cancelTask(this);
    	}
    	System.out.println("this " + counts++ + " times do processor task doFixedDelay");
    }*/

}
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值