Spring Boot笔记(四) springboot 集成 @Scheduled 定时任务

个人博客网:https://wushaopei.github.io/    (你想要这里多有)

1、在SpringBoot 项目中使用@Scheduled注解执行定时任务:

配置pom.xml 依赖:

一般情况下,SpringBoot 的 相关依赖,如:

                <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

是有包含对应  @Scheduled 定时注解启动所需要的jar包的,如下:

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

2、创建定时类

(1)@Component : 将当前类实例化注入到容器中

(2)定时器的生效要素:

@Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次

在程序启动时,定时器会开始计时,并根据设定时间执行设置好的任务内容,线程的执行是以单线程进行

(3)创建以下定时类

package com.example.poiutis.quartz;


import com.example.poiutis.common.SystemLogHandler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @ClassName HandleTimeQuartz
 * @Description TODO
 * @Author wushaopei
 * @Date 2019/7/25 20:23
 * @Version 1.0
 */
@Component
public class HandleTimeQuartz {
    // 系统日志
    //private static SystemLogHandler systemLogger = SystemLogHandler
   //         .getLogger(HandleTimeQuartz.class);

   private static Logger logger = LoggerFactory.getLogger(InvoiceController.class);

    @Scheduled(cron="0/5 * *  * * ? ")
    public void userStatusJob() {

        System.out.println("进入测试");
        logger.info(System.currentTimeMillis()+"");
    }
}

在 程序入口处添加注解

直接启动后会自动执行:效果如下:

GitHub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值