/** * Typesafe Getter for the String represented by the provided key with * default value to return if key is not represented. * * @param key The key to get a value for * @param defaultString Default to return if key is not represented * @return The <code>String</code> value if key is represented, specified * default otherwise */ public String getString(String key, String defaultString) { if (!containsKey(key)) { return defaultString; } return getString(key); }
@BeforeStep public void createOutputNameFromInput(StepExecution stepExecution) { ExecutionContext executionContext = stepExecution.getExecutionContext(); String inputName = stepExecution.getStepName().replace(":", "-"); if (executionContext.containsKey(inputKeyName)) { inputName = executionContext.getString(inputKeyName); } if (!executionContext.containsKey(outputKeyName)) { executionContext.putString(outputKeyName, path + FilenameUtils.getBaseName(inputName) + ".csv"); } }
@Override protected State nextState(String stateName, FlowExecutionStatus status, StepExecution stepExecution) throws FlowExecutionException { State nextState = findState(stateName, status, stepExecution); if(stepExecution != null) { ExecutionContext executionContext = stepExecution.getJobExecution().getExecutionContext(); if(executionContext.containsKey("batch.stoppedStep")) { String stepName = executionContext.getString("batch.stoppedStep"); if(stateName.endsWith(stepName)) { if(nextState != null && executionContext.containsKey("batch.restartStep") && StringUtils.hasText(executionContext.getString("batch.restartStep"))) { nextState = findState(stateName, new FlowExecutionStatus(status.getName() + ".RESTART"), stepExecution); } } } } return nextState; }
private void saveAndAssert(final StepExecution stepExecution, final String value) { new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus status) { stepExecution.getExecutionContext().put("foo", value); dao.saveExecutionContext(stepExecution); return null; } }); ExecutionContext executionContext = dao.getExecutionContext(stepExecution); Assert.state(executionContext != null, "Lost insert: null executionContext at value=" + value); String foo = executionContext.getString("foo"); Assert.state(value.equals(foo), "Lost update: wrong value=" + value + " (found " + foo + ") for id=" + stepExecution.getId()); }
@Test public void testReadFile() throws Exception { Map<String, ExecutionContext> partition = partitioner.partition(0); String url = partition.get("partition0").getString("fileName"); assertTrue(new UrlResource(url).exists()); }
@Test public void testSetInputKeyName() { listener.setPath(""); listener.setInputKeyName("spam"); stepExecution.getExecutionContext().putString("spam", "bar"); listener.createOutputNameFromInput(stepExecution); assertEquals("bar.csv", stepExecution.getExecutionContext().getString("outputFile")); } }
@Test public void testExecutionContext() throws Exception { assertNotNull(execution.getExecutionContext()); ExecutionContext context = new ExecutionContext(); context.putString("foo", "bar"); execution.setExecutionContext(context); assertEquals("bar", execution.getExecutionContext().getString("foo")); }
@Test public void testNormalUsage() { context.putString("1", "testString1"); context.putString("2", "testString2"); context.putLong("3", 3); context.putDouble("4", 4.4); context.putInt("5", 5); assertEquals("testString1", context.getString("1")); assertEquals("testString2", context.getString("2")); assertEquals("defaultString", context.getString("55", "defaultString")); assertEquals(4.4, context.getDouble("4"), 0); assertEquals(5.5, context.getDouble("55", 5.5), 0); assertEquals(3, context.getLong("3")); assertEquals(5, context.getLong("55", 5)); assertEquals(5, context.getInt("5")); assertEquals(6, context.getInt("55", 6)); }
@Override public Collection<StepExecution> handle(StepExecutionSplitter stepSplitter, StepExecution stepExecution) throws Exception { Set<StepExecution> executions = stepSplitter.split(stepExecution, 2); if (!started.get()) { started.set(true); for (StepExecution execution : executions) { execution.setStatus(BatchStatus.FAILED); execution.setExitStatus(ExitStatus.FAILED); execution.getExecutionContext().putString("foo", execution.getStepName()); } } else { for (StepExecution execution : executions) { // On restart the execution context should have been restored assertEquals(execution.getStepName(), execution.getExecutionContext().getString("foo")); } } for (StepExecution execution : executions) { jobRepository.update(execution); jobRepository.updateExecutionContext(execution); } return executions; } });
@Test public void testLaunchJob() throws Exception { JobExecution jobExecution = jobLauncherTestUtils.launchJob(new JobParametersBuilder().addString("value", "foo") .toJobParameters()); assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); assertEquals("yes", jobExecution.getExecutionContext().getString("done")); }
/** * CONDITION: keys = {key}. key is already in job but not in step. * * EXPECTED: key is not erased. */ @Test public void promoteEntriesKeyNotFoundInStep() throws Exception { ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener(); JobExecution jobExecution = new JobExecution(1L); StepExecution stepExecution = jobExecution.createStepExecution("step1"); stepExecution.setExitStatus(ExitStatus.COMPLETED); Assert.state(jobExecution.getExecutionContext().isEmpty(), "Job ExecutionContext is not empty"); Assert.state(stepExecution.getExecutionContext().isEmpty(), "Step ExecutionContext is not empty"); jobExecution.getExecutionContext().putString(key, value); listener.setKeys(new String[] { key }); listener.afterPropertiesSet(); listener.afterStep(stepExecution); assertEquals(value, jobExecution.getExecutionContext().getString(key)); }
/** * Execution context must not be left empty even if job failed before * committing first chunk - otherwise ItemStreams won't recognize it is * restart scenario on next run. */ @Test public void testRestartAfterFailureInFirstChunk() throws Exception { MockRestartableItemReader reader = new MockRestartableItemReader() { @Override public String read() throws RuntimeException { // fail on the very first item throw new RuntimeException("CRASH!"); } }; step.setTasklet(new TestingChunkOrientedTasklet<>(reader, itemWriter)); step.registerStream(reader); StepExecution stepExecution = new StepExecution(step.getName(), new JobExecution(jobInstance, jobParameters)); step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); Throwable expected = stepExecution.getFailureExceptions().get(0); assertEquals("CRASH!", expected.getMessage()); assertFalse(stepExecution.getExecutionContext().isEmpty()); assertTrue(stepExecution.getExecutionContext().getString("spam").equals("bucket")); }
@Test public void testDirectlyInjectedItemStream() throws Exception { step.setStreams(new ItemStream[] { new ItemStreamSupport() { @Override public void update(ExecutionContext executionContext) { super.update(executionContext); executionContext.putString("foo", "bar"); } } }); JobExecution jobExecution = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); assertEquals(false, stepExecution.getExecutionContext().containsKey("foo")); step.execute(stepExecution); assertEquals("bar", stepExecution.getExecutionContext().getString("foo")); }
/** * CONDITION: keys = {key, key2}. Only {key} exists in the ExecutionContext. * * EXPECTED: key is promoted. key2 is not. */ @Test public void promoteEntriesKeyNotFound() throws Exception { ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener(); JobExecution jobExecution = new JobExecution(1L); StepExecution stepExecution = jobExecution.createStepExecution("step1"); stepExecution.setExitStatus(ExitStatus.COMPLETED); Assert.state(jobExecution.getExecutionContext().isEmpty(), "Job ExecutionContext is not empty"); Assert.state(stepExecution.getExecutionContext().isEmpty(), "Step ExecutionContext is not empty"); stepExecution.getExecutionContext().putString(key, value); listener.setKeys(new String[] { key, key2 }); listener.afterPropertiesSet(); listener.afterStep(stepExecution); assertEquals(value, jobExecution.getExecutionContext().getString(key)); assertFalse(jobExecution.getExecutionContext().containsKey(key2)); }
/** * CONDITION: ExecutionContext contains {key, key2}. keys = {key}. statuses * is not set (defaults to {COMPLETED}). * * EXPECTED: key is promoted. key2 is not. */ @Test public void promoteEntryNullStatuses() throws Exception { ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener(); JobExecution jobExecution = new JobExecution(1L); StepExecution stepExecution = jobExecution.createStepExecution("step1"); stepExecution.setExitStatus(ExitStatus.COMPLETED); Assert.state(jobExecution.getExecutionContext().isEmpty(), "Job ExecutionContext is not empty"); Assert.state(stepExecution.getExecutionContext().isEmpty(), "Step ExecutionContext is not empty"); stepExecution.getExecutionContext().putString(key, value); stepExecution.getExecutionContext().putString(key2, value2); listener.setKeys(new String[] { key }); listener.afterPropertiesSet(); listener.afterStep(stepExecution); assertEquals(value, jobExecution.getExecutionContext().getString(key)); assertFalse(jobExecution.getExecutionContext().containsKey(key2)); }
/** * CONDITION: strict = true. keys = {key, key2}. Only {key} exists in the * ExecutionContext. * * EXPECTED: IllegalArgumentException */ @Test(expected = IllegalArgumentException.class) public void promoteEntriesKeyNotFoundStrict() throws Exception { ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener(); listener.setStrict(true); JobExecution jobExecution = new JobExecution(1L); StepExecution stepExecution = jobExecution.createStepExecution("step1"); stepExecution.setExitStatus(ExitStatus.COMPLETED); Assert.state(jobExecution.getExecutionContext().isEmpty(), "Job ExecutionContext is not empty"); Assert.state(stepExecution.getExecutionContext().isEmpty(), "Step ExecutionContext is not empty"); stepExecution.getExecutionContext().putString(key, value); listener.setKeys(new String[] { key, key2 }); listener.afterPropertiesSet(); listener.afterStep(stepExecution); assertEquals(value, jobExecution.getExecutionContext().getString(key)); assertFalse(jobExecution.getExecutionContext().containsKey(key2)); }
/** * CONDITION: keys = {key, key2}. statuses = {statusWildcard}. ExitStatus = * status * * EXPECTED: key is promoted. key2 is not. */ @Test public void promoteEntryStatusWildcardFound() throws Exception { ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener(); JobExecution jobExecution = new JobExecution(1L); StepExecution stepExecution = jobExecution.createStepExecution("step1"); stepExecution.setExitStatus(new ExitStatus(status)); Assert.state(jobExecution.getExecutionContext().isEmpty(), "Job ExecutionContext is not empty"); Assert.state(stepExecution.getExecutionContext().isEmpty(), "Step ExecutionContext is not empty"); stepExecution.getExecutionContext().putString(key, value); stepExecution.getExecutionContext().putString(key2, value2); listener.setKeys(new String[] { key }); listener.setStatuses(new String[] { statusWildcard }); listener.afterPropertiesSet(); listener.afterStep(stepExecution); assertEquals(value, jobExecution.getExecutionContext().getString(key)); assertFalse(jobExecution.getExecutionContext().containsKey(key2)); }
/** * CONDITION: ExecutionContext contains {key, key2}. keys = {key, key2}. * statuses = {status}. ExitStatus = status * * EXPECTED: key is promoted. key2 is not. */ @Test public void promoteEntryStatusFound() throws Exception { ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener(); listener.setStrict(true); JobExecution jobExecution = new JobExecution(1L); StepExecution stepExecution = jobExecution.createStepExecution("step1"); stepExecution.setExitStatus(new ExitStatus(status)); Assert.state(jobExecution.getExecutionContext().isEmpty(), "Job ExecutionContext is not empty"); Assert.state(stepExecution.getExecutionContext().isEmpty(), "Step ExecutionContext is not empty"); stepExecution.getExecutionContext().putString(key, value); stepExecution.getExecutionContext().putString(key2, value2); listener.setKeys(new String[] { key }); listener.setStatuses(new String[] { status }); listener.afterPropertiesSet(); listener.afterStep(stepExecution); assertEquals(value, jobExecution.getExecutionContext().getString(key)); assertFalse(jobExecution.getExecutionContext().containsKey(key2)); }
@Test public void testStreamManager() throws Exception { MockRestartableItemReader reader = new MockRestartableItemReader() { @Override public String read() { return "foo"; } @Override public void update(ExecutionContext executionContext) { super.update(executionContext); executionContext.putString("foo", "bar"); } }; step.setTasklet(new TestingChunkOrientedTasklet<>(reader, itemWriter)); step.registerStream(reader); JobExecution jobExecution = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); assertEquals(false, stepExecution.getExecutionContext().containsKey("foo")); step.execute(stepExecution); // At least once in that process the statistics service was asked for // statistics... assertEquals("bar", stepExecution.getExecutionContext().getString("foo")); }
@Test public void testDirectlyInjectedStreamWhichIsAlsoReader() throws Exception { MockRestartableItemReader reader = new MockRestartableItemReader() { @Override public String read() { return "foo"; } @Override public void update(ExecutionContext executionContext) { super.update(executionContext); executionContext.putString("foo", "bar"); } }; step.setTasklet(new TestingChunkOrientedTasklet<>(reader, itemWriter)); step.setStreams(new ItemStream[] { reader }); JobExecution jobExecution = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); assertEquals(false, stepExecution.getExecutionContext().containsKey("foo")); step.execute(stepExecution); // At least once in that process the statistics service was asked for // statistics... assertEquals("bar", stepExecution.getExecutionContext().getString("foo")); }