最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot與Quartz集成實(shí)現(xiàn)分布式定時(shí)任務(wù)集群的代碼實(shí)例

 更新時(shí)間:2019年03月13日 14:37:56   作者:AlanGogogo  
今天小編就為大家分享一篇關(guān)于SpringBoot與Quartz集成實(shí)現(xiàn)分布式定時(shí)任務(wù)集群的代碼實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

Spring Boot與Quartz集成實(shí)現(xiàn)分布式定時(shí)任務(wù)集群

直接貼代碼

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中的屬性被讀取并注入后再初始化對(duì)象
    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啟動(dòng)類

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);
  }
}

數(shù)據(jù)庫(kù)sql

可以在Quartz的lib中找到適當(dāng)?shù)臄?shù)據(jù)庫(kù)生成文件來(lái)創(chuàng)建jdbc job store所需要的表。這些表用于Quartz在集群環(huán)境中的調(diào)度。

一些解釋

把項(xiàng)目復(fù)制一份,然后改掉spring server的啟動(dòng)端口,啟動(dòng)多個(gè)項(xiàng)目,可以觀察到只有一個(gè)項(xiàng)目的Quartz在運(yùn)行。如果當(dāng)前運(yùn)行Quartz的服務(wù)器掛掉,另一臺(tái)會(huì)跟進(jìn)執(zhí)行相同的Quartz任務(wù)。

有待思考的部分

在Quartz集群環(huán)境中,時(shí)間的同步是一個(gè)重要問(wèn)題,有時(shí)間需要去看一下怎么進(jìn)行時(shí)間同步來(lái)確保集群的正確性。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

  • Java如何接收前端easyui?datagrid傳遞的數(shù)組參數(shù)

    Java如何接收前端easyui?datagrid傳遞的數(shù)組參數(shù)

    這篇文章分享一下怎么在easyui的datagrid刷新表格時(shí),在后端java代碼中接收datagrid傳遞的數(shù)組參數(shù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2023-11-11
  • Spring循環(huán)依賴的三種方式(推薦)

    Spring循環(huán)依賴的三種方式(推薦)

    本篇文章主要介紹了Spring循環(huán)依賴的三種方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • 深入解析面向?qū)ο缶幊淘贘ava中的應(yīng)用小結(jié)

    深入解析面向?qū)ο缶幊淘贘ava中的應(yīng)用小結(jié)

    本文詳細(xì)介紹了面向?qū)ο缶幊痰幕靖拍?包括類和對(duì)象、封裝、繼承和多態(tài),通過(guò)具體的Java代碼示例,展示了如何在Java中應(yīng)用這些面向?qū)ο缶幊痰暮诵乃枷?感興趣的朋友跟隨小編一起看看吧
    2025-01-01
  • Java可變參數(shù)的應(yīng)用小結(jié)

    Java可變參數(shù)的應(yīng)用小結(jié)

    這篇文章主要介紹了Java可變參數(shù)的應(yīng)用小結(jié),實(shí)現(xiàn)同一個(gè)函數(shù)名,不同參數(shù)個(gè)數(shù),實(shí)現(xiàn)的方法相同,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-10-10
  • Android接入微信支付的方法

    Android接入微信支付的方法

    這篇文章主要介紹了Android接入微信支付的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • 解析Mybatis連續(xù)傳遞多個(gè)參數(shù)的方法

    解析Mybatis連續(xù)傳遞多個(gè)參數(shù)的方法

    MyBatis是一個(gè)支持普通SQL查詢,存儲(chǔ)過(guò)程和高級(jí)映射的優(yōu)秀持久層框架,這篇文章主要介紹了Mybatis連續(xù)傳遞多個(gè)參數(shù)的方法,需要的朋友可以參考下
    2016-08-08
  • Java中的Set接口實(shí)現(xiàn)類HashSet和LinkedHashSet詳解

    Java中的Set接口實(shí)現(xiàn)類HashSet和LinkedHashSet詳解

    這篇文章主要介紹了Java中的Set接口實(shí)現(xiàn)類HashSet和LinkedHashSet詳解,Set接口和java.util.List接口一樣,同樣繼承自Collection接口,它與Collection接口中的方法基本一致,并沒(méi)有對(duì)Collection接口進(jìn)行功能上的擴(kuò)充,只是比Collection接口更加嚴(yán)格了,需要的朋友可以參考下
    2024-01-01
  • Java中注解@JsonFormat的用法詳解

    Java中注解@JsonFormat的用法詳解

    這篇文章主要給大家介紹了關(guān)于Java中注解@JsonFormat用法的相關(guān)資料,以及分享了@JsonFormat 將枚舉序列化為對(duì)象的方法,文中給出了詳細(xì)的代碼實(shí)例,需要的朋友可以參考下
    2023-01-01
  • Springmvc文件上傳實(shí)現(xiàn)流程解析

    Springmvc文件上傳實(shí)現(xiàn)流程解析

    這篇文章主要介紹了Springmvc文件上傳實(shí)現(xiàn)流程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • springboot 排除redis的自動(dòng)配置操作

    springboot 排除redis的自動(dòng)配置操作

    這篇文章主要介紹了springboot 排除redis的自動(dòng)配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評(píng)論

涡阳县| 开封市| 泽普县| 娄底市| 渑池县| 兴宁市| 资兴市| 奈曼旗| 洛浦县| 安宁市| 肇庆市| 芮城县| 蒲江县| 合阳县| 寿阳县| 新巴尔虎左旗| 青河县| 柘城县| 弥渡县| 永春县| 泸水县| 公主岭市| 渝北区| 河东区| 东光县| 通海县| 通州区| 新源县| 四子王旗| 中西区| 朝阳县| 体育| 青铜峡市| 大同县| 新营市| 崇信县| 开封市| 隆化县| 永顺县| 缙云县| 和林格尔县|