真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

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

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

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比長(zhǎng)樂(lè)網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式長(zhǎng)樂(lè)網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋長(zhǎng)樂(lè)地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。

直接貼代碼

POM


  4.0.0
  test.daemon
  clusterquartz
  0.0.1-SNAPSHOT
  jar
  clusterquartz
  http://maven.apache.org
  
    org.springframework.boot
    spring-boot-starter-parent
    1.4.1.RELEASE
    
  
  
    UTF-8
    UTF-8
    1.8
  
  
    
      org.springframework.boot
      spring-boot-starter
    
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.springframework.boot
      spring-boot-starter-jdbc
    
    
      org.springframework.boot
      spring-boot-starter-logging
    
    
      org.springframework
      spring-context-support
    
    
      MySQL
      mysql-connector-java
    
    
      com.alibaba
      druid
      1.0.13
    
    
      com.h3database
      h3
    
    
      org.quartz-scheduler
      quartz
      2.2.1
    
    
      org.quartz-scheduler
      quartz-jobs
      2.2.1
    
    
      junit
      junit
      test
    
  

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ì)創(chuàng)新互聯(lián)的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接


網(wǎng)站欄目:SpringBoot與Quartz集成實(shí)現(xiàn)分布式定時(shí)任務(wù)集群的代碼實(shí)例
轉(zhuǎn)載來(lái)于:http://weahome.cn/article/jicogh.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部