BATCH-304: Created new job launcher that does not use BeanFactoryLocator.

This commit is contained in:
lucasward
2008-01-24 13:40:43 +00:00
parent 3f6f0a94e7
commit d922345309
5 changed files with 321 additions and 0 deletions

View File

@@ -0,0 +1,179 @@
/*
* Copyright 2006-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.execution.bootstrap.support;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.domain.IncorrectJobCountException;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobParameters;
import org.springframework.batch.core.domain.NoSuchJobException;
import org.springframework.batch.core.executor.ExitCodeExceptionClassifier;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.runtime.JobParametersFactory;
import org.springframework.batch.execution.launch.JobLauncher;
import org.springframework.batch.execution.step.simple.SimpleExitCodeExceptionClassifier;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.beans.factory.access.SingletonBeanFactoryLocator;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;
/**
* @author Lucas Ward
*
*/
public class CommandLineJobRunner {
protected static final Log logger = LogFactory
.getLog(CommandLineJobRunner.class);
private ExitCodeMapper exitCodeMapper = new SimpleJvmExitCodeMapper();
private ExitCodeExceptionClassifier exceptionClassifier = new SimpleExitCodeExceptionClassifier();
private JobLauncher launcher;
private SystemExiter systemExiter = new JvmSystemExiter();
private JobParametersFactory jobParametersFactory = new ScheduledJobParametersFactory();
/**
* Injection setter for the {@link JobLauncher}.
*
* @param launcher
* the launcher to set
*/
public void setLauncher(JobLauncher launcher) {
this.launcher = launcher;
}
/**
* Injection setter for the {@link ExitCodeExceptionClassifier}
*
* @param exceptionClassifier
*/
public void setExceptionClassifier(
ExitCodeExceptionClassifier exceptionClassifier) {
this.exceptionClassifier = exceptionClassifier;
}
/**
* Injection setter for the {@link JvmExitCodeMapper}.
*
* @param exitCodeMapper
* the exitCodeMapper to set
*/
public void setExitCodeMapper(ExitCodeMapper exitCodeMapper) {
this.exitCodeMapper = exitCodeMapper;
}
/**
* Injection setter for the {@link SystemExiter}.
*
* @param systemExitor
*/
public void setSystemExiter(SystemExiter systemExitor) {
this.systemExiter = systemExitor;
}
/**
* Delegate to the exiter to (possibly) exit the VM gracefully.
*
* @param status
*/
public void exit(int status) {
systemExiter.exit(status);
}
int start(String jobPath, String environmentPath, String[] parameters) {
try {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
jobPath, environmentPath });
context.getAutowireCapableBeanFactory().autowireBeanProperties(this,
AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
Job job = getJob(context);
JobParameters jobParameters = jobParametersFactory.getJobParameters(StringUtils
.splitArrayElementsIntoProperties(parameters, "="));
JobExecution jobExecution = launcher.run(job, jobParameters);
return exitCodeMapper.getExitCode(jobExecution.getExitStatus()
.getExitCode());
} catch (Throwable e) {
logger.error("Job Terminated in error:", e);
return exitCodeMapper.getExitCode(exceptionClassifier
.classifyForExitCode(e).getExitCode());
}
}
private Job getJob(ApplicationContext context)
throws IncorrectJobCountException, NoSuchJobException {
String[] jobs = context.getBeanNamesForType(Job.class);
if (jobs.length > 1) {
logger.error("More than one job exists in the provided context: [" + "jobPath" + "]");
throw new IncorrectJobCountException(
"More than one job exists in the provided context. Bean Names: ["
+ jobs + "]");
} else if (jobs.length == 0) {
throw new NoSuchJobException("No jobs found in the provided context.");
}
return (Job) context.getBean(jobs[0]);
}
/**
* Launch a batch job using a {@link SimpleCommandLineJobRunner}. Creates a
* new Spring context for the job execution, and uses a common parent for
* all such contexts. No exception are thrown from this method, rather
* exceptions are logged and an integer returned through the exit status in
* a {@link JvmSystemExiter} (which can be overridden by defining one in the
* Spring context).
*
* @param args
* <ul>
* <li>-Djob.configuration.path: the classpath location of the
* JobConfiguration to use
* <li>-Djob.name: job name to be passed to the
* {@link JobLauncher}
* <li>-Dbatch.execution.environment.key: the key in
* beanRefContext.xml used to load the execution environment
* which will be the parent context for the job execution
* (mandatory if -Dbean.ref.context is specified).
* <li>-Dbean.ref.context: the location for beanRefContext.xml
* (optional, default is to only use the context specified in the
* job.configuration.path) (@see
* {@link SingletonBeanFactoryLocator}).</li>
* </ul>
*/
public static void main(String[] args) {
String jobPath = args[0];
String executionPath = args[1];
String[] parameters = new String[args.length - 2];
System.arraycopy(args, 2, parameters, 0, args.length - 2);
CommandLineJobRunner command = new CommandLineJobRunner();
int result = command.start(jobPath, executionPath, parameters);
command.exit(result);
}
}

View File

@@ -18,6 +18,7 @@ public interface ExitCodeMapper {
static int JVM_EXITCODE_JOB_ERROR = 2;
public static final String NO_SUCH_JOB = "NO_SUCH_JOB";
public static final String JOB_NOT_PROVIDED = "JOB_NOT_PROVIDED";
public static final String MULTIPLE_JOBS_FOUND = "MULTIPLE_JOBS_FOUND";
/**
* Transform the exitcode known by the batchframework into an exitcode in the

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2006-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.execution.bootstrap.support;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobParameters;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.execution.launch.JobLauncher;
import org.springframework.batch.repeat.ExitStatus;
import junit.framework.TestCase;
/**
* @author Lucas Ward
*
*/
public class CommandLineJobRunnerTests extends TestCase {
private static final String JOB = "org/springframework/batch/execution/bootstrap/support/job.xml";
private static final String TEST_BATCH_ENVIRONMENT = "org/springframework/batch/execution/bootstrap/support/test-environment.xml";
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
}
public void testMain(){
String jobPath = JOB;
String environmentPath = TEST_BATCH_ENVIRONMENT;
String jobKey = "job.Key=myKey";
String scheduleDate = "schedule.Date=01/23/2008";
String vendorId = "vendor.id=33243243";
String[] args = new String[]{jobPath, environmentPath, jobKey, scheduleDate, vendorId};
JobExecution jobExecution = new JobExecution(null, new Long(1));
ExitStatus exitStatus = ExitStatus.FINISHED;
jobExecution.setExitStatus(exitStatus);
StubJobLauncher.jobExecution = jobExecution;
CommandLineJobRunner.main(args);
assertEquals(0, StubSystemExiter.getStatus());
}
public static class StubSystemExiter implements SystemExiter {
public static int status;
public void exit(int status) {
StubSystemExiter.status = status;
}
public static int getStatus() {
return status;
}
}
public static class StubJobLauncher implements JobLauncher{
public static JobExecution jobExecution;
public static boolean throwExecutionRunningException = false;
public JobExecution run(Job job, JobParameters jobParameters)
throws JobExecutionAlreadyRunningException {
if(throwExecutionRunningException){
throw new JobExecutionAlreadyRunningException("");
}
return jobExecution;
}
}
}

View File

@@ -0,0 +1,31 @@
<?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.domain.Job">
<property name="steps">
<bean id="step1" class="org.springframework.batch.execution.step.SimpleStep">
<constructor-arg>
<bean
class="org.springframework.batch.execution.tasklet.ItemOrientedTasklet">
<property name="itemReader">
<bean
class="org.springframework.batch.item.reader.ListItemReader">
<constructor-arg value="foo,bar,spam"/>
</bean>
</property>
<property name="itemProcessor">
<bean class="org.springframework.batch.execution.launch.EmptyItemProcessor"/>
</property>
</bean>
</constructor-arg>
</bean>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,19 @@
<?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="simpleContainerLauncher" class="org.springframework.batch.execution.bootstrap.support.CommandLineJobRunnerTests$StubJobLauncher" />
<bean id="jobConfigurationRegistry" class="org.springframework.batch.execution.configuration.MapJobRegistry"/>
<bean class="org.springframework.batch.execution.bootstrap.support.SimpleJvmExitCodeMapper" />
<bean
class="org.springframework.batch.execution.bootstrap.support.CommandLineJobRunnerTests$StubSystemExiter" />
</beans>