zoukankan      html  css  js  c++  java
  • Springboot之配置定时任务

    1、在启动类上加注解开启定时任务(定时任务可以写在启动类中)

    package com.gxr.imybatisplus;
    
    import com.gxr.imybatisplus.service.schedule.MyScheduleService;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    
    @SpringBootApplication
    @MapperScan("com.gxr.imybatisplus.mapper")
    @EnableScheduling
    public class IMybatisPlusApplication {
    
        @Autowired
        MyScheduleService Myservice;
    
        public static void main(String[] args) {
            SpringApplication.run(IMybatisPlusApplication.class, args);
        }
    
        /**
         * 定时执行,每次插入一条数据
         */
        @Scheduled(cron = "*/5 * * * * ?")
        private void ScheduleTask() {
            String tableName = "t_sample_s_pg1";
            Myservice.insertOne(tableName);
        }
    
    }

    2、编写测试定时任务类 

    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Service;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.logging.Logger;
    
    @Service
    public class MySchedule {
        private final Logger logger = Logger.getLogger(this.getClass().getName());
    
    
        /**
         * 定时任务举例
         */
        @Scheduled(cron = "0/1 * * * * *")
        public void test() {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(format.format(new Date()));
        }
    
    
    }
  • 相关阅读:
    C# 类库 嵌入其他Dll
    docker使用
    7DTD Server Manage
    Eclipse 快捷键-常用
    android webview
    手机摄像头拍摄的照片上传(js .net)
    .net执行存储过程慢,直接执行存储过程很快
    ASP.Net回送。数据提交另外页面
    Mysql详解--知识整理
    IDEA 运行Junit一直卡在Resolving Maven Dependencies
  • 原文地址:https://www.cnblogs.com/gongxr/p/13958288.html
Copyright © 2011-2022 走看看