|
| 1 | +/* |
| 2 | + * Copyright 2020-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.task.batch.configuration; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.springframework.batch.core.Job; |
| 22 | +import org.springframework.batch.core.configuration.JobRegistry; |
| 23 | +import org.springframework.batch.core.explore.JobExplorer; |
| 24 | +import org.springframework.batch.core.launch.JobLauncher; |
| 25 | +import org.springframework.batch.core.repository.JobRepository; |
| 26 | +import org.springframework.beans.factory.FactoryBean; |
| 27 | +import org.springframework.boot.autoconfigure.batch.BatchProperties; |
| 28 | +import org.springframework.cloud.task.batch.handler.TaskJobLauncherApplicationRunner; |
| 29 | +import org.springframework.util.Assert; |
| 30 | +import org.springframework.util.StringUtils; |
| 31 | + |
| 32 | +/** |
| 33 | + * Factory bean for creating an instance of {@link TaskJobLauncherApplicationRunner}. |
| 34 | + * |
| 35 | + * @author Glenn Renfro |
| 36 | + * @since 2.3.0 |
| 37 | + */ |
| 38 | +public class TaskJobLauncherApplicationRunnerFactoryBean |
| 39 | + implements FactoryBean<TaskJobLauncherApplicationRunner> { |
| 40 | + |
| 41 | + private JobLauncher jobLauncher; |
| 42 | + |
| 43 | + private JobExplorer jobExplorer; |
| 44 | + |
| 45 | + private List<Job> jobs; |
| 46 | + |
| 47 | + private String jobNames; |
| 48 | + |
| 49 | + private JobRegistry jobRegistry; |
| 50 | + |
| 51 | + private Integer order = 0; |
| 52 | + |
| 53 | + private TaskBatchProperties taskBatchProperties; |
| 54 | + |
| 55 | + private JobRepository jobRepository; |
| 56 | + |
| 57 | + public TaskJobLauncherApplicationRunnerFactoryBean(JobLauncher jobLauncher, |
| 58 | + JobExplorer jobExplorer, List<Job> jobs, |
| 59 | + TaskBatchProperties taskBatchProperties, JobRegistry jobRegistry, |
| 60 | + JobRepository jobRepository, BatchProperties batchProperties) { |
| 61 | + Assert.notNull(taskBatchProperties, "taskBatchProperties must not be null"); |
| 62 | + Assert.notNull(batchProperties, "batchProperties must not be null"); |
| 63 | + Assert.notEmpty(jobs, "jobs must not be null nor empty"); |
| 64 | + |
| 65 | + this.jobLauncher = jobLauncher; |
| 66 | + this.jobExplorer = jobExplorer; |
| 67 | + this.jobs = jobs; |
| 68 | + this.jobNames = taskBatchProperties.getJobNames(); |
| 69 | + this.jobRegistry = jobRegistry; |
| 70 | + this.taskBatchProperties = taskBatchProperties; |
| 71 | + if (StringUtils.hasText(batchProperties.getJob().getNames())) { |
| 72 | + this.jobNames = batchProperties.getJob().getNames(); |
| 73 | + } |
| 74 | + else { |
| 75 | + this.jobNames = taskBatchProperties.getJobNames(); |
| 76 | + } |
| 77 | + this.order = taskBatchProperties.getCommandLineRunnerOrder(); |
| 78 | + this.jobRepository = jobRepository; |
| 79 | + } |
| 80 | + |
| 81 | + public void setOrder(int order) { |
| 82 | + this.order = order; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public TaskJobLauncherApplicationRunner getObject() { |
| 87 | + TaskJobLauncherApplicationRunner taskJobLauncherApplicationRunner = new TaskJobLauncherApplicationRunner( |
| 88 | + this.jobLauncher, this.jobExplorer, this.jobRepository, |
| 89 | + this.taskBatchProperties); |
| 90 | + taskJobLauncherApplicationRunner.setJobs(this.jobs); |
| 91 | + if (StringUtils.hasText(this.jobNames)) { |
| 92 | + taskJobLauncherApplicationRunner.setJobNames(this.jobNames); |
| 93 | + } |
| 94 | + taskJobLauncherApplicationRunner.setJobRegistry(this.jobRegistry); |
| 95 | + |
| 96 | + if (this.order != null) { |
| 97 | + taskJobLauncherApplicationRunner.setOrder(this.order); |
| 98 | + } |
| 99 | + return taskJobLauncherApplicationRunner; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Class<?> getObjectType() { |
| 104 | + return TaskJobLauncherApplicationRunner.class; |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments