Spring boot2.0 入门(十)-定时器

springboot定时器用起来很简单,都是自带的,直接上代码,看就会,这里介绍常用的:
1.在配置中设置好定时器的参数,启动后无法修改
2.自由(动态)对定时器启停和控制定时参数

1.在配置中设置好定时器的参数,启动后无法修改
先在application.yml文件中配置定时时间:
commom1:每分钟执行一次
comcom2:每天凌晨1点执行

cron:
  commom1:
    0 0/1 * * * ?
  commom2:
    0 0 1 * * ?

test1和test2异步执行

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

import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling   
@EnableAsync        
@PropertySource("classpath:application.yml")
public class MultithreadScheduleTask
{
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    
	@Async
    @Scheduled(cron = "${cron.commom1}")
    public void test1()
    {
    	System.out.println("定时任务执行时间:" + dateFormat.format(new Date()));
    }
    
    @Async
    @Scheduled(cron = "${cron.commom2}")
    public void test2()
    {
    	System.out.println("定时任务执行时间:" + dateFormat.format(new Date()));
    }
}

2.自由(动态)对定时器启停和控制定时参数
先创建个自己的线程任务:

import org.springframework.stereotype.Service;

@Service
public class MyTaskTest implements Runnable
{
	@Override
	public void run() 
	{
		System.out.println("my task");
	}
}

在定时器中添加上自己的任务

import java.util.Date;
import java.util.concurrent.ScheduledFuture;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

@Component
public class DynamicScheduleTask {
	
	@Autowired
	private ThreadPoolTaskScheduler threadPoolTaskScheduler;
	@Autowired
	private MyTaskTest myTaskTest ;
	
	private ScheduledFuture<?> future;
	
	@Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler()
	{
        return new ThreadPoolTaskScheduler();
    }
	
	private String cron = "";

	public String getCron()
	{
		return cron;
	}
	
	public void setCron(String cron)
	{
		this.cron = cron;
	}
	
	public void startCron()
	{
		stopCron();
		future = threadPoolTaskScheduler.schedule(myTaskTest ,new Trigger(){
		@Override
		public Date nextExecutionTime(TriggerContext triggerContext){
		    return new CronTrigger(cron).nextExecutionTime(triggerContext);
		}
		});
	}

	public void stopCron()
	{
		if (future != null)
		{
			future.cancel(true);// 取消任务调度
		}
	}
}

如果想要在程序加载完之后,启动定时,则:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class InitTask implements ApplicationRunner
{
	@Autowired
	private DynamicScheduleTask dynamicScheduleTask;
	
	@Override
	public void run(ApplicationArguments args) throws Exception 
	{
		dynamicScheduleTask.setCron("0 0/1 * * * ?");
        dynamicScheduleTask.startCron();
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值