Try CommandLineJobRunner with params from stdin
This commit is contained in:
@@ -15,14 +15,37 @@
|
||||
*/
|
||||
package org.springframework.batch.core.launch.support;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.core.*;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.ExitStatus;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.JobParameter;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersIncrementer;
|
||||
import org.springframework.batch.core.configuration.JobLocator;
|
||||
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
|
||||
import org.springframework.batch.core.converter.JobParametersConverter;
|
||||
import org.springframework.batch.core.explore.JobExplorer;
|
||||
import org.springframework.batch.core.launch.*;
|
||||
import org.springframework.batch.core.launch.JobExecutionNotFailedException;
|
||||
import org.springframework.batch.core.launch.JobExecutionNotRunningException;
|
||||
import org.springframework.batch.core.launch.JobExecutionNotStoppedException;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.core.launch.JobParametersNotFoundException;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
||||
@@ -31,8 +54,6 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Basic launcher for starting jobs from the command line. In general, it is
|
||||
@@ -482,7 +503,7 @@ public class CommandLineJobRunner {
|
||||
* command line.
|
||||
* </p>
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
CommandLineJobRunner command = new CommandLineJobRunner();
|
||||
|
||||
@@ -520,6 +541,13 @@ public class CommandLineJobRunner {
|
||||
command.exit(1);
|
||||
}
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
||||
String line = reader.readLine();
|
||||
while (line != null) {
|
||||
params.add(line);
|
||||
line = reader.readLine();
|
||||
}
|
||||
|
||||
String[] parameters = params.toArray(new String[params.size()]);
|
||||
|
||||
int result = command.start(jobPath, jobIdentifier, parameters, opts);
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.batch.core.launch.support;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
@@ -63,23 +64,37 @@ public class CommandLineJobRunnerTests {
|
||||
|
||||
private String[] args = new String[] { jobPath, jobName, jobKey, scheduleDate, vendorId };
|
||||
|
||||
private InputStream stdin;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
JobExecution jobExecution = new JobExecution(null, new Long(1));
|
||||
ExitStatus exitStatus = ExitStatus.COMPLETED;
|
||||
jobExecution.setExitStatus(exitStatus);
|
||||
StubJobLauncher.jobExecution = jobExecution;
|
||||
stdin = System.in;
|
||||
System.setIn(new InputStream() {
|
||||
public int read() {
|
||||
return -1;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
System.setIn(stdin);
|
||||
StubJobLauncher.tearDown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMain() {
|
||||
public void testMain() throws Exception {
|
||||
CommandLineJobRunner.main(args);
|
||||
assertTrue("Injected JobParametersConverter not used instead of default", StubJobParametersConverter.called);
|
||||
assertEquals(0, StubSystemExiter.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithJobLocator() {
|
||||
public void testWithJobLocator() throws Exception {
|
||||
jobPath = ClassUtils.addResourcePathToPackagePath(CommandLineJobRunnerTests.class, "launcher-with-locator.xml");
|
||||
CommandLineJobRunner.main(new String[] { jobPath, jobName, jobKey });
|
||||
assertTrue("Injected JobParametersConverter not used instead of default", StubJobParametersConverter.called);
|
||||
@@ -94,7 +109,7 @@ public class CommandLineJobRunnerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidArgs() {
|
||||
public void testInvalidArgs() throws Exception {
|
||||
String[] args = new String[] {};
|
||||
CommandLineJobRunner.presetSystemExiter(new StubSystemExiter());
|
||||
CommandLineJobRunner.main(args);
|
||||
@@ -104,7 +119,7 @@ public class CommandLineJobRunnerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrongJobName() {
|
||||
public void testWrongJobName() throws Exception {
|
||||
String[] args = new String[] { jobPath, "no-such-job" };
|
||||
CommandLineJobRunner.main(args);
|
||||
assertEquals(1, StubSystemExiter.status);
|
||||
@@ -121,6 +136,23 @@ public class CommandLineJobRunnerTests {
|
||||
assertEquals(new JobParameters(), StubJobLauncher.jobParameters);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithStdinParameters() throws Throwable {
|
||||
String[] args = new String[] { jobPath, jobName };
|
||||
System.setIn(new InputStream() {
|
||||
char[] input = ("foo=bar\nspam=bucket").toCharArray();
|
||||
|
||||
int index = 0;
|
||||
|
||||
public int read() {
|
||||
return index<input.length-1 ? (int) input[index++] : -1;
|
||||
}
|
||||
});
|
||||
CommandLineJobRunner.main(args);
|
||||
assertEquals(0, StubSystemExiter.status);
|
||||
assertEquals(2, StubJobLauncher.jobParameters.getParameters().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithInvalidParameters() throws Throwable {
|
||||
String[] args = new String[] { jobPath, jobName, "foo" };
|
||||
@@ -263,7 +295,7 @@ public class CommandLineJobRunnerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextWithNoParameters() {
|
||||
public void testNextWithNoParameters() throws Exception {
|
||||
jobPath = ClassUtils.addResourcePathToPackagePath(CommandLineJobRunnerTests.class, "launcher-with-locator.xml");
|
||||
CommandLineJobRunner.main(new String[] { jobPath, "-next", "test-job2", jobKey });
|
||||
assertEquals(1, StubSystemExiter.getStatus());
|
||||
@@ -279,11 +311,6 @@ public class CommandLineJobRunnerTests {
|
||||
assertTrue(StubJobLauncher.destroyed);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
StubJobLauncher.tearDown();
|
||||
}
|
||||
|
||||
public static class StubSystemExiter implements SystemExiter {
|
||||
|
||||
private static int status;
|
||||
@@ -329,7 +356,7 @@ public class CommandLineJobRunnerTests {
|
||||
destroyed = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class StubJobRepository extends JobRepositorySupport {
|
||||
}
|
||||
|
||||
@@ -367,7 +394,8 @@ public class CommandLineJobRunnerTests {
|
||||
return Arrays.asList(createJobExecution(jobInstance, BatchStatus.ABANDONED));
|
||||
}
|
||||
if (jobInstance.getId() == 5) {
|
||||
return Arrays.asList(createJobExecution(jobInstance, BatchStatus.STARTED), createJobExecution(jobInstance, BatchStatus.FAILED));
|
||||
return Arrays.asList(createJobExecution(jobInstance, BatchStatus.STARTED), createJobExecution(
|
||||
jobInstance, BatchStatus.FAILED));
|
||||
}
|
||||
return Arrays.asList(createJobExecution(jobInstance, BatchStatus.COMPLETED));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user