springboot创建的web项目整合Quartz框架的项目实践


Posted in Java/Android onJune 21, 2022

介绍

Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以做成标准的Java组件或EJBs。Quartz的最新版本为Quartz 2.3.2。

quartz可以在某一个有规律的时间点干某件事,是一个任务调度框架,可以被集成到java的各种应用中使用,quartz框架可以支持分布式任务处理

基于springboot创建的web项目整合Quartz框架

整体代码结构

springboot创建的web项目整合Quartz框架的项目实践

导入依赖

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

创建quartz_demo数据库,执行tables_mysql_innodb.sql文件创建对应表,下面是文件内容

#
# In your Quartz properties file, you'll need to set
# org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#
#
# By: Ron Cordell - roncordell
#  I didn't see this anywhere, so I thought I'd post it here. This is the script from Quartz to create the tables in a MySQL database, modified to use INNODB instead of MYISAM.

DROP TABLE IF EXISTS QRTZ_FIRED_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_PAUSED_TRIGGER_GRPS;
DROP TABLE IF EXISTS QRTZ_SCHEDULER_STATE;
DROP TABLE IF EXISTS QRTZ_LOCKS;
DROP TABLE IF EXISTS QRTZ_SIMPLE_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_SIMPROP_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_CRON_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_BLOB_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_TRIGGERS;
DROP TABLE IF EXISTS QRTZ_JOB_DETAILS;
DROP TABLE IF EXISTS QRTZ_CALENDARS;

CREATE TABLE QRTZ_JOB_DETAILS(
SCHED_NAME VARCHAR(120) NOT NULL,
JOB_NAME VARCHAR(190) NOT NULL,
JOB_GROUP VARCHAR(190) NOT NULL,
DESCRIPTION VARCHAR(250) NULL,
JOB_CLASS_NAME VARCHAR(250) NOT NULL,
IS_DURABLE VARCHAR(1) NOT NULL,
IS_NONCONCURRENT VARCHAR(1) NOT NULL,
IS_UPDATE_DATA VARCHAR(1) NOT NULL,
REQUESTS_RECOVERY VARCHAR(1) NOT NULL,
JOB_DATA BLOB NULL,
PRIMARY KEY (SCHED_NAME,JOB_NAME,JOB_GROUP))
ENGINE=InnoDB;

CREATE TABLE QRTZ_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR(190) NOT NULL,
TRIGGER_GROUP VARCHAR(190) NOT NULL,
JOB_NAME VARCHAR(190) NOT NULL,
JOB_GROUP VARCHAR(190) NOT NULL,
DESCRIPTION VARCHAR(250) NULL,
NEXT_FIRE_TIME BIGINT(13) NULL,
PREV_FIRE_TIME BIGINT(13) NULL,
PRIORITY INTEGER NULL,
TRIGGER_STATE VARCHAR(16) NOT NULL,
TRIGGER_TYPE VARCHAR(8) NOT NULL,
START_TIME BIGINT(13) NOT NULL,
END_TIME BIGINT(13) NULL,
CALENDAR_NAME VARCHAR(190) NULL,
MISFIRE_INSTR SMALLINT(2) NULL,
JOB_DATA BLOB NULL,
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
FOREIGN KEY (SCHED_NAME,JOB_NAME,JOB_GROUP)
REFERENCES QRTZ_JOB_DETAILS(SCHED_NAME,JOB_NAME,JOB_GROUP))
ENGINE=InnoDB;

CREATE TABLE QRTZ_SIMPLE_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR(190) NOT NULL,
TRIGGER_GROUP VARCHAR(190) NOT NULL,
REPEAT_COUNT BIGINT(7) NOT NULL,
REPEAT_INTERVAL BIGINT(12) NOT NULL,
TIMES_TRIGGERED BIGINT(10) NOT NULL,
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP))
ENGINE=InnoDB;

CREATE TABLE QRTZ_CRON_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR(190) NOT NULL,
TRIGGER_GROUP VARCHAR(190) NOT NULL,
CRON_EXPRESSION VARCHAR(120) NOT NULL,
TIME_ZONE_ID VARCHAR(80),
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP))
ENGINE=InnoDB;

CREATE TABLE QRTZ_SIMPROP_TRIGGERS
  (
    SCHED_NAME VARCHAR(120) NOT NULL,
    TRIGGER_NAME VARCHAR(190) NOT NULL,
    TRIGGER_GROUP VARCHAR(190) NOT NULL,
    STR_PROP_1 VARCHAR(512) NULL,
    STR_PROP_2 VARCHAR(512) NULL,
    STR_PROP_3 VARCHAR(512) NULL,
    INT_PROP_1 INT NULL,
    INT_PROP_2 INT NULL,
    LONG_PROP_1 BIGINT NULL,
    LONG_PROP_2 BIGINT NULL,
    DEC_PROP_1 NUMERIC(13,4) NULL,
    DEC_PROP_2 NUMERIC(13,4) NULL,
    BOOL_PROP_1 VARCHAR(1) NULL,
    BOOL_PROP_2 VARCHAR(1) NULL,
    PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
    FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
    REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP))
ENGINE=InnoDB;

CREATE TABLE QRTZ_BLOB_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_NAME VARCHAR(190) NOT NULL,
TRIGGER_GROUP VARCHAR(190) NOT NULL,
BLOB_DATA BLOB NULL,
PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
INDEX (SCHED_NAME,TRIGGER_NAME, TRIGGER_GROUP),
FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP))
ENGINE=InnoDB;

CREATE TABLE QRTZ_CALENDARS (
SCHED_NAME VARCHAR(120) NOT NULL,
CALENDAR_NAME VARCHAR(190) NOT NULL,
CALENDAR BLOB NOT NULL,
PRIMARY KEY (SCHED_NAME,CALENDAR_NAME))
ENGINE=InnoDB;

CREATE TABLE QRTZ_PAUSED_TRIGGER_GRPS (
SCHED_NAME VARCHAR(120) NOT NULL,
TRIGGER_GROUP VARCHAR(190) NOT NULL,
PRIMARY KEY (SCHED_NAME,TRIGGER_GROUP))
ENGINE=InnoDB;

CREATE TABLE QRTZ_FIRED_TRIGGERS (
SCHED_NAME VARCHAR(120) NOT NULL,
ENTRY_ID VARCHAR(95) NOT NULL,
TRIGGER_NAME VARCHAR(190) NOT NULL,
TRIGGER_GROUP VARCHAR(190) NOT NULL,
INSTANCE_NAME VARCHAR(190) NOT NULL,
FIRED_TIME BIGINT(13) NOT NULL,
SCHED_TIME BIGINT(13) NOT NULL,
PRIORITY INTEGER NOT NULL,
STATE VARCHAR(16) NOT NULL,
JOB_NAME VARCHAR(190) NULL,
JOB_GROUP VARCHAR(190) NULL,
IS_NONCONCURRENT VARCHAR(1) NULL,
REQUESTS_RECOVERY VARCHAR(1) NULL,
PRIMARY KEY (SCHED_NAME,ENTRY_ID))
ENGINE=InnoDB;

CREATE TABLE QRTZ_SCHEDULER_STATE (
SCHED_NAME VARCHAR(120) NOT NULL,
INSTANCE_NAME VARCHAR(190) NOT NULL,
LAST_CHECKIN_TIME BIGINT(13) NOT NULL,
CHECKIN_INTERVAL BIGINT(13) NOT NULL,
PRIMARY KEY (SCHED_NAME,INSTANCE_NAME))
ENGINE=InnoDB;

CREATE TABLE QRTZ_LOCKS (
SCHED_NAME VARCHAR(120) NOT NULL,
LOCK_NAME VARCHAR(40) NOT NULL,
PRIMARY KEY (SCHED_NAME,LOCK_NAME))
ENGINE=InnoDB;

CREATE INDEX IDX_QRTZ_J_REQ_RECOVERY ON QRTZ_JOB_DETAILS(SCHED_NAME,REQUESTS_RECOVERY);
CREATE INDEX IDX_QRTZ_J_GRP ON QRTZ_JOB_DETAILS(SCHED_NAME,JOB_GROUP);

CREATE INDEX IDX_QRTZ_T_J ON QRTZ_TRIGGERS(SCHED_NAME,JOB_NAME,JOB_GROUP);
CREATE INDEX IDX_QRTZ_T_JG ON QRTZ_TRIGGERS(SCHED_NAME,JOB_GROUP);
CREATE INDEX IDX_QRTZ_T_C ON QRTZ_TRIGGERS(SCHED_NAME,CALENDAR_NAME);
CREATE INDEX IDX_QRTZ_T_G ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_GROUP);
CREATE INDEX IDX_QRTZ_T_STATE ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_STATE);
CREATE INDEX IDX_QRTZ_T_N_STATE ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP,TRIGGER_STATE);
CREATE INDEX IDX_QRTZ_T_N_G_STATE ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_GROUP,TRIGGER_STATE);
CREATE INDEX IDX_QRTZ_T_NEXT_FIRE_TIME ON QRTZ_TRIGGERS(SCHED_NAME,NEXT_FIRE_TIME);
CREATE INDEX IDX_QRTZ_T_NFT_ST ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_STATE,NEXT_FIRE_TIME);
CREATE INDEX IDX_QRTZ_T_NFT_MISFIRE ON QRTZ_TRIGGERS(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME);
CREATE INDEX IDX_QRTZ_T_NFT_ST_MISFIRE ON QRTZ_TRIGGERS(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME,TRIGGER_STATE);
CREATE INDEX IDX_QRTZ_T_NFT_ST_MISFIRE_GRP ON QRTZ_TRIGGERS(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME,TRIGGER_GROUP,TRIGGER_STATE);

CREATE INDEX IDX_QRTZ_FT_TRIG_INST_NAME ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,INSTANCE_NAME);
CREATE INDEX IDX_QRTZ_FT_INST_JOB_REQ_RCVRY ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,INSTANCE_NAME,REQUESTS_RECOVERY);
CREATE INDEX IDX_QRTZ_FT_J_G ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,JOB_NAME,JOB_GROUP);
CREATE INDEX IDX_QRTZ_FT_JG ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,JOB_GROUP);
CREATE INDEX IDX_QRTZ_FT_T_G ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP);
CREATE INDEX IDX_QRTZ_FT_TG ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,TRIGGER_GROUP);

commit;

配置quartz

server:
  port: ${PORT:12011}
  servlet:
    session:
      timeout: 86400s # session有效期(xxx秒)
      cookie:
        http-only: false # 如果为true, 浏览器脚本将无法访问cookie
        secure: false # 如果为true, 则仅通过https连接发送cookie, http无法携带cookie
    context-path:  # 程序上下文路径配置
spring:
  application:
    name: quartz-demo
  aop:
    proxy-target-class: true
    auto: true
  datasource:
    url: jdbc:mysql://localhost:3306/quartz_demo
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
  # 配置日期格式化
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss  #时间戳统一转换为指定格式
    time-zone: GMT+8  # 时区修改为东8区
  quartz:
    # 将任务等保存化到数据库
    job-store-type: jdbc
    # 程序结束时会等待quartz相关的内容结束
    wait-for-jobs-to-complete-on-shutdown: true
    # QuartzScheduler启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录
    overwrite-existing-jobs: true
    properties:
      org:
        quartz:
          # scheduler相关
          scheduler:
            # scheduler的实例名
            instanceName: scheduler
            instanceId: AUTO
          # 持久化相关
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            # 表示数据库中相关表是QRTZ_开头的
            tablePrefix: QRTZ_
            useProperties: false
            # 配置集群
            # 是否加入集群
            isClustered: true
            # 容许的最大作业延长时间
            clusterCheckinInterval: 20000
          # 线程池相关
          threadPool:
            class: org.quartz.simpl.SimpleThreadPool
            # 线程数
            threadCount: 10
            # 线程优先级
            threadPriority: 5
            threadsInheritContextClassLoaderOfInitializingThread: true
mybatis-plus:
  global-config:
    db-config:
      logic-delete-field: is_deleted
      logic-delete-value: 1
      logic-not-delete-value: 0
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
logging:
  level:
    com.my.quartz: debug

添加sys_job表

CREATE TABLE `sys_job` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT,
  `job_id` varchar(100) DEFAULT NULL,
  `job_name` varchar(255) DEFAULT NULL,
  `job_group_name` varchar(255) DEFAULT NULL,
  `invoke_target` varchar(255) NOT NULL,
  `cron_expression` varchar(50) DEFAULT NULL,
  `misfire_policy` varchar(255) DEFAULT NULL COMMENT 'cron计划策略0=默认,1=立即触发执行,2=触发一次执行,3=不触发立即执行',
  `status` varchar(10) DEFAULT NULL COMMENT '任务状态(0正常 1暂停)',
  `concurrent` varchar(10) DEFAULT NULL COMMENT '是否并发执行(0允许 1禁止)',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

写两条数据

springboot创建的web项目整合Quartz框架的项目实践

依次实现mvc三层

mapper层的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.my.quartz.boot.mapper.SysJobMapper">
    <resultMap id="BaseResultMap" type="com.my.quartz.boot.SysJob">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="job_id" jdbcType="VARCHAR" property="jobId" />
        <result column="job_name" jdbcType="VARCHAR" property="jobName" />
        <result column="job_group_name" jdbcType="VARCHAR" property="jobGroupName" />
        <result column="invoke_target" jdbcType="VARCHAR" property="invokeTarget" />
        <result column="cron_expression" jdbcType="VARCHAR" property="cronExpression" />
        <result column="misfire_policy" jdbcType="VARCHAR" property="misfirePolicy" />
        <result column="status" jdbcType="VARCHAR" property="status" />
        <result column="concurrent" jdbcType="VARCHAR" property="concurrent" />
    </resultMap>
    <sql id="Base_Column_List">
        t.id, t.job_id jobId, t.job_name jobName, t.job_group_name jobGroupName, t.invoke_target invokeTarget, t.cron_expression cronExpression, t.misfire_policy misfirePolicy, t.status, t.concurrent
    </sql>
</mapper>

mapper接口

/**
 * Auto Generated
 *
 * @author :Boy
 * @date :Created in 2022-06-18
 * @description:
 * @modified By:
 */
public interface SysJobMapper extends BaseMapper<SysJob> {

}

service接口

public interface SysJobService extends IService<SysJob> {

}

service接口实现

@Service
public class SysJobServiceImpl extends ServiceImpl<SysJobMapper, SysJob> implements SysJobService {
    @Autowired
    private Scheduler scheduler;
    @Autowired
    private ScheduleUtils scheduleUtils;

    /**
     * 项目启动时,初始化定时器
     * 主要是防止手动修改数据库导致未同步到定时任务处理(注:不能手动修改数据库ID和任务组名,否则会导致脏数据)
     */
    @PostConstruct
    public void init() throws SchedulerException, TaskException {
        scheduler.clear();
        List<SysJob> jobList = baseMapper.selectList(null);
        for (SysJob job : jobList) {
            scheduleUtils.createScheduleJob(scheduler, job);
        }
    }
}

controller代码

@RestController
@RequestMapping("/job")
public class SysJobController {
    @Resource
    private SysJobService sysJobService;
    @Resource
    private ScheduleUtils scheduleUtils;
    @Resource
    private Scheduler scheduler;

    @PostMapping("/add")
    public Object add(@RequestBody SysJob entity) throws SchedulerException, TaskException {
        sysJobService.save(entity);
        scheduleUtils.createScheduleJob(scheduler, entity);
        return "ok";
    }
}

ScheduleUtils 定时任务工具类

/**
 * @Author ScholarTang
 * @Date 2021/7/15 下午3:50
 * @Desc 定时任务工具类
 */
@Component
public class ScheduleUtils {

    @Autowired
    private Scheduler scheduler;

    /**
     * 构建任务触发对象
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    public TriggerKey getTriggerKey(String jobName, String jobGroup) {
        return TriggerKey.triggerKey(jobName, jobGroup);
    }

    /**
     * 构建任务键对象
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    public JobKey getJobKey(String jobName, String jobGroup) {
        return JobKey.jobKey(jobName, jobGroup);
    }

    /**
     * 创建定时调度任务
     *
     *
     * @param scheduler
     * @param job
     * @throws SchedulerException
     * @throws TaskException
     */
    public void createScheduleJob(Scheduler scheduler, SysJob job) throws SchedulerException, TaskException {
        // 构建job信息
        String jobName = job.getJobId() + "_" + job.getJobName();
        String jobGroupName = job.getJobGroupName();

        //TODO 反射动态获取Job实现类
        //构建job实例
        JobDetail jobDetail = JobBuilder.newJob(QuartzJobImpl.class)
                .withIdentity(getJobKey(jobName, jobGroupName))
                .build();
        jobDetail.getJobDataMap().put("QuartzJobExecutionData", job);
        // 表达式调度构建器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
        cronScheduleBuilder = handleCronScheduleMisfirePolicy(job, cronScheduleBuilder);

        // 按新的cronExpression表达式构建一个新的trigger
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(jobName, jobGroupName))
                .withSchedule(cronScheduleBuilder).build();

        // 放入参数,运行时的方法可以获取
        jobDetail.getJobDataMap().put("QuartzJobExecutionData", job);

        // 判断是否存在
        if (scheduler.checkExists(getJobKey(jobName, jobGroupName))) {
            // 防止创建时存在数据问题 先移除,然后在执行创建操作
            scheduler.deleteJob(getJobKey(jobName, jobGroupName));
        }

        //创建定时任务调度
        scheduler.scheduleJob(jobDetail, trigger);

        // 暂停任务 规定 0启动 1暂停
        if (job.getStatus().equals("1")) {
            scheduler.pauseJob(getJobKey(jobName, jobGroupName));
        }
    }


    /**
     * 删除定时调度任务
     *
     * @param sysJob
     * @throws SchedulerException
     */
    public void deleteScheduleJob(SysJob sysJob) throws SchedulerException {
        scheduler.deleteJob(getJobKey(sysJob.getJobId() + "_" + sysJob.getJobName(), sysJob.getJobGroupName()));
    }


    /**
     * 设置定时任务策略
     *
     * @param sysJob
     * @param cronScheduleBuilder
     * @return
     */
    public CronScheduleBuilder handleCronScheduleMisfirePolicy(SysJob sysJob, CronScheduleBuilder cronScheduleBuilder) throws TaskException {
        switch (sysJob.getMisfirePolicy()) {
            case "0":
                return cronScheduleBuilder;
            case "1":
                return cronScheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
            case "2":
                return cronScheduleBuilder.withMisfireHandlingInstructionFireAndProceed();
            case "3":
                return cronScheduleBuilder.withMisfireHandlingInstructionDoNothing();
            default:
                throw new TaskException("任务失败策略 '" + sysJob.getMisfirePolicy()
                        + "' 不能在cron计划任务中使用", TaskException.Code.CONFIG_ERROR);
        }
    }
}

创建一个Job实现类,只是一个简单实现类,没有对方法添加参数

@Slf4j
@Component
public class QuartzJobImpl implements Job {

    @Autowired
    private ApplicationContext context;

    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SysJob sysJob = new SysJob();
        BeanUtils.copyProperties(jobExecutionContext.getMergedJobDataMap().get("QuartzJobExecutionData"), sysJob);
        log.info("正在执行任务");
        String invokeTarget = sysJob.getInvokeTarget();
        String beanName = invokeTarget.split("\\.")[0];
        String methodName = invokeTarget.split("\\.")[1];
        Object bean = context.getBean(beanName);
        Class<?> clazz = bean.getClass();
        Method method = clazz.getMethod(methodName);
        method.invoke(bean);
        log.info("已结束任务");
        System.out.println();
    }
}

创建最终任务执行类,执行函数级别的定时任务,数据库中的invoke_target是和这里放入spring中的名称相同的,quartz_target.hello最终执行的函数就是hello函数

@Slf4j
@Component("quartz_target")
public class QuartzJobExe {

    @Resource
    private Scheduler scheduler;

    public void hello() throws Exception {
        System.out.println("[job]实例执行...hello..." + System.currentTimeMillis());
    }

    public void action() throws Exception {
        System.out.println("[job]实例执行...action..." + System.currentTimeMillis());
    }
}

最终代码结构

springboot创建的web项目整合Quartz框架的项目实践

启动spring boot之后的任务状态

springboot创建的web项目整合Quartz框架的项目实践

使用postman工具类添加一个任务

springboot创建的web项目整合Quartz框架的项目实践

然后在代码中添加一个对应的方法

springboot创建的web项目整合Quartz框架的项目实践

重新启动springboot的运行结果

springboot创建的web项目整合Quartz框架的项目实践

理论上使用这样的反射方式可以运行任何代码的

 到此这篇关于springboot创建的web项目整合Quartz框架的项目实践的文章就介绍到这了,更多相关springboot整合Quartz框架内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!


Tags in this post...

Java/Android 相关文章推荐
java中重写父类方法加不加@Override详解
Jun 21 Java/Android
图解排序算法之希尔排序Java实现
Jun 26 Java/Android
Java中PriorityQueue实现最小堆和最大堆的用法
Jun 27 Java/Android
Lombok的详细使用及优缺点总结
Jul 15 Java/Android
SpringRetry重试框架的具体使用
Jul 25 Java/Android
logback 实现给变量指定默认值
Aug 30 Java/Android
Java 深入探究讲解简单工厂模式
Apr 07 Java/Android
Java Spring Lifecycle的使用
May 06 Java/Android
Android Studio实现带三角函数对数运算功能的高级计算器
May 20 Java/Android
Java完整实现记事本代码
Jun 16 Java/Android
详解Spring Security如何在权限中使用通配符
Jun 28 Java/Android
springboot 全局异常处理和统一响应对象的处理方式
Jun 28 Java/Android
springboot读取resources下文件的方式详解
Jun 21 #Java/Android
java实现自定义时钟并实现走时功能
Jun 21 #Java/Android
SpringBoot使用ip2region获取地理位置信息的方法
Jun 21 #Java/Android
Android基础入门之dataBinding的简单使用教程
Jun 21 #Java/Android
一文搞懂Java中的注解和反射
Jun 21 #Java/Android
Android学习之BottomSheetDialog组件的使用
Jun 21 #Java/Android
SpringCloud中分析讲解Feign组件添加请求头有哪些坑梳理
Jun 21 #Java/Android
You might like
创建数据库php代码 用PHP写出自己的BLOG系统
2010/04/12 PHP
解析CI的AJAX分页 另类实现方法
2013/06/27 PHP
php通过前序遍历树实现无需递归的无限极分类
2015/07/10 PHP
php解决crontab定时任务不能写入文件问题的方法分析
2019/09/16 PHP
jQuery 判断元素上是否绑定了事件
2009/10/28 Javascript
JavaScript游戏之优化篇
2010/11/08 Javascript
GreyBox技术总结(转)
2010/11/23 Javascript
顶部缓冲下拉菜单导航特效的JS代码
2013/08/27 Javascript
jquery动态增加删除表格行的小例子
2013/11/14 Javascript
深入理解JS中的变量及作用域、undefined与null
2014/03/04 Javascript
JQuery复制DOM节点的方法
2015/06/11 Javascript
switch语句的妙用(必看篇)
2016/10/03 Javascript
JS正则表达式之非捕获分组用法实例分析
2016/12/28 Javascript
vue组件生命周期详解
2017/11/07 Javascript
详解微信小程序实现跑马灯效果(附完整代码)
2019/04/29 Javascript
vue+mock.js实现前后端分离
2019/07/24 Javascript
Vue 权限控制的两种方法(路由验证)
2019/08/16 Javascript
js实现简单的打印表格
2020/01/15 Javascript
JavaScript冒泡算法原理与实现方法深入理解
2020/06/04 Javascript
Django中login_required装饰器的深入介绍
2017/11/24 Python
python利用ffmpeg进行录制屏幕的方法
2019/01/10 Python
Python正则表达式和re库知识点总结
2019/02/11 Python
Python如何筛选序列中的元素的方法实现
2019/07/15 Python
Python字典添加,删除,查询等相关操作方法详解
2020/02/07 Python
Pytorch 使用CNN图像分类的实现
2020/06/16 Python
捷克体育用品购物网站:D-sport
2017/12/28 全球购物
英国标志性生活方式品牌:Skinnydip London
2019/12/15 全球购物
文艺晚会主持词
2014/03/24 职场文书
创先争优一句话承诺
2014/05/29 职场文书
商场周年庆活动方案
2014/08/19 职场文书
观看信仰心得体会
2014/09/04 职场文书
文员转正自我鉴定怎么写
2014/09/29 职场文书
对党的十八届四中全会的期盼
2014/10/17 职场文书
民主评议教师党员自我评价
2015/03/04 职场文书
在职证明格式样本
2015/06/15 职场文书
校园运动会广播稿
2015/08/19 职场文书