Java springboot实现定时器功能
1.在启动类中加注解:
//支持定时任务
@EnableScheduling
2.在实现类上加注解:
@Component
3.实现定时器功能
public final static long ONE_Minute = 60*1000;
@Scheduled(cron = "0 0/10 * * * ?")//意思是每10分钟触发一次下面的方法
public void test(){
system.out.println("定时器触发了。。。");
}
整体实现例子:
/**
* 启动类
*/
@SpringBootApplication
//开启定时
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
/**
*定时器实现类
*/
//要声明为bean,没有声明启动类启动无法实现定时效果
@Component
public class SchedulTest {
//定时入库
public final static long ONE_Minute = 60*1000;
@Scheduled(cron = "0 0/10 * * * ?")
public void test(){
System.out.println("定时器触发。。。");
}
}
附:
cron表达式在线生成器地址:https://cron.qqe2.com/