我们正在使用Spring Boot 2.1.6.RELEASE。之后,我们将spring版本更新为2.2.2.RELEASE。当我们更改版本时,我们注意到我们的石英作业无法正常工作。我们有多个作业,并按如下所示配置它们。经过一些研究之后,我发现QuartzAutoConfiguration类之间存在一些差异。我该如何在2.2.2.Spring发行版中注入触发器。有什么简单的方法吗?我不想写许多触发器和触发器详细信息。
MyConfig
import io.rkpc.commons.util.ApplicationReflectionUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "quartz")
@Profile("quartz")
@Data
public class JobConfig {
private List<Job> jobs;
@Bean
public JobDetail[] jobDetail() throws SchedulerConfigException {
Set<Class<QuartzJobBean>> subClasses = ApplicationReflectionUtil.getSubClasses(QuartzJobBean.class, "io.rkpc");
List<JobDetail> jobDetails = new ArrayList<>();
for (Class<QuartzJobBean> quartzJobBeanClass : subClasses) {
Job job = getJob(quartzJobBeanClass.getSimpleName());
if (job.isEnabled()) {
JobDetail jobDetail = JobBuilder.newJob(quartzJobBeanClass)
.withIdentity(quartzJobBeanClass.getSimpleName())
.storeDurably()
.build();
jobDetails.add(jobDetail);
}
}
return jobDetails.toArray(new JobDetail[0]);
}
@Bean
public Trigger[] jobATrigger(JobDetail[] jobADetails) throws SchedulerConfigException {
List<Trigger> triggers = new ArrayList<>();
for (JobDetail jobDetail : jobADetails) {
Job job = getJob(jobDetail.getKey().getName());
CronTrigger trigger = TriggerBuilder.newTrigger().forJob(jobDetail)
.withIdentity(jobDetail.getKey().getName().concat("Trigger"))
.withSchedule(CronScheduleBuilder.cronSchedule(job.getCron()))
.build();
triggers.add(trigger);
}
return triggers.toArray(new Trigger[0]);
}
private Job getJob(String name) throws SchedulerConfigException {
List<Job> filteredJobs = jobs.stream().filter(job -> job.getName().equals(name)).collect(Collectors.toList());
if (CollectionUtils.isEmpty(filteredJobs) || filteredJobs.size() > 1) {
log.error("{} is not configured", name);
throw new SchedulerConfigException("Job is not configured");
}
return filteredJobs.get(0);
}
@Data
public static class Job {
private String name;
private String cron;
private boolean enabled;
}
}
QuartzAutoConfiguration.java春季版本2.1.6 github url; https://github.com/spring-projects/spring-boot/blob/v2.1.6.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java
QuartzAutoConfiguration.java春季版本2.2.2 github url https://github.com/spring-projects/spring-boot/blob/v2.2.2.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.java
我注意到的主要区别是;在2.1.6版本中,Quartz AutoConfiguration是“ Trigger”数组,但是2.2.2没有“ Trigger”数组。
您将公开一个Trigger[]
bean,而不是多个Trigger
bean。您应该为每个Trigger
定义一个bean。您还应该对每个JobDetail
执行相同的操作。在使用Boot ObjectProvider<Trigger[]>
进行自动配置时,这在Spring Boot 2.1.x上是偶然的。自动配置的目的是消耗所有Trigger
bean,而应用程序上下文在注入它们之前将这些bean变成数组。
如果您不想定义多个Trigger
和JobDetail
Bean,则最好自己配置SchedulerFactoryBean
,而不要依赖自动配置。