BATCH-793:Added new class with simpler configuration

This commit is contained in:
lucasward
2008-08-27 22:40:01 +00:00
parent a3d851a726
commit 6ef4c3b44a
4 changed files with 262 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
/**
*
*/
package org.springframework.batch.core.configuration.support;
import java.util.Collection;
import java.util.List;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.configuration.DuplicateJobException;
import org.springframework.batch.core.configuration.JobFactory;
import org.springframework.batch.core.configuration.ListableJobRegistry;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
/**
* Implementation of the {@link ListableJobRegistry} interface that assumes all Jobs will be loaded from
* ClassPathXml resources.
*
* @author Lucas Ward
* @since 2.0
*/
public class ClassPathXmlJobRegistry implements ListableJobRegistry, ApplicationContextAware, InitializingBean {
List<Resource> jobPaths;
ApplicationContext parent;
ListableJobRegistry jobRegistry;
public ClassPathXmlJobRegistry(List<Resource> jobPaths) {
this.jobPaths = jobPaths;
}
public Job getJob(String name) throws NoSuchJobException {
return jobRegistry.getJob(name);
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
parent = applicationContext;
}
public void afterPropertiesSet() throws Exception {
jobRegistry = new MapJobRegistry();
for(Resource resource:jobPaths){
ClassPathXmlApplicationContextFactory applicationContextFactory = new ClassPathXmlApplicationContextFactory();
applicationContextFactory.setPath(new Resource[]{resource});
applicationContextFactory.setApplicationContext(parent);
ApplicationContext context = applicationContextFactory.createApplicationContext();
String[] names = context.getBeanNamesForType(Job.class);
if(names.length > 1){
throw new DuplicateJobException("More than one Job found for resource: [" + resource + "]");
}
else if(names.length == 0){
throw new NoSuchJobException("No Jobs found in resource: [" + resource + "]");
}
ApplicationContextJobFactory jobFactory = new ApplicationContextJobFactory(applicationContextFactory, names[0]);
jobRegistry.register(jobFactory);
}
}
public Collection<String> getJobNames() {
return jobRegistry.getJobNames();
}
public void register(JobFactory jobFactory) throws DuplicateJobException {
jobRegistry.register(jobFactory);
}
public void unregister(String jobName) {
jobRegistry.unregister(jobName);
}
}

View File

@@ -0,0 +1,69 @@
package org.springframework.batch.core.configuration.support;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.configuration.DuplicateJobException;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class ClassPathXmlJobRegistryTests {
ClassPathXmlJobRegistry registry;
@Test
public void testLocateJob() throws Exception{
List<Resource> jobPaths = new ArrayList<Resource>();
jobPaths.add(new ClassPathResource("org/springframework/batch/core/launch/support/job.xml"));
jobPaths.add(new ClassPathResource("org/springframework/batch/core/launch/support/job2.xml"));
registry = new ClassPathXmlJobRegistry(jobPaths);
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
registry.setApplicationContext(applicationContext);
registry.afterPropertiesSet();
Collection<String> names = registry.getJobNames();
assertEquals(2, names.size());
assertTrue(names.contains("test-job"));
assertTrue(names.contains("test-job2"));
Job job = registry.getJob("test-job");
assertEquals("test-job", job.getName());
job = registry.getJob("test-job2");
assertEquals("test-job2", job.getName());
}
@Test(expected=NoSuchJobException.class)
public void testNoJobFound() throws Exception{
List<Resource> jobPaths = new ArrayList<Resource>();
jobPaths.add(new ClassPathResource("org/springframework/batch/core/launch/support/test-environment.xml"));
registry = new ClassPathXmlJobRegistry(jobPaths);
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
registry.setApplicationContext(applicationContext);
registry.afterPropertiesSet();
}
@Test(expected=DuplicateJobException.class)
public void testDuplicateJobsInFile() throws Exception{
List<Resource> jobPaths = new ArrayList<Resource>();
jobPaths.add(new ClassPathResource("org/springframework/batch/core/launch/support/2jobs.xml"));
registry = new ClassPathXmlJobRegistry(jobPaths);
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
registry.setApplicationContext(applicationContext);
registry.afterPropertiesSet();
}
}

View File

@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="test-job"
class="org.springframework.batch.core.job.SimpleJob">
<property name="jobRepository">
<bean class="org.springframework.batch.core.step.JobRepositorySupport" />
</property>
<property name="steps">
<bean id="step1"
class="org.springframework.batch.core.step.item.SimpleStepFactoryBean">
<property name="itemReader">
<bean
class="org.springframework.batch.item.support.ListItemReader">
<constructor-arg value="foo,bar,spam" />
</bean>
</property>
<property name="itemWriter">
<bean
class="org.springframework.batch.core.launch.EmptyItemWriter" />
</property>
<property name="jobRepository">
<bean
class="org.springframework.batch.core.step.JobRepositorySupport" />
</property>
<property name="transactionManager">
<bean
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
</property>
</bean>
</property>
</bean>
<bean id="test-job2"
class="org.springframework.batch.core.job.SimpleJob">
<property name="jobRepository">
<bean class="org.springframework.batch.core.step.JobRepositorySupport" />
</property>
<property name="steps">
<bean id="step2"
class="org.springframework.batch.core.step.item.SimpleStepFactoryBean">
<property name="itemReader">
<bean
class="org.springframework.batch.item.support.ListItemReader">
<constructor-arg value="foo,bar,spam" />
</bean>
</property>
<property name="itemWriter">
<bean
class="org.springframework.batch.core.launch.EmptyItemWriter" />
</property>
<property name="jobRepository">
<bean
class="org.springframework.batch.core.step.JobRepositorySupport" />
</property>
<property name="transactionManager">
<bean
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
</property>
</bean>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="test-job2"
class="org.springframework.batch.core.job.SimpleJob">
<property name="jobRepository">
<bean class="org.springframework.batch.core.step.JobRepositorySupport" />
</property>
<property name="steps">
<bean id="step1"
class="org.springframework.batch.core.step.item.SimpleStepFactoryBean">
<property name="itemReader">
<bean
class="org.springframework.batch.item.support.ListItemReader">
<constructor-arg value="foo,bar,spam" />
</bean>
</property>
<property name="itemWriter">
<bean
class="org.springframework.batch.core.launch.EmptyItemWriter" />
</property>
<property name="jobRepository">
<bean
class="org.springframework.batch.core.step.JobRepositorySupport" />
</property>
<property name="transactionManager">
<bean
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
</property>
</bean>
</property>
</bean>
</beans>