主要内容
分布式批处理框架, 采用 spring boot
2, quartz
2 集群
本文以 spring boot
V2.2.5 版本为例说明。
更新历史
无
POM文件大体如下:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
spring: quartz: job-store-type: jdbc jdbc: initialize-schema: never/embedded/always # 第一次必须使用 always, 生成表 properties: org: quartz: jobStore: class: org.quartz.impl.jdbcjobstore.JobStoreTX driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate clusterCheckinInterval: 5000 tablePrefix: QRTZ_ # 辅助表, 表名开头 isClustered: true # 打开集群模式 scheduler: instanceId: AUTO threadPool: class: org.quartz.simpl.SimpleThreadPool threadCount: 10 threadPriority: 5 threadsInheritContextClassLoaderOfInitializingThread: true
job
package com.mandalat.ehealth.job; import cc.giveme5.common.service.ComSystemParameterService; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import java.time.LocalDateTime; public class AJob implements Job { @Autowired private ComSystemParameterService parameterService; @Value("${server.port}") private String port; @Override public void execute(JobExecutionContext context) { parameterService.findAll(); System.out.println(port + "--------------------" + LocalDateTime.now()); } }
job config
package com.mandalat.ehealth.common.config; import com.mandalat.ehealth.job.AJob; import org.quartz.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class JobConfiguration { @Bean public JobDetail jobADetails() { return JobBuilder.newJob(AJob.class).withIdentity("sampleJobA") .storeDurably().build(); } @Bean public Trigger jobATrigger() { return TriggerBuilder.newTrigger() .forJob(jobADetails()) .withIdentity("sampleTriggerA") .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * ? * * *")) .build(); } }
分别在 8080 和 8081 上 启动服务;
可以看到只有一台服务调用到了 AJob
中的方法;
把这台服务关闭, 5秒后可以看到另一台服务调用到了 AJob
中的方法;
说明Quartz 的集群分布式部署成功.