/** * @return a {@link CompletionPolicy} consistent with the chunk size and injected policy (if present). */ protected CompletionPolicy getChunkCompletionPolicy() { Assert.state(!(completionPolicy != null && chunkSize > 0), "You must specify either a chunkCompletionPolicy or a commitInterval but not both."); Assert.state(chunkSize >= 0, "The commitInterval must be positive or zero (for default value)."); if (completionPolicy != null) { return completionPolicy; } if (chunkSize == 0) { logger.info("Setting commit interval to default value (" + DEFAULT_COMMIT_INTERVAL + ")"); chunkSize = DEFAULT_COMMIT_INTERVAL; } return new SimpleCompletionPolicy(chunkSize); }
/** * Set up a {@link SimpleCompletionPolicy} with a commit interval taken from * the {@link JobParameters}. If there is a Long parameter with the given * key name, the intValue of this parameter is used. If not an exception * will be thrown. * * @see org.springframework.batch.core.listener.StepExecutionListenerSupport#beforeStep(org.springframework.batch.core.StepExecution) */ @Override public void beforeStep(StepExecution stepExecution) { JobParameters jobParameters = stepExecution.getJobParameters(); Assert.state(jobParameters.getParameters().containsKey(keyName), "JobParameters do not contain Long parameter with key=[" + keyName + "]"); delegate = new SimpleCompletionPolicy(jobParameters.getLong(keyName).intValue()); }
public RepeatTemplate getRepeatTemplate() { template = new RepeatTemplate(); // default stop after more items than exist in dataset template.setCompletionPolicy(new SimpleCompletionPolicy(8)); return template; }
@Test public void testReceiveAndExecuteWithCallbackReturningNull() throws Exception { RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(2)); container = getContainer(template); Session session = mock(Session.class); MessageConsumer consumer = mock(MessageConsumer.class); Message message = null; // Expect one call to consumer (chunk size is 2 but terminates on // first)... when(consumer.receive(1000)).thenReturn(message); when(session.getTransacted()).thenReturn(false); boolean received = doExecute(session, consumer); assertFalse("Message not received", received); }
@Test public void testReceiveAndExecuteWithCallback() throws Exception { RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(2)); container = getContainer(template); container.setMessageListener(new MessageListener() { @Override public void onMessage(Message arg0) { } }); Session session = mock(Session.class); MessageConsumer consumer = mock(MessageConsumer.class); Message message = mock(Message.class); // Expect two calls to consumer (chunk size)... when(session.getTransacted()).thenReturn(true); when(session.getTransacted()).thenReturn(true); when(consumer.receive(1000)).thenReturn(message); boolean received = doExecute(session, consumer); assertTrue("Message not received", received); }
public RepeatTemplate getRepeatTemplate() { TaskExecutorRepeatTemplate template = new TaskExecutorRepeatTemplate(); template.setTaskExecutor(new SimpleAsyncTaskExecutor()); // Set default completion above number of items in input file template.setCompletionPolicy(new SimpleCompletionPolicy(8)); return template; }
@Test public void testThrottleLimitWithEarlyCompletion() throws Exception { early = 2; template.setCompletionPolicy(new SimpleCompletionPolicy(10)); template.iterate(callback); int frequency = Collections.frequency(items, "null"); assertEquals(10, items.size() - frequency); // System.err.println("Frequency: " + frequency); assertEquals(0, frequency); }
/** * Check that a dedicated TerminationPolicy can terminate the batch. * * @throws Exception */ @Test public void testEarlyCompletionWithPolicy() throws Exception { template.setCompletionPolicy(new SimpleCompletionPolicy(2)); template.iterate(new ItemReaderRepeatCallback<>(provider, processor)); assertEquals(2, processor.count); }
@Test public void testNonTransactionalReceiveAndExecuteWithCallbackThrowingException() throws Exception { RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(2)); container = getContainer(template); container.setSessionTransacted(false); boolean received = doTestWithException(new IllegalStateException("No way!"), false, 2); assertTrue("Message not received but listener not transactional so this should be true", received); }
@Test public void testTransactionalReceiveAndExecuteWithCallbackThrowingException() throws Exception { RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(2)); container = getContainer(template); container.setSessionTransacted(true); try { boolean received = doTestWithException(new IllegalStateException("No way!"), true, 2); assertFalse("Message received", received); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { assertEquals("No way!", e.getMessage()); } }
public void testInterceptorChainWithRetry() throws Exception { ((Advised) service).addAdvice(interceptor); final List<Object> list = new ArrayList<>(); ((Advised) service).addAdvice(new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { list.add("chain"); return invocation.proceed(); } }); RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(2)); interceptor.setRepeatOperations(template); service.service(); assertEquals(2, target.count); assertEquals(2, list.size()); }
@Test public void testNonTransactionalReceiveAndExecuteWithCallbackThrowingError() throws Exception { RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(2)); container = getContainer(template); container.setSessionTransacted(false); try { boolean received = doTestWithException(new RuntimeException("No way!"), false, 2); assertTrue("Message not received but listener not transactional so this should be true", received); } catch (RuntimeException e) { assertEquals("No way!", e.getMessage()); fail("Unexpected Error - should be swallowed"); } }
public void testVoidServiceSunnyDay() throws Exception { ((Advised) service).addAdvice(interceptor); RepeatTemplate template = new RepeatTemplate(); // N.B. the default completion policy results in an infinite loop, so we // need to set the chunk size. template.setCompletionPolicy(new SimpleCompletionPolicy(2)); interceptor.setRepeatOperations(template); service.alternate(); assertEquals(2, target.count); }
public void testCallbackWithBoolean() throws Exception { RepeatTemplate template = new RepeatTemplate(); // N.B. the default completion policy results in an infinite loop, so we // need to set the chunk size. template.setCompletionPolicy(new SimpleCompletionPolicy(2)); interceptor.setRepeatOperations(template); ((Advised) service).addAdvice(interceptor); assertTrue(service.isContinuable()); assertEquals(2, target.count); }
/** * Commit interval specified is not allowed to be zero or negative. * @throws Exception */ @Test public void testCommitIntervalAndCompletionPolicyBothSet() throws Exception { SimpleStepFactoryBean<String, String> factory = getStepFactory("foo"); // but exception expected after setting commit interval and completion // policy factory.setCommitInterval(1); factory.setChunkCompletionPolicy(new SimpleCompletionPolicy(2)); try { factory.getObject(); fail(); } catch (IllegalStateException e) { // expected } }
@Override protected void setChunk(SimpleStepBuilder<I, O> builder) { if(timeout != null && getCommitInterval() != null) { CompositeCompletionPolicy completionPolicy = new CompositeCompletionPolicy(); CompletionPolicy [] policies = new CompletionPolicy[2]; policies[0] = new SimpleCompletionPolicy(getCommitInterval()); policies[1] = new TimeoutTerminationPolicy(timeout * 1000); completionPolicy.setPolicies(policies); builder.chunk(completionPolicy); } else if(timeout != null) { builder.chunk(new TimeoutTerminationPolicy(timeout * 1000)); } else if(getCommitInterval() != null) { builder.chunk(getCommitInterval()); } if(getCompletionPolicy() != null) { builder.chunk(getCompletionPolicy()); } }
@Before public void onSetUp() throws Exception { step = new TaskletStep("stepName"); step.setJobRepository(jobRepository); step.setTransactionManager(transactionManager); // Only process one item: chunkOperations = new RepeatTemplate(); chunkOperations.setCompletionPolicy(new SimpleCompletionPolicy(1)); job = new JobSupport("FOO"); step.setTransactionManager(transactionManager); }
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; }
@Before public void setUp() throws Exception { transactionManager = new ResourcelessTransactionManager(); RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(1)); step = getStep(new String[] { "foo", "bar", "spam" }); step.setStepOperations(template); job = new JobSupport("FOO"); jobInstance = new JobInstance(0L, job.getName()); jobParameters = new JobParameters(); step.setTransactionManager(transactionManager); }
@Before public void init() throws Exception { maxActive = dataSource.getMaxTotal(); maxIdle = dataSource.getMaxIdle(); // Force deadlock with batch waiting for DB pool and vice versa dataSource.setMaxTotal(1); dataSource.setMaxIdle(1); step = new TaskletStep("stepName"); step.setJobRepository(jobRepository); step.setTransactionManager(transactionManager); // Only process one item: chunkOperations = new RepeatTemplate(); chunkOperations.setCompletionPolicy(new SimpleCompletionPolicy(1)); job = new JobSupport("FOO"); TaskExecutorRepeatTemplate repeatTemplate = new TaskExecutorRepeatTemplate(); repeatTemplate.setThrottleLimit(2); repeatTemplate.setTaskExecutor(new SimpleAsyncTaskExecutor()); step.setStepOperations(repeatTemplate); step.setTransactionManager(transactionManager); }