欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Quartz+Spring Boot实现动态管理定时任务

程序员文章站 2024-02-24 18:59:16
项目实践过程中碰到一个动态管理定时任务的需求:针对每个人员进行信息的定时更新,具体更新时间可随时调整、启动、暂定等。 思路 将每个人员信息的定时配置保存到数据库中,这样...

项目实践过程中碰到一个动态管理定时任务的需求:针对每个人员进行信息的定时更新,具体更新时间可随时调整、启动、暂定等。

思路

将每个人员信息的定时配置保存到数据库中,这样实现了任务的动态展示和管理。任务的每一次新增或变更,都会去数据库变更信息。

设置一个统一的任务管理器,专门负责动态任务的增删改查。

pom依赖

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
     xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
  <groupid>com.example</groupid>
  <artifactid>dynamic-quartz</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>
  <name>dynamic-quartz</name>
  <description>动态定时任务管理</description>
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.0.2.release</version>
    <relativepath/> <!-- lookup parent from repository -->
  </parent>
  <dependencies>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-context-support</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupid>org.mybatis.spring.boot</groupid>
      <artifactid>mybatis-spring-boot-starter</artifactid>
      <version>1.1.1</version>
    </dependency>
    <!-- jdbc driver -->
    <dependency>
      <groupid>mysql</groupid>
      <artifactid>mysql-connector-java</artifactid>
      <version>5.1.44</version>
    </dependency>
    <dependency>
      <groupid>org.quartz-scheduler</groupid>
      <artifactid>quartz</artifactid>
      <version>2.3.0</version>
    </dependency>
  </dependencies>
<build>
  <plugins>
    <plugin>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-maven-plugin</artifactid>
    </plugin>
  </plugins>
</build>
</project>
application.yml
spring:
 datasource:
  url: jdbc:mysql://localhost:3306/wujiwen
  username: root
  password: root
  driver-class-name: com.mysql.jdbc.driver
 profiles:
   active: dev
logback-spring.xml
<configuration>
  <jmxconfigurator />
  <appender name="stdout" class="ch.qos.logback.core.consoleappender">
    <layout class="ch.qos.logback.classic.patternlayout">
      <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
    </layout>
  </appender>
  <logger name="org.springframework">
    <level value="error" />
  </logger>
  <logger name="org.mybatis">
    <level value="error" />
  </logger>
  <logger name="java.sql.connection">
    <level value="debug" />
  </logger>
  <logger name="java.sql.statement">
    <level value="debug" />
  </logger>
  <logger name="java.sql.preparedstatement">
    <level value="debug" />
  </logger>
  <logger name="java.sql.resultset">
    <level value="debug"/>
  </logger>
  <logger name="org.apache.http">
    <level value="info" />
  </logger>
  <logger name="org.quartz">
    <level value="info" />
  </logger>
  <springprofile name="dev,test">
    <root>
      <level value="debug" />
      <appender-ref ref="stdout" />
    </root>
  </springprofile>
</configuration>

cronentity.java

保存在数据库中的表达式管理器.

package com.example.dynamicquartz.bean;
import java.io.serializable;
import java.util.date;
/**
 * created with intellij idea.
 * packagename : com.example.dynamicquartz.bean
 * author    : wujw
 * date     : 2018/9/9 16:23
 * version   : 1.0.0
 * description : todo
 */
public class cronentity implements serializable {
  private static final long serialversionuid = -3406421161273529348l;
  private string id;
  private string userid; //用户标识
  private string cron; //表达式
  private string quarzname; //任务名称
  private string schedulerclass;//定时任务类
  private date time;
  public string getid() {
    return id;
  }
  public void setid(string id) {
    this.id = id;
  }
  public string getuserid() {
    return userid;
  }
  public void setuserid(string userid) {
    this.userid = userid;
  }
  public string getcron() {
    return cron;
  }
  public void setcron(string cron) {
    this.cron = cron;
  }
  public string getquarzname() {
    return quarzname;
  }
  public void setquarzname(string quarzname) {
    this.quarzname = quarzname;
  }
  public string getschedulerclass() {
    return schedulerclass;
  }
  public void setschedulerclass(string schedulerclass) {
    this.schedulerclass = schedulerclass;
  }
  public date gettime() {
    return time;
  }
  public void settime(date time) {
    this.time = time;
  }
  @override
  public string tostring() {
    return "cronentity{" +
        "id='" + id + '\'' +
        ", userid='" + userid + '\'' +
        ", cron='" + cron + '\'' +
        ", quarzname='" + quarzname + '\'' +
        ", schedulerclass='" + schedulerclass + '\'' +
        ", time=" + time +
        '}';
  }
}

cronmapper

package com.example.dynamicquartz.dao;
import com.example.dynamicquartz.bean.cronentity;
import org.apache.ibatis.annotations.*;
import java.util.list;
/**
 * created with intellij idea.
 * packagename : com.example.dynamicquartz
 * author    : wujw
 * date     : 2018/9/9 20:14
 * version   : 1.0.0
 * description : todo
 */
@mapper
public interface cronmapper{
  @select("select * from cron_table where id = #{id}")
  @results({
      @result(property = "id", column = "id"),
      @result(property = "userid", column = "user_id"),
      @result(property = "cron", column = "cron"),
      @result(property = "quarzname", column = "quarz_name"),
      @result(property = "schedulerclass", column = "scheduler_class"),
      @result(property = "time", column = "time")
  })
  cronentity load(string id);
  @insert("insert into cron_table(id,userid,cron,quartzname,schedulerclass,time)" +
      "values(#{id},#{user_id},#{cron},#{quartz_name},#{scheduler_class},#   {time})")
  void insert(cronentity cronentity);
  @select("select * from cron_table")
  @results({
      @result(property = "id", column = "id"),
      @result(property = "userid", column = "user_id"),
      @result(property = "cron", column = "cron"),
      @result(property = "quarzname", column = "quarz_name"),
      @result(property = "schedulerclass", column = "scheduler_class"),
      @result(property = "time", column = "time")
  })
  list<cronentity> queryall();
  @update("update cron_table set cron=#{1} where id =#{0}")
  void updatecron(string id,string cron);
}

icronservice

package com.example.dynamicquartz.serivce;
import com.example.dynamicquartz.bean.cronentity;
import java.util.list;
/**
 * created with intellij idea.
 * packagename : com.example.dynamicquartz
 * author    : wujw
 * date     : 2018/9/9 19:54
 * version   : 1.0.0
 * description : todo
 */
public interface icronservice{
  void sendemail(string quartzname);
  void sendsms(string quartzname);
  cronentity findbycronid(string id );
  void update(cronentity cronentity);
  list<cronentity> findall();
}

cronserviceimpl

package com.example.dynamicquartz.serivce;

import com.example.dynamicquartz.bean.cronentity;
import com.example.dynamicquartz.dao.cronmapper;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import java.util.list;
/**
 * created with intellij idea.
 * packagename : com.example.dynamicquartz.serivce
 * author    : wujw
 * date     : 2018/9/9 19:55
 * version   : 1.0.0
 * description : todo
 */
@service
public class cronserviceimpl implements icronservice{
  @autowired
  private cronmapper cronmapper;
  @override
  public void sendemail(string quartzname) {
    system.out.println(quartzname +" 正在发送邮件");
  }
  @override
  public void sendsms(string quartzname) {
    system.out.println(quartzname +"正在发送短信");
  }
  @override
  public cronentity findbycronid(string id) {
    return this.cronmapper.load(id);
  }
  @override
  public void update(cronentity cronentity) {
    this.cronmapper.insert(cronentity);
  }
  @override
  public list<cronentity> findall() {
    return this.cronmapper.queryall();
  }
}

quartzmanager

package com.example.dynamicquartz.serivce;
import com.example.dynamicquartz.bean.cronentity;
import org.quartz.*;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.context.annotation.configuration;
import java.util.date;
/**
 * created with intellij idea.
 * packagename : com.example.dynamicquartz
 * author    : wujw
 * date     : 2018/9/9 19:31
 * version   : 1.0.0
 * description : todo
 */
@configuration
public class quartzmanager {
  @autowired
  private scheduler scheduler;
  @autowired
  private icronservice icronservice;
  public void start(string id) throws schedulerexception {
    cronentity cronentity = icronservice.findbycronid(id);
    if(cronentity!= null){
  startjob(scheduler,cronentity.getquarzname(),cronentity.getcron(),cronentity.getschedulerclass());
    }
  }
  private void startjob(scheduler scheduler,string name,string cron,string classname) throws schedulerexception {
    // 通过jobbuilder构建jobdetail实例,jobdetail规定只能是实现job接口的实例
    // jobdetail 是具体job实例
    class<job> jobclass = null;
    try {
      //实例化具体的job任务
      jobclass = (class<job>) class.forname(classname);
    } catch (classnotfoundexception e) {
      e.printstacktrace();
    }
    jobdetail jobdetail = jobbuilder.newjob(jobclass).withidentity(name, "group1").build();
    // 基于表达式构建触发器
    cronschedulebuilder cronschedulebuilder = cronschedulebuilder.cronschedule(cron);
    // crontrigger表达式触发器 继承于trigger
    // triggerbuilder 用于构建触发器实例
    crontrigger crontrigger = triggerbuilder.newtrigger().withidentity(name, "group1")
        .withschedule(cronschedulebuilder).build();
    scheduler.schedulejob(jobdetail, crontrigger);
  }
  /**
   * 获取job信息
   *
   * @param name
   * @param group
   * @return
   * @throws schedulerexception
   */
  public string getjobinfo(string name, string group) throws schedulerexception {
    triggerkey triggerkey = new triggerkey(name, group);
    crontrigger crontrigger = (crontrigger) scheduler.gettrigger(triggerkey);
    return string.format("time:%s,state:%s", crontrigger.getcronexpression(),
        scheduler.gettriggerstate(triggerkey).name());
  }
  /**
   * 修改某个任务的执行时间
   *
   * @param name
   * @param group
   * @param time
   * @return
   * @throws schedulerexception
   */
  public boolean modifyjob(string name, string group, string time) throws schedulerexception {
    date date = null;
    triggerkey triggerkey = new triggerkey(name, group);
    crontrigger crontrigger = (crontrigger) scheduler.gettrigger(triggerkey);
    string oldtime = crontrigger.getcronexpression();
    if (!oldtime.equalsignorecase(time)) {
      cronschedulebuilder cronschedulebuilder = cronschedulebuilder.cronschedule(time);
      crontrigger trigger = triggerbuilder.newtrigger().withidentity(name, group)
          .withschedule(cronschedulebuilder).build();
      date = scheduler.reschedulejob(triggerkey, trigger);
    }
    return date != null;
  }
  /**
   * 暂停所有任务
   *
   * @throws schedulerexception
   */
  public void pausealljob() throws schedulerexception {
    scheduler.pauseall();
  }
  /**
   * 暂停某个任务
   *
   * @param name
   * @param group
   * @throws schedulerexception
   */
  public void pausejob(string name, string group) throws schedulerexception {
    jobkey jobkey = new jobkey(name, group);
    jobdetail jobdetail = scheduler.getjobdetail(jobkey);
    if (jobdetail == null)
      return;
    scheduler.pausejob(jobkey);
  }
  /**
   * 恢复所有任务
   *
   * @throws schedulerexception
   */
  public void resumealljob() throws schedulerexception {
    scheduler.resumeall();
  }
  /**
   * 恢复某个任务
   *
   * @param name
   * @param group
   * @throws schedulerexception
   */
  public void resumejob(string name, string group) throws schedulerexception {
    jobkey jobkey = new jobkey(name, group);
    jobdetail jobdetail = scheduler.getjobdetail(jobkey);
    if (jobdetail == null)
      return;
    scheduler.resumejob(jobkey);
  }
  /**
   * 删除某个任务
   *
   * @param name
   * @param group
   * @throws schedulerexception
   */
  public void deletejob(string name, string group) throws schedulerexception {
    jobkey jobkey = new jobkey(name, group);
    jobdetail jobdetail = scheduler.getjobdetail(jobkey);
    if (jobdetail == null)
      return;
    scheduler.deletejob(jobkey);
  }
}

创建两个task类,分别执行不同的任务

public class emailtask implements job {
  private logger logger = loggerfactory.getlogger(emailtask.class);
  @autowired
  private icronservice icronservice;
  @override
  public void execute(jobexecutioncontext jobexecutioncontext) throws jobexecutionexception {
    logger.info("emailtask task start execute.");
    //模拟任务执行
    icronservice.sendemail(jobexecutioncontext.getjobdetail().getkey().getname());
  }
}
public class smstask implements job {
  private logger logger = loggerfactory.getlogger(smstask.class);
  @autowired
  private icronservice icronservice;
  @override
  public void execute(jobexecutioncontext jobexecutioncontext) throws jobexecutionexception {
    logger.info("smstask task start execute.");
    //模拟任务执行
    icronservice.sendsms(jobexecutioncontext.getjobdetail().getkey().getname());
  }
}

schedulercontroller

@restcontroller
@requestmapping("/scheduler")
public class schedulercontroller{
  @autowired
  quartzmanager quartzmanager;
  @requestmapping(value = "/start", method = requestmethod.get)
   @responsebody
  public string start(@requestparam(name = "id", defaultvalue = "") string id) {
    try {
      quartzmanager.start(id);
    } catch (schedulerexception e) {
      e.printstacktrace();
    }
    return "启动成功";
  }
}

cron_table.sql

create table `cron_table` (
 `id` varchar(11) collate utf8_bin not null,
 `user_id` varchar(11) collate utf8_bin default null,
 `cron` varchar(255) collate utf8_bin default null,
 `quarz_name` varchar(255) collate utf8_bin default null,
 `scheduler_class` varchar(255) collate utf8_bin default null,
 `time` datetime default null,
 primary key (`id`)
) engine=myisam default charset=utf8 collate=utf8_bin;
-- ----------------------------
-- records of cron_table
-- ----------------------------
insert into `cron_table` values ('1', 'aaa', '0/5 * * * * ?', '用户aaa', 'com.example.dynamicquartz.job.smstask', '2018-09-06 20:26:55');
insert into `cron_table` values ('2', 'bbb', '0/2 * * * * ?', '用户bbb', 'com.example.dynamicquartz.job.emailtask', '2018-09-09 21:02:08');

测试结果

http://localhost:8080/scheduler/start?id=1

http://localhost:8080/scheduler/start?id=2

2018-09-09 21:05:08,386 [http-nio-8080-exec-2] debug com.example.dynamicquartz.dao.cronmapper.load - ==> preparing: select * from cron_table where id = ? 
2018-09-09 21:05:08,408 [http-nio-8080-exec-2] debug com.example.dynamicquartz.dao.cronmapper.load - ==> parameters: 1(string)
2018-09-09 21:05:08,427 [http-nio-8080-exec-2] debug com.example.dynamicquartz.dao.cronmapper.load - <==   total: 1
2018-09-09 21:05:10,036 [quartzscheduler_worker-1] info com.example.dynamicquartz.job.smstask - smstask task start execute.
用户aaa正在发送短信
2018-09-09 21:05:11,581 [http-nio-8080-exec-1] debug com.example.dynamicquartz.dao.cronmapper.load - ==> preparing: select * from cron_table where id = ? 
2018-09-09 21:05:11,582 [http-nio-8080-exec-1] debug com.example.dynamicquartz.dao.cronmapper.load - ==> parameters: 2(string)
2018-09-09 21:05:11,584 [http-nio-8080-exec-1] debug com.example.dynamicquartz.dao.cronmapper.load - <==   total: 1
2018-09-09 21:05:12,013 [quartzscheduler_worker-2] info com.example.dynamicquartz.job.emailtask - emailtask task start execute.
用户bbb 正在发送邮件
2018-09-09 21:05:14,002 [quartzscheduler_worker-3] info com.example.dynamicquartz.job.emailtask - emailtask task start execute.
用户bbb 正在发送邮件
2018-09-09 21:05:15,001 [quartzscheduler_worker-4] info com.example.dynamicquartz.job.smstask - smstask task start execute.
用户aaa正在发送短信
2018-09-09 21:05:16,001 [quartzscheduler_worker-5] info com.example.dynamicquartz.job.emailtask - emailtask task start execute.
用户bbb 正在发送邮件
2018-09-09 21:05:18,001 [quartzscheduler_worker-6] info com.example.dynamicquartz.job.emailtask - emailtask task start execute.
用户bbb 正在发送邮件
2018-09-09 21:05:20,000 [quartzscheduler_worker-7] info com.example.dynamicquartz.job.smstask - smstask task start execute.
用户aaa正在发送短信
2018-09-09 21:05:20,001 [quartzscheduler_worker-8] info com.example.dynamicquartz.job.emailtask - emailtask task start execute.
用户bbb 正在发送邮件
2018-09-09 21:05:22,001 [quartzscheduler_worker-9] info com.example.dynamicquartz.job.emailtask - emailtask task start execute.
用户bbb 正在发送邮件
2018-09-09 21:05:24,000 [quartzscheduler_worker-10] info com.example.dynamicquartz.job.emailtask - emailtask task start execute.
用户bbb 正在发送邮件
2018-09-09 21:05:25,001 [quartzscheduler_worker-1] info com.example.dynamicquartz.job.smstask - smstask task start execute.
用户aaa正在发送短信
2018-09-09 21:05:26,000 [quartzscheduler_worker-2] info com.example.dynamicquartz.job.emailtask - emailtask task start execute.
用户bbb 正在发送邮件
2018-09-09 21:05:28,001 [quartzscheduler_worker-3] info com.example.dynamicquartz.job.emailtask - emailtask task start execute.

可以看出,aaa的间隔时间时5s,bbb的间隔时间时2s.

到这里我们的测试已经一本完成了,接下来就是针对任务的更新,停止和启动了,只需要调用 quartzmanager 对应的方法即可,必要时更新一下数据库就好啦。

总结

以上所述是小编给大家介绍的quartz+spring boot实现动态管理定时任务,希望对大家有所帮助