Example usage for org.springframework.batch.repeat.policy SimpleCompletionPolicy SimpleCompletionPolicy

List of usage examples for org.springframework.batch.repeat.policy SimpleCompletionPolicy SimpleCompletionPolicy

Introduction

In this page you can find the example usage for org.springframework.batch.repeat.policy SimpleCompletionPolicy SimpleCompletionPolicy.

Prototype

public SimpleCompletionPolicy(int chunkSize) 

Source Link

Usage

From source file:org.springframework.batch.core.step.item.SimpleStepFactoryBean.java

/**
 * @return a {@link CompletionPolicy} consistent with the commit interval
 * and injected policy (if present)./*w  w  w .  j av a  2 s .c  o  m*/
 */
private CompletionPolicy getChunkCompletionPolicy() {
    Assert.state(!(chunkCompletionPolicy != null && commitInterval != 0),
            "You must specify either a chunkCompletionPolicy or a commitInterval but not both.");
    Assert.state(commitInterval >= 0, "The commitInterval must be positive or zero (for default value).");

    if (chunkCompletionPolicy != null) {
        return chunkCompletionPolicy;
    }
    if (commitInterval == 0) {
        logger.info("Setting commit interval to default value (" + DEFAULT_COMMIT_INTERVAL + ")");
        commitInterval = DEFAULT_COMMIT_INTERVAL;
    }
    return new SimpleCompletionPolicy(commitInterval);
}

From source file:org.springframework.batch.core.step.tasklet.AsyncChunkOrientedStepIntegrationTests.java

@Before
public void init() throws Exception {

    maxActive = dataSource.getMaxActive();
    maxIdle = dataSource.getMaxIdle();/*from w  ww.j ava 2  s  . c o  m*/

    // Force deadlock with batch waiting for DB pool and vice versa
    dataSource.setMaxActive(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);
}

From source file:org.springframework.batch.core.step.tasklet.AsyncTaskletStepTests.java

private void setUp() throws Exception {

    step = new TaskletStep("stepName");

    ResourcelessTransactionManager transactionManager = new ResourcelessTransactionManager();
    step.setTransactionManager(transactionManager);

    RepeatTemplate chunkTemplate = new RepeatTemplate();
    chunkTemplate.setCompletionPolicy(new SimpleCompletionPolicy(2));
    step.setTasklet(new TestingChunkOrientedTasklet<String>(new ListItemReader<String>(items), itemProcessor,
            itemWriter, chunkTemplate));

    jobRepository = new JobRepositorySupport();
    step.setJobRepository(jobRepository);

    TaskExecutorRepeatTemplate template = new TaskExecutorRepeatTemplate();
    template.setThrottleLimit(throttleLimit);
    SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
    taskExecutor.setConcurrencyLimit(concurrencyLimit);
    template.setTaskExecutor(taskExecutor);
    step.setStepOperations(template);//from   w  w w . ja v  a  2s. co  m

    step.registerStream(new ItemStreamSupport() {
        private int count = 0;

        @Override
        public void update(ExecutionContext executionContext) {
            super.update(executionContext);
            executionContext.putInt("counter", count++);
        }
    });

}

From source file:org.springframework.batch.repeat.support.TaskExecutorRepeatTemplateBulkAsynchronousTests.java

@Test
public void testThrottleLimitWithEarlyCompletion() throws Exception {

    early = 2;//from w  w  w  .  j a v  a  2s .c o m
    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);

}

From source file:org.springframework.batch.repeat.support.TaskExecutorRepeatTemplateBulkAsynchronousTests.java

@Test
public void testErrorThrownByCallback() throws Exception {

    callback = new RepeatCallback() {

        private volatile AtomicInteger count = new AtomicInteger(0);

        @Override//from   ww  w. j a v  a  2  s.  c  om
        public RepeatStatus doInIteration(RepeatContext context) throws Exception {
            int position = count.incrementAndGet();

            if (position == 4) {
                throw new OutOfMemoryError("Planned");
            } else {
                return RepeatStatus.CONTINUABLE;
            }
        }
    };

    template.setCompletionPolicy(new SimpleCompletionPolicy(10));

    try {
        template.iterate(callback);
        fail("Expected planned exception");
    } catch (OutOfMemoryError oome) {
        assertEquals("Planned", oome.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        fail("Wrong exception was thrown: " + e);
    }
}