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();
	}
	
}

你可能感兴趣的:(java)