zoukankan      html  css  js  c++  java
  • Springboot使用Quartz定时任务

    1、在pom.xml文件中配置引入jar包

    <!--配置quartz,定时任务-->
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-quartz</artifactId>
      </dependency>

    2、创建CheckDevStatusQuartz类

    import com.well.driving.service.AlarmService;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.quartz.QuartzJobBean;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class CheckDevStatusQuartz extends QuartzJobBean {
    
        @Autowired
        private XxxService xxxService;
    
        /**
         * 执行定时任务
         *
         * @param jobExecutionContext
         * @throws JobExecutionException
         */
        @Override
        protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
          //此处为你要运行的任务具体接口 xxxService.checkDeviceStatus(); System.out.println(
    "Check device status" + "=========" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); } }

    3、创建QuartzConfig类

    import com.well.driving.quartz.CheckDevStatusQuartz;
    import org.quartz.*;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class QuartzConfig {
        @Bean
        public JobDetail checkDevStatusDetail() {
            return JobBuilder.newJob(CheckDevStatusQuartz.class).withIdentity("checkDevStatusQuartz").storeDurably().build();
        }
    
        @Bean
        public Trigger testQuartzTrigger() {
            SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInSeconds(120)  //设置时间周期单位秒,目前设置为2分钟一次
                    .repeatForever();
            return TriggerBuilder.newTrigger().forJob(checkDevStatusDetail())
                    .withIdentity("checkDevStatusQuartz")
                    .withSchedule(scheduleBuilder)
                    .build();
        }
    }
  • 相关阅读:
    TCP/IP报文 三次握手 四次挥手
    socket 编程
    出现线程死锁的几种情况
    类模板的写法
    【HTTP】boundary 中一个 = 导致HTTP上传文件失败
    【时间戳】 年月日 转换为时间戳
    【CSV文件】CSV文件内容读取
    std::string 的方法c_str() 和 data() 有什么区别
    [转载] C++ STL中判断list为空,size()==0和empty()有什么区别
    【SQL】glob 和 like 的区别
  • 原文地址:https://www.cnblogs.com/shoose/p/12930649.html
Copyright © 2011-2022 走看看