Spring Task 定时器如何在项目启动时一次性注册多个定时器任务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37848710/article/details/79651298
使用情景:前面的一篇文章已经说明了如何通过外部访问,在不停服务器的情况下动态的修改cron参数值,从而达到动态的修改定时器的任务,有需要的可以参考文章:  动态修改参数cron的值来实现Spring自带定时器动态的执行任务,但是还有一种情况就是能不能不是通过外部修改,而是直接在项目启动的时候就已经注册好所需的几个定时器,答案是可以的,这可能就是因为Spring设计的一贯作风吧!
目的:一个系统下有多家公司,而每家公司的要执行定时任务的cron时间周期不一定会一样,所以就需要将每家公司不同的cron周期写在一个xml的配置文件中,在项目启动时读取配置文件获得cron及每家公司唯一的公司编码code,然后放在一个map集合中,然后通过Spring Task的多个触发器注册多个定时器任务;PS 为了测试是否能实现,我简单的直接将cron与公司编码存在一个map集合中了。最后测试是完全OK的,具体代码如下:

package timer;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

@Lazy(value=false)
@Component
@EnableScheduling
public class QuartzTask implements SchedulingConfigurer{
	private static Logger log = Logger.getLogger(QuartzTask.class);
	
	/**
	 * 注册多个Spring Task的定时器任务,从而来执行多家公司不同的定时任务
	 */
	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		taskRegistrar.setTriggerTasks(getManyTaskAndTrigger());		
	}
	
	/**
	 * 获得多家公司code及多个不同的cron(定时器执行周期)
	 * @return
	 */
	public Map<String, String> getCompanyCodeAndCron(){
		Map<String, String> mapCodeAndCron = new HashMap<String, String>();
		mapCodeAndCron.put("A00000000", "0 46 11 22 3 4");
		mapCodeAndCron.put("B07550001", "0 47 11 22 3 4");
		mapCodeAndCron.put("C00210001", "0 48 11 22 3 4");
		return mapCodeAndCron;
	}
	
	/**
	 * 获得多触发器的多个定时任务的map集合
	 * @return
	 */
	public Map<Runnable, Trigger> getManyTaskAndTrigger(){
		Map<Runnable, Trigger> mapTask = new HashMap<Runnable, Trigger>();
		Map<String, String> mapStr = this.getCompanyCodeAndCron();
		//遍历map集合
		for(Map.Entry<String, String> entry : mapStr.entrySet()){
			final String compCode = entry.getKey();
			final String cron = entry.getValue();
			Runnable runnable = new Runnable() {
				@Override
				public void run() {
					// 执行逻辑任务
					System.err.println("公司:编码=" + compCode + "------cron时间周期=" + cron);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					System.out.println("当前系统时间:" + sdf.format(System.currentTimeMillis()));
				}
			};
			Trigger trigger = new Trigger() {
				@Override
				public Date nextExecutionTime(TriggerContext triggerContext) {
					// 任务触发,可修改任务的执行周期
					CronTrigger trigger = new CronTrigger(cron);
					Date nextExecutor = trigger.nextExecutionTime(triggerContext);
					return nextExecutor;
				}
			};
			mapTask.put(runnable, trigger);
		}
		return mapTask;
	}
	
	
}

运行后控制台输出信息如下:


至此完毕,有不正之处还望多多指教!!!



猜你喜欢

转载自blog.csdn.net/weixin_37848710/article/details/79651298