Example usage for org.springframework.batch.core.step.item Chunk skip

List of usage examples for org.springframework.batch.core.step.item Chunk skip

Introduction

In this page you can find the example usage for org.springframework.batch.core.step.item Chunk skip.

Prototype

public void skip(Exception e) 

Source Link

Document

Register an anonymous skip.

Usage

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

/**
 * Adds retry and skip logic to the reading phase of the chunk loop.
 *
 * @param contribution a {@link StepContribution}
 * @param chunk a {@link Chunk}//from   w w w.  ja  v a  2 s.  c  om
 * @return I an item
 * @throws Exception
 */
@Override
protected I provide(final StepContribution contribution, final Chunk<I> chunk) throws Exception {
    RetryCallback<I, Exception> retryCallback = new RetryCallback<I, Exception>() {

        @Override
        public I doWithRetry(RetryContext arg0) throws Exception {
            while (true) {
                try {
                    return doProvide(contribution, chunk);
                } catch (Exception e) {
                    if (shouldSkip(skipPolicy, e, contribution.getStepSkipCount())) {

                        // increment skip count and try again
                        contribution.incrementReadSkipCount();
                        chunk.skip(e);

                        getListener().onSkipInRead(e);

                        logger.debug("Skipping failed input", e);
                    } else {
                        getListener().onRetryReadException(e);

                        if (rollbackClassifier.classify(e)) {
                            throw e;
                        } else {
                            throw e;
                        }
                    }
                }
            }
        }
    };

    RecoveryCallback<I> recoveryCallback = new RecoveryCallback<I>() {

        @Override
        public I recover(RetryContext context) throws Exception {
            Throwable e = context.getLastThrowable();
            if (shouldSkip(skipPolicy, e, contribution.getStepSkipCount())) {
                contribution.incrementReadSkipCount();
                logger.debug("Skipping after failed process", e);
                return null;
            } else {
                if (rollbackClassifier.classify(e)) {
                    // Default is to rollback unless the classifier
                    // allows us to continue
                    throw new RetryException("Non-skippable exception in recoverer while reading", e);
                }

                throw new BatchRuntimeException(e);
            }
        }

    };

    return batchRetryTemplate.execute(retryCallback, recoveryCallback);
}