Example usage for org.springframework.batch.core.step.tasklet TaskletStep setTransactionAttribute

List of usage examples for org.springframework.batch.core.step.tasklet TaskletStep setTransactionAttribute

Introduction

In this page you can find the example usage for org.springframework.batch.core.step.tasklet TaskletStep setTransactionAttribute.

Prototype

public void setTransactionAttribute(TransactionAttribute transactionAttribute) 

Source Link

Document

Public setter for the TransactionAttribute .

Usage

From source file:org.springframework.batch.core.configuration.xml.StepParserStepFactoryBean.java

@SuppressWarnings("serial")
private void configureTaskletStep(TaskletStep ts) {
    configureAbstractStep(ts);//ww  w .  j  a v a  2s . c o m
    if (listeners != null) {
        List<ChunkListener> newListeners = new ArrayList<ChunkListener>();
        for (StepListener listener : listeners) {
            if (listener instanceof ChunkListener) {
                newListeners.add((ChunkListener) listener);
            }
        }
        ts.setChunkListeners(newListeners.toArray(new ChunkListener[0]));
    }
    if (tasklet != null) {
        ts.setTasklet(tasklet);
    }
    if (taskExecutor != null) {
        TaskExecutorRepeatTemplate repeatTemplate = new TaskExecutorRepeatTemplate();
        repeatTemplate.setTaskExecutor(taskExecutor);
        if (throttleLimit != null) {
            repeatTemplate.setThrottleLimit(throttleLimit);
        }
        ts.setStepOperations(repeatTemplate);
    }
    if (transactionManager != null) {
        ts.setTransactionManager(transactionManager);
    }
    if (transactionTimeout != null || propagation != null || isolation != null
            || noRollbackExceptionClasses != null) {
        DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
        if (propagation != null) {
            attribute.setPropagationBehavior(propagation.value());
        }
        if (isolation != null) {
            attribute.setIsolationLevel(isolation.value());
        }
        if (transactionTimeout != null) {
            attribute.setTimeout(transactionTimeout);
        }
        Collection<Class<? extends Throwable>> exceptions = noRollbackExceptionClasses == null
                ? new HashSet<Class<? extends Throwable>>()
                : noRollbackExceptionClasses;
        final BinaryExceptionClassifier classifier = new BinaryExceptionClassifier(exceptions, false);
        ts.setTransactionAttribute(new DefaultTransactionAttribute(attribute) {
            @Override
            public boolean rollbackOn(Throwable ex) {
                return classifier.classify(ex);
            }
        });
    }
}

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

/**
 * @param step// w ww.  j  ava2  s. co  m
 * 
 */
protected void applyConfiguration(TaskletStep step) {

    Assert.state(getItemReader() != null, "ItemReader must be provided");
    Assert.state(getItemWriter() != null || getItemProcessor() != null,
            "ItemWriter or ItemProcessor must be provided");
    Assert.state(transactionManager != null, "TransactionManager must be provided");

    step.setTransactionManager(transactionManager);
    step.setTransactionAttribute(getTransactionAttribute());
    step.setJobRepository(jobRepository);
    step.setStartLimit(startLimit);
    step.setAllowStartIfComplete(allowStartIfComplete);

    registerStreams(step, streams);

    if (chunkOperations == null) {
        RepeatTemplate repeatTemplate = new RepeatTemplate();
        repeatTemplate.setCompletionPolicy(getChunkCompletionPolicy());
        chunkOperations = repeatTemplate;
    }

    if (stepOperations == null) {

        stepOperations = new RepeatTemplate();

        if (taskExecutor != null) {
            TaskExecutorRepeatTemplate repeatTemplate = new TaskExecutorRepeatTemplate();
            repeatTemplate.setTaskExecutor(taskExecutor);
            repeatTemplate.setThrottleLimit(throttleLimit);
            stepOperations = repeatTemplate;
        }

        ((RepeatTemplate) stepOperations).setExceptionHandler(exceptionHandler);

    }

    step.setStepOperations(stepOperations);

    SimpleChunkProvider<T> chunkProvider = configureChunkProvider();

    SimpleChunkProcessor<T, S> chunkProcessor = configureChunkProcessor();

    registerItemListeners(chunkProvider, chunkProcessor);
    registerStepListeners(step, chunkOperations);
    registerStreams(step, itemReader, itemProcessor, itemWriter);

    ChunkOrientedTasklet<T> tasklet = new ChunkOrientedTasklet<T>(chunkProvider, chunkProcessor);
    tasklet.setBuffering(!isReaderTransactionalQueue());

    step.setTasklet(tasklet);

}