1. 程式人生 > >SpringBoot2整合Quartz

SpringBoot2整合Quartz

  • 新增maven依耐:
<!-- quartz定時任務 -->
		<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
		</dependency>
  • 示例程式碼:
@Component
@Configurable
@EnableScheduling
public class ScheduledTasks {
	
	private int count = 0;

	//每2秒執行一次,初始延遲3秒開始執行
	@Scheduled(fixedRate = 2000,initialDelay = 3000)
	public void reportCurrentByCron() {
		System.out.println("每2秒執行一次定時任務,這是第" + ++count + "次執行,當前時間:" + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
	}
  • 執行結果: