java springboot实现定时器

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/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值