首页 > 其他分享 >SpringBoot整合简单的定时任务~

SpringBoot整合简单的定时任务~

时间:2023-02-08 21:22:23浏览次数:51  
标签:quartz SpringBoot springframework 整合 org import 定时 com public

定时任务框架很多种Quartz,SpringTask,xxljob,PowerJob...

1、JDK提供的timer
// JDK提供的
        Timer timer = new Timer();
        //timer.schedule(new TimerTask() {
        //    @Override
        //    public void run() {
        //        System.out.println("JDK提供的 timer task 执行了~");
        //    }
        //},0);
        
        // 指定时间执行,并且两秒执行一次
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("JDK提供的 timer task 执行了~");
            }
        }, new Date(), 2000);
2、Quartz

先导入依赖

implementation 'org.springframework.boot:spring-boot-starter-quartz:3.0.2'

定义一个你需要执行的任务

package com.qbb.quartz;

import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * @author startqbb (个人博客:https://www.cnblogs.com/qbbit)
 * @date 2023-02-08  20:56
 * @tags 喜欢就去努力的争取
 */
@Slf4j
public class QuartzBean extends QuartzJobBean {
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        log.info("quartz task run ......");
    }
}

定义一个Quartz的配置类,绑定Trigger和JobDetail

package com.qbb.quartz;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author startqbb (个人博客:https://www.cnblogs.com/qbbit)
 * @date 2023-02-08  20:57
 * @tags 喜欢就去努力的争取
 */
@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail jobDetail() {
        // 绑定Job
        return JobBuilder
                .newJob(QuartzBean.class)
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger trigger() {
        // 绑定JobDetail
        ScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
        return TriggerBuilder
                .newTrigger()
                .forJob(jobDetail())
                .withSchedule(scheduleBuilder)
                .build();
    }
}

启动主程序
image

3、SpringTask

开启SpringTask定时任务

package com.qbb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling // 开启定时任务
public class SpringbootTaskApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootTaskApplication.class, args);
    }
}

在需要执行的任务上加@Scheduled(注意需要把当前任务类交给Spring管理)

package com.qbb.springtask;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author startqbb (个人博客:https://www.cnblogs.com/qbbit)
 * @date 2023-02-08  21:10
 * @tags 喜欢就去努力的争取
 */
@Component
@Slf4j
public class SpringTaskDemo {

    @Scheduled(cron = "0/1 * * * * ?")
    public void print(){
      log.info("SpringTask Run ......");
    }
}

image

TODO 整合PowerJob...

标签:quartz,SpringBoot,springframework,整合,org,import,定时,com,public
From: https://www.cnblogs.com/qbbit/p/17103343.html

相关文章

  • linux系统时间、机器克隆、定时任务
    linux系统时间、虚拟机克隆、定时任务系统时间时间同步策略电脑主板有电容,能存储一定的电量,这个电量会支持时间继续计数。现在联网后,计算机的服务商会发送一个时间......
  • MySQL 定时备份数据库
    在操作数据过程中,可能会导致数据错误,甚至数据库奔溃,而有效的定时备份能很好地保护数据库。本篇文章主要讲述了几种方法进行 MySQL定时备份数据库。一.mysqldump命令备......
  • springboot 动态获取配置信息完成启动
    架构说设计到数据量较大的应用要从k8s中迁出单独机器部署于是将8节点的服务准备迁出,且端口号在数据库中保存在不引入springcloud的方式下启动spring容器中对args进行配......
  • 完整工作流整合方案,自定义配置,Java+Vue+Activiti@附配套文档
    前言activiti工作流引擎项目,企业erp、oa、hr、crm等企事业办公系统轻松落地,一套完整并且实际运用在多套项目中的案例,满足日常业务流程审批需求。一、项目形式springboot......
  • SpringBoot工程入门case
    SpringBoot的设计目的是用来简化Spring应用的初始搭建以及开发过程。SpringBoot入门案例:1、创建一个新module  2、除pom和src文件剩余都删除。  3、在src.com......
  • springboot开发日记(7)
    springboot——自动配置在日记(2)中提到过,@SpringBootApplication由以下三个注解组合而成:@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan1.@Spr......
  • SpringBoot
    SpringBoot文章来源于:雷神:https://www.bilibili.com/video/BV19K4y1L7MT/?spm_id_from=333.337.search-card.all.click&vd_source=a9bff059910348f08db3690eefbeacbe特点......
  • 【Elasticsearch】整合Spring Data Elasticsearch
    整合SpringDataElasticsearch如何查看官方文档(了解)官方文档:​​JavaHighLevelRESTClient|JavaRESTClient[6.8]|Elastic​​下面是获得文档的方式(可以不用看):步......
  • Springboot整合AOP和注解,实现丰富的切面功能
    简介我们在文章《SpringAOP与AspectJ的对比及应用》介绍了AOP的使用,这篇文章讲解一下AOP与注解的整合,通过注解来使用AOP,会非常方便。为了简便,我们还是来实现一个计时的功......
  • SpringBoot动态生成接口
    原文链接:https://blog.csdn.net/lmchhh/article/details/128634606文章目录SpringBoot动态生成接口一,简单例子二,各种请求方法以及条件2.1无参GET方法2.2带1参的......