即使提到了节点ID,所有计划作业仍在运行

时间:2018-09-03 07:33:12

标签: java spring-boot scheduled-tasks quartz-scheduler terracotta

我配置了两个作业。我已经提到了工作组的节点ID。 我的要求是在提到的特定节点上运行特定作业。 但是,当我启动我的应用程序时,即使我在属性文件中提到了节点ID,所有作业也会执行。

请检查以下代码。我在Spring Boot中使用Quartz。

pom.xml

<dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.terracotta.toolkit</groupId>
        <artifactId>terracotta-toolkit-api</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.terracotta.quartz</groupId>
        <artifactId>quartz-terracotta-ee</artifactId>
        <version>2.3.10</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>23.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.5</version>
    </dependency>

第一份工作

public class FirstJob implements Job, Serializable {


    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("In First Job Execution....");
        JobKey jobKey = context.getJobDetail().getKey();
        JobDataMap data = context.getJobDetail().getJobDataMap();
    }

第二个职位

public class SecondJob implements Job, Serializable {
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("In Second Job Execution....");
        JobKey jobKey = context.getJobDetail().getKey();
        JobDataMap data = context.getJobDetail().getJobDataMap();
    }
}

ScheduleConfig

@Configuration
public class SchedulerConfig {

private static final Logger LOG = LoggerFactory.getLogger(SchedulerConfig.class);

@Bean
public JobFactory jobFactory(ApplicationContext applicationContext) {
    AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
    jobFactory.setApplicationContext(applicationContext);
    return jobFactory;
}


@Bean
public Scheduler schedulerFactoryBean(DataSource dataSource,JobFactory jobFactory)throws Exception{
    SchedulerFactoryBean factory = new SchedulerFactoryBean();
    factory.setOverwriteExistingJobs(true);
    factory.setDataSource(dataSource);
    factory.setJobFactory(jobFactory);
    factory.setQuartzProperties(quartzProperties());
    factory.afterPropertiesSet();

    Scheduler scheduler = factory.getScheduler();
    scheduler.setJobFactory(jobFactory);

    TriggerKey triggerKey = TriggerKey.triggerKey("FirstJob", "firstTriggers");
    CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);

    JobDetail jobDetail = localJob(newJob(FirstJob.class).withIdentity("FirstJob", "firstJobs")
            .withDescription("i am first job").storeDurably(true).build())
            .where(node().is(partOfNodeGroup("firstJobs"))).build();

    FirstJob job = new FirstJob();
    jobDetail.getJobDataMap().put("firstJob", job);

    CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
    trigger = TriggerBuilder.newTrigger().withIdentity("FirstJob", "firstTriggers")
            .withSchedule(scheduleBuilder).build();
    System.out.println("Successfully configured First Job");
    //Second Job

    TriggerKey triggerKey1 = TriggerKey.triggerKey("SecondJob", "secondTriggers");
    CronTrigger trigger1 = (CronTrigger) scheduler.getTrigger(triggerKey1);

    JobDetail jobDetail1 = localJob(newJob(SecondJob.class).withIdentity("SecondJob", "secondJobs")
            .withDescription("i am second job").storeDurably(true).build())
            .where(node().is(partOfNodeGroup("secondJobs"))).build();

    SecondJob secondJob = new SecondJob();
    jobDetail1.getJobDataMap().put("secondJob", job);
    CronScheduleBuilder scheduleBuilder1 = CronScheduleBuilder.cronSchedule("0/17 * * * * ?");
    trigger1 = TriggerBuilder.newTrigger().withIdentity("SecondJob", "secondTriggers")
            .withSchedule(scheduleBuilder1).build();
    System.out.println("Successfully configured Second Job");

    System.out.println("Starting Schedulers....");
    scheduler.scheduleJob(jobDetail, trigger);
    scheduler.scheduleJob(jobDetail1, trigger1);

    scheduler.start();
    return scheduler;
}

@Bean
public Properties quartzProperties() throws IOException {
    PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
    propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
    propertiesFactoryBean.setLocation(new ClassPathResource("/quartzLocality.properties"));
    propertiesFactoryBean.afterPropertiesSet();
    return propertiesFactoryBean.getObject();
}
}

应用程序属性

spring.jpa.show-sql=true
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto = none

spring.jpa.database=mysql
spring.datasource.schema=classpath:db/quartz_tables_mysql.sql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/quartz
spring.datasource.username=root
spring.datasource.password=root

quartz.properties

org.quartz.scheduler.instanceName=springboot-quartz
org.quartz.scheduler.instanceId=node1
org.quartz.threadPool.threadCount=2
org.quartz.jobStore.isClustered=true
org.quartz.jobStore.misfireThreshold=60000
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass= 
org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.plugin.jobInitializer.scanInterval = 0 

quartzLocality.properties

org.quartz.locality.nodeGroup.firstJobs = node0
#org.quartz.locality.nodeGroup.secondJobs = node1

org.quartz.locality.nodeGroup.allNodes = node0,node1

org.quartz.locality.nodeGroup.firstJobs.triggerGroups = firstTriggers
org.quartz.locality.nodeGroup.secondJobs.triggerGroups = secondTriggers

编辑:控制台日志

首先我明白了

Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
Using job-store 'org.springframework.scheduling.quartz.LocalDataSourceJobStore' - which supports persistence. and is not clustered.

接下来我在相同的执行过程中得到这个。.

Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered

1 个答案:

答案 0 :(得分:1)

简介:

确定要在群集模式下正确运行吗?

关于集群,您的Spring Boot配置和项目是否配置正确?

在我自己的测试中(当我未启用群集时),我看到以下指示:

  

使用作业存储库'org.quartz.simpl.RAMJobStore'-不   支持持久性。并且没有聚集。

     

2018-09-05 13:35:03.433 INFO 9096 --- [主要]   org.quartz.core.QuartzScheduler:调度程序   crystalScheduler _ $ _ NON_CLUSTERED已启动。

当然,作业会立即在本地计算机上开始。

也许您的配置需要与春季启动和集群有关的工作。例如:https://medium.com/@Hronom/spring-boot-quartz-scheduler-in-cluster-mode-457f4535104d

更新:

根据您的日志,我看到了相同的问题:

Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered

为了增加集群支持,您可以尝试这样的教程吗?