From d9223453095b60c387e0551605cbee132f83b26a Mon Sep 17 00:00:00 2001 From: lucasward Date: Thu, 24 Jan 2008 13:40:43 +0000 Subject: [PATCH] BATCH-304: Created new job launcher that does not use BeanFactoryLocator. --- .../support/CommandLineJobRunner.java | 179 ++++++++++++++++++ .../bootstrap/support/ExitCodeMapper.java | 1 + .../support/CommandLineJobRunnerTests.java | 91 +++++++++ .../batch/execution/bootstrap/support/job.xml | 31 +++ .../bootstrap/support/test-environment.xml | 19 ++ 5 files changed, 321 insertions(+) create mode 100644 spring-batch-execution/src/main/java/org/springframework/batch/execution/bootstrap/support/CommandLineJobRunner.java create mode 100644 spring-batch-execution/src/test/java/org/springframework/batch/execution/bootstrap/support/CommandLineJobRunnerTests.java create mode 100644 spring-batch-execution/src/test/resources/org/springframework/batch/execution/bootstrap/support/job.xml create mode 100644 spring-batch-execution/src/test/resources/org/springframework/batch/execution/bootstrap/support/test-environment.xml diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/bootstrap/support/CommandLineJobRunner.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/bootstrap/support/CommandLineJobRunner.java new file mode 100644 index 000000000..4c50955ad --- /dev/null +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/bootstrap/support/CommandLineJobRunner.java @@ -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 + * + */ + 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); + } + +} diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/bootstrap/support/ExitCodeMapper.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/bootstrap/support/ExitCodeMapper.java index 41d74e7bd..41afe4af3 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/bootstrap/support/ExitCodeMapper.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/bootstrap/support/ExitCodeMapper.java @@ -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 diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/bootstrap/support/CommandLineJobRunnerTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/bootstrap/support/CommandLineJobRunnerTests.java new file mode 100644 index 000000000..3de4fd17a --- /dev/null +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/bootstrap/support/CommandLineJobRunnerTests.java @@ -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; + } + } +} diff --git a/spring-batch-execution/src/test/resources/org/springframework/batch/execution/bootstrap/support/job.xml b/spring-batch-execution/src/test/resources/org/springframework/batch/execution/bootstrap/support/job.xml new file mode 100644 index 000000000..c99206abd --- /dev/null +++ b/spring-batch-execution/src/test/resources/org/springframework/batch/execution/bootstrap/support/job.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-execution/src/test/resources/org/springframework/batch/execution/bootstrap/support/test-environment.xml b/spring-batch-execution/src/test/resources/org/springframework/batch/execution/bootstrap/support/test-environment.xml new file mode 100644 index 000000000..57f320612 --- /dev/null +++ b/spring-batch-execution/src/test/resources/org/springframework/batch/execution/bootstrap/support/test-environment.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + +