springboot2之系統架構基礎(六) Scheduled定時任務

step1 創建定時配置類  TaskSchedulerConfig

package com.bhz.mail.config.schedule;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

@Configuration
@EnableScheduling	//定時任務配置啓用
public class TaskSchedulerConfig implements SchedulingConfigurer{

	
	/**
	 * 
	 */
	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		//taskRegistrar.setScheduler(null);
	}

}

step2 創建定時任務的實現  ConsumerMailTask

package com.bhz.mail.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ConsumerMailTask {

	/**
	 * initialDelay:項目啓動的時候,延遲5秒開始執行
	 * fixedDelay:每隔2秒執行一次
	 */
	@Scheduled(initialDelay=5000,fixedDelay=2000)
	public void intervalFast() {
		try {
			System.out.println("執行... ... ... ... ... ");
		} catch (Exception e) {
			
		}
		
	}
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章