BATCH-1474: fix registration error if job name is not the same as bean name
This commit is contained in:
@@ -134,24 +134,30 @@ public class DefaultJobLoader implements JobLoader {
|
||||
contexts.put(factory, context);
|
||||
String[] names = context.getBeanNamesForType(Job.class);
|
||||
|
||||
Collection<Job> result = new ArrayList<Job>();
|
||||
|
||||
for (String name : names) {
|
||||
|
||||
if (!autoRegistrationDetected) {
|
||||
|
||||
Job job = (Job) context.getBean(name);
|
||||
String jobName = job.getName();
|
||||
|
||||
// On reload try to unregister first
|
||||
if (unregister) {
|
||||
logger.debug("Unregistering job: " + name + " from context: " + context.getDisplayName());
|
||||
jobRegistry.unregister(name);
|
||||
logger.debug("Unregistering job: " + jobName + " from context: " + context.getDisplayName());
|
||||
jobRegistry.unregister(jobName);
|
||||
}
|
||||
|
||||
logger.debug("Registering job: " + name + " from context: " + context.getDisplayName());
|
||||
JobFactory jobFactory = new ReferenceJobFactory((Job) context.getBean(name));
|
||||
logger.debug("Registering job: " + jobName + " from context: " + context.getDisplayName());
|
||||
JobFactory jobFactory = new ReferenceJobFactory(job);
|
||||
jobRegistry.register(jobFactory);
|
||||
jobsRegistered.add(name);
|
||||
jobsRegistered.add(jobName);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Collection<Job> result = new ArrayList<Job>();
|
||||
for (String name : jobsRegistered) {
|
||||
try {
|
||||
result.add(jobRegistry.getJob(name));
|
||||
}
|
||||
|
||||
@@ -18,7 +18,11 @@ package org.springframework.batch.core.configuration.support;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParametersIncrementer;
|
||||
import org.springframework.batch.core.configuration.JobRegistry;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
@@ -31,6 +35,16 @@ public class DefaultJobLoaderTests {
|
||||
|
||||
private DefaultJobLoader jobLoader = new DefaultJobLoader(registry);
|
||||
|
||||
@Test
|
||||
public void testLoadWithExplicitName() throws Exception {
|
||||
ClassPathXmlApplicationContextFactory factory = new ClassPathXmlApplicationContextFactory(
|
||||
new ByteArrayResource(JOB_XML.getBytes()));
|
||||
jobLoader.load(factory);
|
||||
assertEquals(1, registry.getJobNames().size());
|
||||
jobLoader.reload(factory);
|
||||
assertEquals(1, registry.getJobNames().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReload() throws Exception {
|
||||
ClassPathXmlApplicationContextFactory factory = new ClassPathXmlApplicationContextFactory(
|
||||
@@ -51,4 +65,29 @@ public class DefaultJobLoaderTests {
|
||||
assertEquals(1, registry.getJobNames().size());
|
||||
}
|
||||
|
||||
private static final String JOB_XML = String
|
||||
.format(
|
||||
"<beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
|
||||
+ "xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd'><bean class='%s$StubJob'/></beans>",
|
||||
DefaultJobLoaderTests.class.getName());
|
||||
|
||||
public static class StubJob implements Job {
|
||||
|
||||
public void execute(JobExecution execution) {
|
||||
}
|
||||
|
||||
public JobParametersIncrementer getJobParametersIncrementer() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return "job";
|
||||
}
|
||||
|
||||
public boolean isRestartable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user