/** * Register each of the objects as listeners. * * @param listeners an array of listener objects of known types. */ public void setChunkListeners(ChunkListener[] listeners) { for (int i = 0; i < listeners.length; i++) { registerChunkListener(listeners[i]); } }
/** * Public setter for the {@link Tasklet}. * * @param tasklet the {@link Tasklet} to set */ public void setTasklet(Tasklet tasklet) { this.tasklet = tasklet; if (tasklet instanceof StepExecutionListener) { registerStepExecutionListener((StepExecutionListener) tasklet); } }
/** * Register each of the streams for callbacks at the appropriate time in the * step. The {@link ItemReader} and {@link ItemWriter} are automatically * registered, but it doesn't hurt to also register them here. Injected * dependencies of the reader and writer are not automatically registered, * so if you implement {@link ItemWriter} using delegation to another object * which itself is a {@link ItemStream}, you need to register the delegate * here. * * @param streams an array of {@link ItemStream} objects. */ public void setStreams(ItemStream[] streams) { for (int i = 0; i < streams.length; i++) { registerStream(streams[i]); } }
private TaskletStep getStep(String[] strings, int commitInterval) throws Exception { TaskletStep step = new TaskletStep("stepName"); // Only process one item: RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(commitInterval)); step.setTasklet(new TestingChunkOrientedTasklet<>(getReader(strings), itemWriter, template)); step.setJobRepository(new JobRepositorySupport()); step.setTransactionManager(transactionManager); return step; }
@Test public void testNonRestartedJob() throws Exception { MockRestartableItemReader tasklet = new MockRestartableItemReader(); step.setTasklet(new TestingChunkOrientedTasklet<>(tasklet, itemWriter)); step.registerStream(tasklet); JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); step.execute(stepExecution); assertFalse(tasklet.isRestoreFromCalled()); assertTrue(tasklet.isGetExecutionAttributesCalled()); }
private void initializeTasklet() { tasklet = new SystemCommandTasklet(); tasklet.setEnvironmentParams(null); // inherit from parent process tasklet.setWorkingDirectory(null); // inherit from parent process tasklet.setSystemProcessExitCodeMapper(new SimpleSystemProcessExitCodeMapper()); tasklet.setTimeout(5000); // long enough timeout tasklet.setTerminationCheckInterval(500); tasklet.setCommand("invalid command, change value for successful execution"); tasklet.setInterruptOnCancel(true); tasklet.setTaskExecutor(new SimpleAsyncTaskExecutor()); }
protected Step createStep(String stepName) { return new TaskletStep(stepName); }
@Test public void testDifferentParametersOrder() throws Exception { adapter.setTargetMethod("execute2"); RepeatStatus repeatStatus = adapter.execute(stepContribution, chunkContext); assertEquals(RepeatStatus.FINISHED, repeatStatus); assertEquals(tasklet.getStepContribution(), stepContribution); assertEquals(tasklet.getChunkContext(), chunkContext); }
@Test public void testArgumentSubsetWithOnlyChunkContext() throws Exception { adapter.setTargetMethod("execute3"); RepeatStatus repeatStatus = adapter.execute(stepContribution, chunkContext); assertEquals(RepeatStatus.FINISHED, repeatStatus); assertEquals(tasklet.getChunkContext(), chunkContext); }
@Test public void testArgumentSubsetWithOnlyStepContributionAndCompatibleReturnTypeBoolean() throws Exception { adapter.setTargetMethod("execute8"); RepeatStatus repeatStatus = adapter.execute(stepContribution, chunkContext); assertEquals(RepeatStatus.FINISHED, repeatStatus); assertEquals(tasklet.getStepContribution(), stepContribution); }
@Test(expected = java.util.concurrent.ExecutionException.class) public void testExecuteException() throws Exception { String command = "non-sense-that-should-cause-exception-when-attempted-to-execute"; tasklet.setCommand(command); tasklet.afterPropertiesSet(); tasklet.execute(null, null); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); initializeTasklet(); tasklet.afterPropertiesSet(); tasklet.beforeStep(stepExecution); }
@Override public void run() { try { jobRepository.add(stepExecution); step.execute(stepExecution); } catch (JobInterruptedException e) { // do nothing... } } };
private TaskletStep getStep(String[] strings) throws Exception { return getStep(strings, 1); }
@Override public void close() throws ItemStreamException { super.close(); // Simulate failure on rollback when stream resets throw new RuntimeException("Bar"); } };
@Override public void update(ExecutionContext executionContext) { super.update(executionContext); executionContext.putString("foo", "bar"); } };
@Test public void testSameSignatureWithDifferentMethodName() throws Exception { adapter.setTargetMethod("execute1"); RepeatStatus repeatStatus = adapter.execute(stepContribution, chunkContext); assertEquals(RepeatStatus.FINISHED, repeatStatus); assertEquals(tasklet.getStepContribution(), stepContribution); assertEquals(tasklet.getChunkContext(), chunkContext); }
@Test public void testArgumentSubsetWithOnlyChunkContextAndCompatibleReturnTypeVoid() throws Exception { adapter.setTargetMethod("execute9"); RepeatStatus repeatStatus = adapter.execute(stepContribution, chunkContext); assertEquals(RepeatStatus.FINISHED, repeatStatus); assertEquals(tasklet.getChunkContext(), chunkContext); }
@Test public void testArgumentSubsetWithOnlyStepContribution() throws Exception { adapter.setTargetMethod("execute4"); RepeatStatus repeatStatus = adapter.execute(stepContribution, chunkContext); assertEquals(RepeatStatus.FINISHED, repeatStatus); assertEquals(tasklet.getStepContribution(), stepContribution); }
@Test public void testExactlySameSignature() throws Exception { adapter.setTargetMethod("execute"); RepeatStatus repeatStatus = adapter.execute(stepContribution, chunkContext); assertEquals(RepeatStatus.FINISHED, repeatStatus); assertEquals(tasklet.getStepContribution(), stepContribution); assertEquals(tasklet.getChunkContext(), chunkContext); }