|
| 1 | +/* |
| 2 | + * Copyright 2024 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 | +package org.springframework.batch.core.configuration.support; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.apache.commons.logging.Log; |
| 23 | +import org.apache.commons.logging.LogFactory; |
| 24 | +import org.springframework.batch.core.Job; |
| 25 | +import org.springframework.batch.core.configuration.DuplicateJobException; |
| 26 | +import org.springframework.batch.core.configuration.JobLocator; |
| 27 | +import org.springframework.batch.core.configuration.JobRegistry; |
| 28 | +import org.springframework.beans.BeansException; |
| 29 | +import org.springframework.beans.FatalBeanException; |
| 30 | +import org.springframework.beans.factory.BeanFactory; |
| 31 | +import org.springframework.beans.factory.BeanFactoryAware; |
| 32 | +import org.springframework.beans.factory.DisposableBean; |
| 33 | +import org.springframework.beans.factory.InitializingBean; |
| 34 | +import org.springframework.beans.factory.ListableBeanFactory; |
| 35 | +import org.springframework.beans.factory.SmartInitializingSingleton; |
| 36 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 37 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 38 | +import org.springframework.util.Assert; |
| 39 | + |
| 40 | +/** |
| 41 | + * A {@link SmartInitializingSingleton} that registers {@link Job} beans with a |
| 42 | + * {@link JobRegistry}. Include a bean of this type along with your job configuration and |
| 43 | + * use the same {@link JobRegistry} as a {@link JobLocator} when you need to locate a |
| 44 | + * {@link Job} to launch. |
| 45 | + * <p> |
| 46 | + * This class is an alternative to {@link JobRegistryBeanPostProcessor} and prevents early |
| 47 | + * bean initializations. You must include at most one of either of them as a bean. |
| 48 | + * |
| 49 | + * @author Henning Pöttker |
| 50 | + * @since 5.1.1 |
| 51 | + */ |
| 52 | +public class JobRegistrySmartInitializingSingleton |
| 53 | + implements SmartInitializingSingleton, BeanFactoryAware, InitializingBean, DisposableBean { |
| 54 | + |
| 55 | + private static final Log logger = LogFactory.getLog(JobRegistrySmartInitializingSingleton.class); |
| 56 | + |
| 57 | + // It doesn't make sense for this to have a default value... |
| 58 | + private JobRegistry jobRegistry = null; |
| 59 | + |
| 60 | + private final Collection<String> jobNames = new HashSet<>(); |
| 61 | + |
| 62 | + private String groupName = null; |
| 63 | + |
| 64 | + private ListableBeanFactory beanFactory; |
| 65 | + |
| 66 | + /** |
| 67 | + * Default constructor. |
| 68 | + */ |
| 69 | + public JobRegistrySmartInitializingSingleton() { |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Convenience constructor for setting the {@link JobRegistry}. |
| 74 | + * @param jobRegistry the {@link JobRegistry} to register the {@link Job}s with |
| 75 | + */ |
| 76 | + public JobRegistrySmartInitializingSingleton(JobRegistry jobRegistry) { |
| 77 | + this.jobRegistry = jobRegistry; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * The group name for jobs registered by this component. Optional (defaults to null, |
| 82 | + * which means that jobs are registered with their bean names). Useful where there is |
| 83 | + * a hierarchy of application contexts all contributing to the same |
| 84 | + * {@link JobRegistry}: child contexts can then define an instance with a unique group |
| 85 | + * name to avoid clashes between job names. |
| 86 | + * @param groupName the groupName to set |
| 87 | + */ |
| 88 | + public void setGroupName(String groupName) { |
| 89 | + this.groupName = groupName; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Injection setter for {@link JobRegistry}. |
| 94 | + * @param jobRegistry the {@link JobRegistry} to register the {@link Job}s with |
| 95 | + */ |
| 96 | + public void setJobRegistry(JobRegistry jobRegistry) { |
| 97 | + this.jobRegistry = jobRegistry; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
| 102 | + if (beanFactory instanceof ListableBeanFactory listableBeanFactory) { |
| 103 | + this.beanFactory = listableBeanFactory; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Make sure the registry is set before use. |
| 109 | + */ |
| 110 | + @Override |
| 111 | + public void afterPropertiesSet() throws Exception { |
| 112 | + Assert.state(jobRegistry != null, "JobRegistry must not be null"); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Unregister all the {@link Job} instances that were registered by this post |
| 117 | + * processor. |
| 118 | + */ |
| 119 | + @Override |
| 120 | + public void destroy() throws Exception { |
| 121 | + for (String name : jobNames) { |
| 122 | + if (logger.isDebugEnabled()) { |
| 123 | + logger.debug("Unregistering job: " + name); |
| 124 | + } |
| 125 | + jobRegistry.unregister(name); |
| 126 | + } |
| 127 | + jobNames.clear(); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void afterSingletonsInstantiated() { |
| 132 | + if (beanFactory == null) { |
| 133 | + return; |
| 134 | + } |
| 135 | + Map<String, Job> jobs = beanFactory.getBeansOfType(Job.class, false, false); |
| 136 | + for (var entry : jobs.entrySet()) { |
| 137 | + postProcessAfterInitialization(entry.getValue(), entry.getKey()); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private void postProcessAfterInitialization(Job job, String beanName) { |
| 142 | + try { |
| 143 | + String groupName = this.groupName; |
| 144 | + if (beanFactory instanceof DefaultListableBeanFactory defaultListableBeanFactory |
| 145 | + && beanFactory.containsBean(beanName)) { |
| 146 | + groupName = getGroupName(defaultListableBeanFactory.getBeanDefinition(beanName), job); |
| 147 | + } |
| 148 | + job = groupName == null ? job : new GroupAwareJob(groupName, job); |
| 149 | + ReferenceJobFactory jobFactory = new ReferenceJobFactory(job); |
| 150 | + String name = jobFactory.getJobName(); |
| 151 | + if (logger.isDebugEnabled()) { |
| 152 | + logger.debug("Registering job: " + name); |
| 153 | + } |
| 154 | + jobRegistry.register(jobFactory); |
| 155 | + jobNames.add(name); |
| 156 | + } |
| 157 | + catch (DuplicateJobException e) { |
| 158 | + throw new FatalBeanException("Cannot register job configuration", e); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Determine a group name for the job to be registered. The default implementation |
| 164 | + * returns the {@link #setGroupName(String) groupName} configured. Provides an |
| 165 | + * extension point for specialised subclasses. |
| 166 | + * @param beanDefinition the bean definition for the job |
| 167 | + * @param job the job |
| 168 | + * @return a group name for the job (or null if not needed) |
| 169 | + */ |
| 170 | + protected String getGroupName(BeanDefinition beanDefinition, Job job) { |
| 171 | + return groupName; |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments