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 相关文章推荐
浅谈什么是SpringBoot异常处理自动配置的原理
Jun 21 Java/Android
详解java如何集成swagger组件
Jun 21 Java/Android
详解Spring事件发布与监听机制
Jun 30 Java/Android
Springboot配置suffix指定mvc视图的后缀方法
Jul 03 Java/Android
Java网络编程之UDP实现原理解析
Sep 04 Java/Android
Java 在线考试云平台的实现
Nov 23 Java/Android
深入浅出讲解Java8函数式编程
Jan 18 Java/Android
Java实现学生管理系统(IO版)
Feb 24 Java/Android
Java的Object类的九种方法
Apr 13 Java/Android
Java设计模式之代理模式
Apr 22 Java/Android
Java 异步任务计算FutureTask
Apr 28 Java/Android
Java多线程并发FutureTask使用详解
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 采集心得技巧
2009/05/15 PHP
如何实现php图片等比例缩放
2015/07/28 PHP
php array_key_exists() 与 isset() 的区别
2016/10/24 PHP
PHP实现的简单sha1加密功能示例
2017/08/27 PHP
PHP检测接口Traversable用法详解
2017/12/29 PHP
正则表达式判断是否存在中文和全角字符和判断包含中文字符串长度
2008/09/27 Javascript
JavaScript 读取元素的CSS信息的代码
2010/02/07 Javascript
JS解析XML实例分析
2015/01/30 Javascript
jquery对dom节点的操作【推荐】
2016/04/15 Javascript
使用CSS+JavaScript或纯js实现半透明遮罩效果的实例分享
2016/05/09 Javascript
Bootstrap 布局组件(全)
2016/07/18 Javascript
jQuery事件绑定方法学习总结(推荐)
2016/11/21 Javascript
js实现时间轴自动排列效果
2017/03/09 Javascript
Angular.JS中指令ng-if的注意事项小结
2017/06/21 Javascript
JS全角与半角转化实例(分享)
2017/07/04 Javascript
angularjs 获取默认选中的单选按钮的value方法
2018/02/28 Javascript
详解vue通过NGINX部署在子目录或者二级目录实践
2018/09/03 Javascript
Vue使用axios出现options请求方法
2019/05/30 Javascript
jquery 回调操作实例分析【回调成功与回调失败的情况】
2019/09/27 jQuery
node.js使用http模块创建服务器和客户端完整示例
2020/02/10 Javascript
js函数和this用法实例分析
2020/03/13 Javascript
Javascript异步编程async实现过程详解
2020/04/02 Javascript
[29:23]2014 DOTA2国际邀请赛中国区预选赛 LGD-GAMING VS CIS 第一场1
2014/05/23 DOTA
[00:12]DAC2018 Miracle-站上中单舞台,他能否再写奇迹?
2018/04/06 DOTA
Zabbix实现微信报警功能
2016/10/09 Python
Python格式化输出字符串方法小结【%与format】
2018/10/29 Python
利用PyCharm Profile分析异步爬虫效率详解
2019/05/08 Python
Pycharm连接远程服务器并实现远程调试的实现
2019/08/02 Python
Python API自动化框架总结
2019/11/12 Python
500行python代码实现飞机大战
2020/04/24 Python
Python监听剪切板实现方法代码实例
2020/11/11 Python
Street One瑞士:德国现代时装公司
2019/10/09 全球购物
垃圾回收的优点和原理。并考虑2种回收机制
2016/10/16 面试题
环境科学专业大学生自荐信格式
2013/09/21 职场文书
卫校毕业生个人自我鉴定
2014/04/28 职场文书
应急处置方案
2014/06/16 职场文书