springboot的任务调度(定时任务)


springboot的任务调度(定时任务)

制作人:全心全意

springboot的任务调度(定时任务,不支持分布式)

任务调度实现类

package com.zq.main.tasks;

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

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

@Component // 表示将这个类交给Spring管理
public class ScheduledTasks {
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

	@Scheduled(fixedRate = 5000)
	// 设置时间间隔,毫秒为单位
	//@Scheduled(cron="1/2 * * * * ? ")	//每2秒执行一次
	//使用Quartz表达式设定定时任务,表达式生成网址https://www.bejson.com/othertools/cron,格式为秒分时日月周
	public void reportCurrentTime() {
		System.out.println("现在时间:" + dateFormat.format(new Date()));
	}

}

  

启动类

package com.zq.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.zq.main.mybatis.dao.config.data1.Data1Config;
import com.zq.main.mybatis.dao.config.data2.Data2Config;

@EnableConfigurationProperties({ Data1Config.class, Data2Config.class })
@SpringBootApplication
@EnableScheduling // 开启任务调度
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM