Example usage for org.springframework.batch.core StepContribution getStepSkipCount

List of usage examples for org.springframework.batch.core StepContribution getStepSkipCount

Introduction

In this page you can find the example usage for org.springframework.batch.core StepContribution getStepSkipCount.

Prototype

public int getStepSkipCount() 

Source Link

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   www.  ja va2s.co  m
 * @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);
}

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

/**
 * Adds retry and skip logic to the process phase of the chunk loop.
 *
 * @param contribution a {@link StepContribution}
 * @param item an item to be processed/*from ww  w. jav  a2 s  . c  o  m*/
 * @return O an item that has been processed if a processor is available
 * @throws Exception
 */
@Override
@SuppressWarnings("unchecked")
protected O transform(final StepContribution contribution, final I item) throws Exception {
    if (!hasProcessor) {
        return (O) item;
    }

    RetryCallback<O, Exception> retryCallback = new RetryCallback<O, Exception>() {

        @Override
        public O doWithRetry(RetryContext context) throws Exception {
            try {
                return doTransform(item);
            } catch (Exception e) {
                if (shouldSkip(skipPolicy, e, contribution.getStepSkipCount())) {
                    // If we are not re-throwing then we should check if
                    // this is skippable
                    contribution.incrementProcessSkipCount();
                    logger.debug("Skipping after failed process with no rollback", e);
                    // If not re-throwing then the listener will not be
                    // called in next chunk.
                    getListener().onSkipInProcess(item, e);
                } else {
                    getListener().onRetryProcessException(item, e);

                    if (rollbackClassifier.classify(e)) {
                        // Default is to rollback unless the classifier
                        // allows us to continue
                        throw e;
                    } else {
                        throw e;
                    }
                }
            }
            return null;
        }

    };

    RecoveryCallback<O> recoveryCallback = new RecoveryCallback<O>() {
        @Override
        public O recover(RetryContext context) throws Exception {
            Throwable e = context.getLastThrowable();
            if (shouldSkip(skipPolicy, e, contribution.getStepSkipCount())) {
                contribution.incrementProcessSkipCount();
                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 processing", e);
                }

                throw new BatchRuntimeException(e);
            }
        }
    };

    return batchRetryTemplate.execute(retryCallback, recoveryCallback);
}

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

/**
 * Adds retry and skip logic to the write phase of the chunk loop.
 *
 * @param contribution a {@link StepContribution}
 * @param chunk a {@link Chunk}/*from  w  w w  .j  a  v a  2  s  .c o  m*/
 * @throws Exception
 */
@Override
protected void persist(final StepContribution contribution, final Chunk<O> chunk) throws Exception {

    RetryCallback<Object, Exception> retryCallback = new RetryCallback<Object, Exception>() {
        @Override
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public Object doWithRetry(RetryContext context) throws Exception {

            chunkMonitor.setChunkSize(chunk.size());
            try {
                doPersist(contribution, chunk);
            } catch (Exception e) {
                if (shouldSkip(skipPolicy, e, contribution.getStepSkipCount())) {
                    // Per section 9.2.7 of JSR-352, the SkipListener receives all the items within the chunk                    
                    ((MulticasterBatchListener) getListener()).onSkipInWrite(chunk.getItems(), e);
                } else {
                    getListener().onRetryWriteException((List<Object>) chunk.getItems(), e);

                    if (rollbackClassifier.classify(e)) {
                        throw e;
                    }
                }
                /*
                 * If the exception is marked as no-rollback, we need to
                 * override that, otherwise there's no way to write the
                 * rest of the chunk or to honour the skip listener
                 * contract.
                 */
                throw new ForceRollbackForWriteSkipException(
                        "Force rollback on skippable exception so that skipped item can be located.", e);
            }
            contribution.incrementWriteCount(chunk.size());
            return null;

        }
    };

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

        @Override
        public O recover(RetryContext context) throws Exception {
            Throwable e = context.getLastThrowable();
            if (shouldSkip(skipPolicy, e, contribution.getStepSkipCount())) {
                contribution.incrementWriteSkipCount();
                logger.debug("Skipping after failed write", 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 write", e);
                }
                return null;
            }
        }

    };

    batchRetryTemplate.execute(retryCallback, recoveryCallback);
}

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

@Override
protected Chunk<O> transform(final StepContribution contribution, Chunk<I> inputs) throws Exception {

    Chunk<O> outputs = new Chunk<O>();
    @SuppressWarnings("unchecked")
    final UserData<O> data = (UserData<O>) inputs.getUserData();
    final Chunk<O> cache = data.getOutputs();
    final Iterator<O> cacheIterator = cache.isEmpty() ? null : new ArrayList<O>(cache.getItems()).iterator();
    final AtomicInteger count = new AtomicInteger(0);

    // final int scanLimit = processorTransactional && data.scanning() ? 1 :
    // 0;//w w  w . j a v  a  2  s .  c om

    for (final Chunk<I>.ChunkIterator iterator = inputs.iterator(); iterator.hasNext();) {

        final I item = iterator.next();

        RetryCallback<O, Exception> retryCallback = new RetryCallback<O, Exception>() {

            @Override
            public O doWithRetry(RetryContext context) throws Exception {
                O output = null;
                try {
                    count.incrementAndGet();
                    O cached = (cacheIterator != null && cacheIterator.hasNext()) ? cacheIterator.next() : null;
                    if (cached != null && !processorTransactional) {
                        output = cached;
                    } else {
                        output = doProcess(item);
                        if (output == null) {
                            data.incrementFilterCount();
                        } else if (!processorTransactional && !data.scanning()) {
                            cache.add(output);
                        }
                    }
                } catch (Exception e) {
                    if (rollbackClassifier.classify(e)) {
                        // Default is to rollback unless the classifier
                        // allows us to continue
                        throw e;
                    } else if (shouldSkip(itemProcessSkipPolicy, e, contribution.getStepSkipCount())) {
                        // If we are not re-throwing then we should check if
                        // this is skippable
                        contribution.incrementProcessSkipCount();
                        logger.debug("Skipping after failed process with no rollback", e);
                        // If not re-throwing then the listener will not be
                        // called in next chunk.
                        callProcessSkipListener(item, e);
                    } else {
                        // If it's not skippable that's an error in
                        // configuration - it doesn't make sense to not roll
                        // back if we are also not allowed to skip
                        throw new NonSkippableProcessException(
                                "Non-skippable exception in processor.  Make sure any exceptions that do not cause a rollback are skippable.",
                                e);
                    }
                }
                if (output == null) {
                    // No need to re-process filtered items
                    iterator.remove();
                }
                return output;
            }

        };

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

            @Override
            public O recover(RetryContext context) throws Exception {
                Throwable e = context.getLastThrowable();
                if (shouldSkip(itemProcessSkipPolicy, e, contribution.getStepSkipCount())) {
                    iterator.remove(e);
                    contribution.incrementProcessSkipCount();
                    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 processing", e);
                    }
                    iterator.remove(e);
                    return null;
                }
            }

        };

        O output = batchRetryTemplate.execute(retryCallback, recoveryCallback,
                new DefaultRetryState(getInputKey(item), rollbackClassifier));
        if (output != null) {
            outputs.add(output);
        }

        /*
         * We only want to process the first item if there is a scan for a
         * failed item.
         */
        if (data.scanning()) {
            while (cacheIterator != null && cacheIterator.hasNext()) {
                outputs.add(cacheIterator.next());
            }
            // Only process the first item if scanning
            break;
        }
    }

    return outputs;

}

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

private void checkSkipPolicy(Chunk<I>.ChunkIterator inputIterator, Chunk<O>.ChunkIterator outputIterator,
        Throwable e, StepContribution contribution, boolean recovery) throws Exception {
    logger.debug("Checking skip policy after failed write");
    if (shouldSkip(itemWriteSkipPolicy, e, contribution.getStepSkipCount())) {
        contribution.incrementWriteSkipCount();
        inputIterator.remove();// w w  w .  jav a 2 s  .  co m
        outputIterator.remove(e);
        logger.debug("Skipping after failed write", e);
    } else {
        if (recovery) {
            // Only if already recovering should we check skip policy
            throw new RetryException("Non-skippable exception in recoverer", e);
        } else {
            if (e instanceof Exception) {
                throw (Exception) e;
            } else if (e instanceof Error) {
                throw (Error) e;
            } else {
                throw new RetryException("Non-skippable throwable in recoverer", e);
            }
        }
    }
}