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

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

Introduction

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

Prototype

public boolean isEmpty() 

Source Link

Usage

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

@Override
protected boolean isComplete(Chunk<I> inputs) {

    /*// ww w .j  av a 2 s  .  c o  m
     * Need to remember the write skips across transactions, otherwise they
     * keep coming back. Since we register skips with the inputs they will
     * not be processed again but the output skips need to be saved for
     * registration later with the listeners. The inputs are going to be the
     * same for all transactions processing the same chunk, but the outputs
     * are not, so we stash them in user data on the inputs.
     */

    @SuppressWarnings("unchecked")
    UserData<O> data = (UserData<O>) inputs.getUserData();
    Chunk<O> previous = data.getOutputs();

    return inputs.isEmpty() && previous.getSkips().isEmpty();

}

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;/*from  w ww  . jav a  2 s  .com*/

    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 scan(final StepContribution contribution, final Chunk<I> inputs, final Chunk<O> outputs,
        ChunkMonitor chunkMonitor, boolean recovery) throws Exception {

    @SuppressWarnings("unchecked")
    final UserData<O> data = (UserData<O>) inputs.getUserData();

    if (logger.isDebugEnabled()) {
        if (recovery) {
            logger.debug("Scanning for failed item on recovery from write: " + inputs);
        } else {/*from www  .  ja v  a 2 s .  com*/
            logger.debug("Scanning for failed item on write: " + inputs);
        }
    }
    if (outputs.isEmpty() || inputs.isEmpty()) {
        data.scanning(false);
        inputs.setBusy(false);
        chunkMonitor.resetOffset();
        return;
    }

    Chunk<I>.ChunkIterator inputIterator = inputs.iterator();
    Chunk<O>.ChunkIterator outputIterator = outputs.iterator();

    List<O> items = Collections.singletonList(outputIterator.next());
    inputIterator.next();
    try {
        writeItems(items);
        // If successful we are going to return and allow
        // the driver to commit...
        doAfterWrite(items);
        contribution.incrementWriteCount(1);
        inputIterator.remove();
        outputIterator.remove();
    } catch (Exception e) {
        try {
            doOnWriteError(e, items);
        } finally {
            Throwable cause = e;
            if (e instanceof StepListenerFailedException) {
                cause = e.getCause();
            }

            if (!shouldSkip(itemWriteSkipPolicy, cause, -1) && !rollbackClassifier.classify(cause)) {
                inputIterator.remove();
                outputIterator.remove();
            } else {
                checkSkipPolicy(inputIterator, outputIterator, cause, contribution, recovery);
            }
            if (rollbackClassifier.classify(cause)) {
                throw (Exception) cause;
            }
        }
    }
    chunkMonitor.incrementOffset();
    if (outputs.isEmpty()) {
        data.scanning(false);
        inputs.setBusy(false);
        chunkMonitor.resetOffset();
    }
}