编程语言
首页 > 编程语言> > java – 即使提到了节点ID,所有计划作业也在运行

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

作者:互联网

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

请检查以下代码.我正在使用Quartz和Spring启动.

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

SecondJob

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

解决方法:

介绍 :

您确定正确地以群集模式运行吗?

您的弹簧启动配置和项目是否与群集相关配置正确?

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

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

2018-09-05 13:35:03.433 INFO 9096 — [ main]
org.quartz.core.QuartzScheduler : Scheduler
quartzScheduler_$_NON_CLUSTERED started.

当然,工作立即在本地机器上启动.

也许你的配置需要与spring boot和clustering相关的tweeking.例如:https://medium.com/@Hronom/spring-boot-quartz-scheduler-in-cluster-mode-457f4535104d

更新:

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

使用job-store’org.quartz.simpl.RAMJobStore’ – 它不支持持久性.并没有聚集

您可以尝试使用此类教程,以便添加群集支持:

> https://medium.com/@Hronom/spring-boot-quartz-scheduler-in-cluster-mode-457f4535104d
> http://www.quartz-scheduler.org/documentation/quartz-2.x/configuration/ConfigJDBCJobStoreClustering.html

标签:java,scheduled-tasks,spring-boot-2,quartz-scheduler,terracotta
来源: https://codeday.me/bug/20190705/1387956.html