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

SpringBoot与Quartz集成实现分布式定时任务集群的代码实例

程序员文章站 2023-12-21 14:02:34
spring boot与quartz集成实现分布式定时任务集群 直接贴代码 pom

spring boot与quartz集成实现分布式定时任务集群

直接贴代码

pom

<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>test.daemon</groupid>
  <artifactid>clusterquartz</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>
  <name>clusterquartz</name>
  <url>http://maven.apache.org</url>
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.4.1.release</version>
    <relativepath />
  </parent>
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-jdbc</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-logging</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-context-support</artifactid>
    </dependency>
    <dependency>
      <groupid>mysql</groupid>
      <artifactid>mysql-connector-java</artifactid>
    </dependency>
    <dependency>
      <groupid>com.alibaba</groupid>
      <artifactid>druid</artifactid>
      <version>1.0.13</version>
    </dependency>
    <dependency>
      <groupid>com.h2database</groupid>
      <artifactid>h2</artifactid>
    </dependency>
    <dependency>
      <groupid>org.quartz-scheduler</groupid>
      <artifactid>quartz</artifactid>
      <version>2.2.1</version>
    </dependency>
    <dependency>
      <groupid>org.quartz-scheduler</groupid>
      <artifactid>quartz-jobs</artifactid>
      <version>2.2.1</version>
    </dependency>
    <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

application.yml

server:
 port: 80
spring:
 datasource:
  url: jdbc:mysql://localhost:3306/quartz
  username: admin
  password: admin
  driver-class-name: com.mysql.jdbc.driver

quartz.properties

#============================================================================
# configure jobstore
# using spring datasource in schedulerconfig.java
# spring uses localdatasourcejobstore extension of jobstorecmt
#============================================================================
org.quartz.jobstore.useproperties=false
org.quartz.jobstore.tableprefix = qrtz_
org.quartz.jobstore.isclustered = true
org.quartz.jobstore.clustercheckininterval = 5000
org.quartz.jobstore.misfirethreshold = 60000
org.quartz.jobstore.txisolationlevelreadcommitted = true
org.quartz.jobstore.class = org.quartz.impl.jdbcjobstore.jobstoretx
org.quartz.jobstore.driverdelegateclass = org.quartz.impl.jdbcjobstore.stdjdbcdelegate
#============================================================================
# configure main scheduler properties
# needed to manage cluster instances
#============================================================================
org.quartz.scheduler.instancename = clusterquartz
org.quartz.scheduler.instanceid= auto
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapjobexecutioninusertransaction = false
#============================================================================
# configure threadpool
# can also be configured in spring configuration
#============================================================================
#org.quartz.threadpool.class = org.quartz.simpl.simplethreadpool
#org.quartz.threadpool.threadcount = 5
#org.quartz.threadpool.threadpriority = 5
#org.quartz.threadpool.threadsinheritcontextclassloaderofinitializingthread = true

spring配置类

package test.daemon.clusterquartz.config;
import java.io.ioexception;
import java.util.properties;
import java.util.concurrent.executor;
import javax.sql.datasource;
import org.quartz.scheduler;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.config.propertiesfactorybean;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.core.io.classpathresource;
import org.springframework.scheduling.concurrent.threadpooltaskexecutor;
import org.springframework.scheduling.quartz.crontriggerfactorybean;
import org.springframework.scheduling.quartz.jobdetailfactorybean;
import org.springframework.scheduling.quartz.schedulerfactorybean;
import test.daemon.clusterquartz.quartz.quartzjob;
@configuration
public class schedulerconfig {
  @autowired
  private datasource datasource;
  @bean
  public scheduler scheduler() throws exception {
    scheduler scheduler = schedulerfactorybean().getscheduler();
    scheduler.start();
    return scheduler;
  }
  @bean
  public schedulerfactorybean schedulerfactorybean() throws ioexception {
    schedulerfactorybean factory = new schedulerfactorybean();
    factory.setschedulername("cluster_scheduler");
    factory.setdatasource(datasource);
    factory.setapplicationcontextschedulercontextkey("applicationcontext");
    factory.settaskexecutor(schedulerthreadpool());
    factory.settriggers(trigger1().getobject());
    factory.setquartzproperties(quartzproperties());
    return factory;
  }
  @bean
  public properties quartzproperties() throws ioexception {
    propertiesfactorybean propertiesfactorybean = new propertiesfactorybean();
    propertiesfactorybean.setlocation(new classpathresource("/quartz.properties"));
    // 在quartz.properties中的属性被读取并注入后再初始化对象
    propertiesfactorybean.afterpropertiesset();
    return propertiesfactorybean.getobject();
  }
  @bean
  public jobdetailfactorybean job1() {
    jobdetailfactorybean jobdetailfactorybean = new jobdetailfactorybean();
    jobdetailfactorybean.setjobclass(quartzjob.class);
    jobdetailfactorybean.setdurability(true);
    jobdetailfactorybean.setrequestsrecovery(true);
    return jobdetailfactorybean;
  }
  @bean
  public crontriggerfactorybean trigger1() {
    crontriggerfactorybean crontriggerfactorybean = new crontriggerfactorybean();
    crontriggerfactorybean.setjobdetail(job1().getobject());
    crontriggerfactorybean.setcronexpression("0/3 * * * * ?");
    return crontriggerfactorybean;
  }
  @bean
  public executor schedulerthreadpool() {
    threadpooltaskexecutor executor = new threadpooltaskexecutor();
    executor.setcorepoolsize(15);
    executor.setmaxpoolsize(25);
    executor.setqueuecapacity(100);
    return executor;
  }
}

quartz job类

package test.daemon.clusterquartz.quartz;
import java.util.date;
import org.quartz.disallowconcurrentexecution;
import org.quartz.jobexecutioncontext;
import org.quartz.jobexecutionexception;
import org.quartz.persistjobdataafterexecution;
import org.springframework.scheduling.quartz.quartzjobbean;
@persistjobdataafterexecution
@disallowconcurrentexecution
public class quartzjob extends quartzjobbean {
  @override
  protected void executeinternal(jobexecutioncontext context) throws jobexecutionexception {
    // todo auto-generated method stub
    system.out.println("\nquartz job " + new date());
  }
}

spring boot启动类

package test.daemon.clusterquartz;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
@springbootapplication
public class cluster {
  public static void main(string[] args) throws exception {
    springapplication.run(cluster.class, args);
  }
}

数据库sql

可以在quartz的lib中找到适当的数据库生成文件来创建jdbc job store所需要的表。这些表用于quartz在集群环境中的调度。

一些解释

把项目复制一份,然后改掉spring server的启动端口,启动多个项目,可以观察到只有一个项目的quartz在运行。如果当前运行quartz的服务器挂掉,另一台会跟进执行相同的quartz任务。

有待思考的部分

在quartz集群环境中,时间的同步是一个重要问题,有时间需要去看一下怎么进行时间同步来确保集群的正确性。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

上一篇:

下一篇: