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

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

Introduction

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

Prototype

public void incrementReadCount() 

Source Link

Document

Increment the counter for the number of items read.

Usage

From source file:fr.acxio.tools.agia.tasks.FileCopyTasklet.java

public RepeatStatus execute(StepContribution sContribution, ChunkContext sChunkContext)
        throws ResourceCreationException, IOException, FileCopyException {
    File aOriginFile = origin.getFile();
    if (aOriginFile.exists() && aOriginFile.isFile()) {

        if (!ignoreEmptyFile || (aOriginFile.length() > 0)) {

            if (sContribution != null) {
                sContribution.incrementReadCount();
            }//www  .ja v  a 2  s.c  o  m

            if (destinationFactory != null) {
                Map<String, Object> aDestinationParams = new HashMap<String, Object>();
                aDestinationParams.put(ResourceFactoryConstants.PARAM_SOURCE, origin);
                aDestinationParams.put(ResourceFactoryConstants.PARAM_STEP_EXEC,
                        ((sChunkContext != null) && (sChunkContext.getStepContext() != null))
                                ? sChunkContext.getStepContext().getStepExecution()
                                : null);

                destination = destinationFactory.getResource(aDestinationParams);
            }

            File aDestinationFile = destination.getFile();
            if (aDestinationFile.exists()) {
                if (forceReplace && aDestinationFile.isFile()) {
                    if (!aDestinationFile.delete()) {
                        throw new FileCopyException("Cannot remove: " + destination);
                    }
                } else {
                    throw new FileCopyException("Cannot replace: " + destination);
                }
            }

            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("Copying : {} => {}", aOriginFile.getAbsolutePath(),
                        aDestinationFile.getAbsolutePath());
            }

            FileUtils.copyFile(aOriginFile, aDestinationFile);

            if ((emptyOrigin || deleteOrigin) && !aOriginFile.delete()) {
                throw new FileCopyException("Cannot delete: " + origin);
            }
            if (emptyOrigin && !aOriginFile.createNewFile()) {
                throw new FileCopyException("Cannot create: " + origin);
            }

            if (sContribution != null) {
                sContribution.incrementWriteCount(1);
            }
        } else if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Ignoring empty file : {}", aOriginFile.getAbsolutePath());
        }

    } else {
        throw new FileCopyException("File not found: " + origin);
    }

    return RepeatStatus.FINISHED;
}

From source file:fr.acxio.tools.agia.io.AbstractFileOperations.java

protected List<String> doOperation(Resource aSourceResource, Map<String, Object> aDestinationParams,
        StepContribution sContribution, StepExecution sStepExecution)
        throws IOException, ResourceCreationException, FileOperationException {

    List<String> aDestinationFilesList = new ArrayList<String>();

    File aOriginFile = aSourceResource.getFile();
    if (aOriginFile.exists()) {
        if (sContribution != null) {
            sContribution.incrementReadCount();
        }/*from w  w  w.  j  av a  2 s . c  om*/

        if (operation == Operation.REMOVE) {

            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("Deleting : {}", aOriginFile.getAbsolutePath());
            }

            removeFile(aSourceResource);
        } else {
            Resource aDestination = getDefaultDestination();
            if (destinationFactory != null) {
                aDestinationParams.put(ResourceFactoryConstants.PARAM_SOURCE, aSourceResource);
                aDestinationParams.put(ResourceFactoryConstants.PARAM_STEP_EXEC, sStepExecution);
                aDestination = destinationFactory.getResource(aDestinationParams);
            }
            if ((aDestination != null) && (aDestination.getFile() != null)) {
                File aDestinationFile = aDestination.getFile();
                if (operation == Operation.COPY) {

                    if (LOGGER.isInfoEnabled()) {
                        LOGGER.info("Copying : {} => {}", aOriginFile.getAbsolutePath(),
                                aDestinationFile.getAbsolutePath());
                    }

                    copyFile(aSourceResource, aDestination);
                } else if (operation == Operation.MOVE) {

                    if (LOGGER.isInfoEnabled()) {
                        LOGGER.info("Moving : {} => {}", aOriginFile.getAbsolutePath(),
                                aDestinationFile.getAbsolutePath());
                    }

                    moveFile(aSourceResource, aDestination);
                } else {
                    throw new FileOperationException("Unknown operation");
                }
            } else {
                throw new FileOperationException("No destination specified");
            }
            aDestinationFilesList.add(aDestination.getFile().getCanonicalPath());
        }

        if (sContribution != null) {
            sContribution.incrementWriteCount(1);
        }
    } else {
        throw new FileOperationException("File not found: " + aOriginFile);
    }

    return aDestinationFilesList;
}

From source file:fr.acxio.tools.agia.tasks.ZipFilesTasklet.java

protected void zipResource(Resource sSourceResource, ZipArchiveOutputStream sZipArchiveOutputStream,
        StepContribution sContribution, ChunkContext sChunkContext) throws IOException, ZipFilesException {
    // TODO : use a queue to reduce the callstack overhead
    if (sSourceResource.exists()) {
        File aSourceFile = sSourceResource.getFile();
        String aSourcePath = aSourceFile.getCanonicalPath();

        if (!aSourcePath.startsWith(sourceBaseDirectoryPath)) {
            throw new ZipFilesException(
                    "Source file " + aSourcePath + " does not match base directory " + sourceBaseDirectoryPath);
        }/*from  w  w  w .  j a  v a  2s .c o m*/

        if (sContribution != null) {
            sContribution.incrementReadCount();
        }
        String aZipEntryName = aSourcePath.substring(sourceBaseDirectoryPath.length() + 1);
        sZipArchiveOutputStream.putArchiveEntry(new ZipArchiveEntry(aZipEntryName));
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Zipping {} to {}", sSourceResource.getFile().getCanonicalPath(), aZipEntryName);
        }
        if (aSourceFile.isFile()) {
            InputStream aInputStream = sSourceResource.getInputStream();
            IOUtils.copy(aInputStream, sZipArchiveOutputStream);
            aInputStream.close();
            sZipArchiveOutputStream.closeArchiveEntry();
        } else {
            sZipArchiveOutputStream.closeArchiveEntry();
            for (File aFile : aSourceFile
                    .listFiles((FileFilter) (recursive ? TrueFileFilter.TRUE : FileFileFilter.FILE))) {
                zipResource(new FileSystemResource(aFile), sZipArchiveOutputStream, sContribution,
                        sChunkContext);
            }
        }
        if (sContribution != null) {
            sContribution.incrementWriteCount(1);
        }
    } else if (LOGGER.isInfoEnabled()) {
        LOGGER.info("{} does not exist", sSourceResource.getFilename());
    }
}

From source file:fr.acxio.tools.agia.file.pdf.SplitPDFTasklet.java

@Override
public RepeatStatus execute(StepContribution sContribution, ChunkContext sChunkContext) throws Exception {
    Map<String, Object> aSourceParams = new HashMap<String, Object>();
    aSourceParams.put(ResourceFactoryConstants.PARAM_STEP_EXEC,
            ((sChunkContext != null) && (sChunkContext.getStepContext() != null))
                    ? sChunkContext.getStepContext().getStepExecution()
                    : null);/*from   w w w .jav  a 2  s  .c  o  m*/
    Resource[] aSourceResources = sourceFactory.getResources(aSourceParams);

    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("{} file(s) to split", aSourceResources.length);
    }

    for (Resource aSourceResource : aSourceResources) {
        if (sContribution != null) {
            sContribution.incrementReadCount();
        }
        File aOriginFile = aSourceResource.getFile();
        if (aOriginFile.exists()) {
            int aOutputCount = splitFile(aSourceResource, sChunkContext);
            if (sContribution != null) {
                sContribution.incrementWriteCount(aOutputCount);
            }
        } else {
            throw new SplitPDFException("File not found: " + aOriginFile);
        }
    }

    return RepeatStatus.FINISHED;
}

From source file:fr.acxio.tools.agia.ftp.FTPUploadTasklet.java

@Override
public RepeatStatus execute(StepContribution sContribution, ChunkContext sChunkContext) throws Exception {
    FTPClient aClient = ftpClientFactory.getFtpClient();

    RegexFilenameFilter aFilter = new RegexFilenameFilter();
    aFilter.setRegex(regexFilename);/*  w w  w.j  a va 2 s. c  o m*/

    try {
        File aLocalDir = new File(localBaseDir);

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Listing : {} ({}) for upload to [{}]", localBaseDir, regexFilename,
                    aClient.getRemoteAddress().toString());
        }

        File[] aLocalFiles = aLocalDir.listFiles(aFilter);

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("  {} file(s) found", aLocalFiles.length);
        }

        for (File aLocalFile : aLocalFiles) {

            if (sContribution != null) {
                sContribution.incrementReadCount();
            }

            URI aRemoteFile = new URI(remoteBaseDir).resolve(aLocalFile.getName());
            InputStream aInputStream;
            aInputStream = new FileInputStream(aLocalFile);
            try {

                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info(" Uploading : {} => {}", aLocalFile.getAbsolutePath(),
                            aRemoteFile.toASCIIString());
                }

                aClient.storeFile(aRemoteFile.toASCIIString(), aInputStream);

                if (sContribution != null) {
                    sContribution.incrementWriteCount(1);
                }
            } finally {
                aInputStream.close();
            }
        }
    } finally {
        aClient.logout();
        aClient.disconnect();
    }

    return RepeatStatus.FINISHED;
}

From source file:fr.acxio.tools.agia.ftp.FTPDownloadTasklet.java

@Override
public RepeatStatus execute(StepContribution sContribution, ChunkContext sChunkContext) throws Exception {
    FTPClient aClient = ftpClientFactory.getFtpClient();

    RegexFilenameFilter aFilter = new RegexFilenameFilter();
    aFilter.setRegex(regexFilename);//from   ww  w.j a  v a 2s  .co m
    try {
        URI aRemoteBaseURI = new URI(remoteBaseDir);
        URI aRemoteBasePath = new URI(aRemoteBaseURI.toASCIIString() + SEPARATOR);

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Listing : [{}] {} ({})", aClient.getRemoteAddress().toString(),
                    aRemoteBaseURI.toASCIIString(), regexFilename);
        }

        FTPFile[] aRemoteFiles = aClient.listFiles(aRemoteBaseURI.toASCIIString(), aFilter);

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("  {} file(s) found", aRemoteFiles.length);
        }

        for (FTPFile aRemoteFile : aRemoteFiles) {

            if (sContribution != null) {
                sContribution.incrementReadCount();
            }

            File aLocalFile = new File(localBaseDir, aRemoteFile.getName());
            URI aRemoteTFile = aRemoteBasePath.resolve(aRemoteFile.getName());

            FileOutputStream aOutputStream = new FileOutputStream(aLocalFile);
            try {

                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info(" Downloading : {} => {}", aRemoteTFile.toASCIIString(),
                            aLocalFile.getAbsolutePath());
                }

                aClient.retrieveFile(aRemoteTFile.toASCIIString(), aOutputStream);
                if (removeRemote) {

                    if (LOGGER.isInfoEnabled()) {
                        LOGGER.info(" Deleting : {}", aRemoteTFile.toASCIIString());
                    }

                    aClient.deleteFile(aRemoteTFile.toASCIIString());
                }

                if (sContribution != null) {
                    sContribution.incrementWriteCount(1);
                }

            } finally {
                aOutputStream.close();
            }
        }
    } finally {
        aClient.logout();
        aClient.disconnect();
    }

    return RepeatStatus.FINISHED;
}

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

/**
 * Loops through reading (via {@link #provide(StepContribution, Chunk)} and
 * processing (via {@link #transform(StepContribution, Object)}) until the chunk
 * is complete.  Once the chunk is complete, the results are written (via
 * {@link #persist(StepContribution, Chunk)}.
 *
 * @see ChunkProcessor#process(StepContribution, Chunk)
 * @param contribution a {@link StepContribution}
 * @param chunk a {@link Chunk}/*from w  ww.ja  v a  2  s .c o  m*/
 */
@Override
public void process(final StepContribution contribution, final Chunk<I> chunk) throws Exception {

    final AtomicInteger filterCount = new AtomicInteger(0);
    final Chunk<O> output = new Chunk<O>();

    repeatTemplate.iterate(new RepeatCallback() {

        @Override
        public RepeatStatus doInIteration(RepeatContext context) throws Exception {
            I item = provide(contribution, chunk);

            if (item != null) {
                contribution.incrementReadCount();
            } else {
                return RepeatStatus.FINISHED;
            }

            O processedItem = transform(contribution, item);

            if (processedItem == null) {
                filterCount.incrementAndGet();
            } else {
                output.add(processedItem);
            }

            return RepeatStatus.CONTINUABLE;
        }
    });

    contribution.incrementFilterCount(filterCount.get());
    if (output.size() > 0) {
        persist(contribution, output);
    }
}

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

@Override
public Chunk<I> provide(final StepContribution contribution) throws Exception {

    final Chunk<I> inputs = new Chunk<I>();
    repeatOperations.iterate(new RepeatCallback() {

        @Override/*  www.j a va 2s .  c om*/
        public RepeatStatus doInIteration(final RepeatContext context) throws Exception {
            I item = null;
            try {
                item = read(contribution, inputs);
            } catch (SkipOverflowException e) {
                // read() tells us about an excess of skips by throwing an
                // exception
                return RepeatStatus.FINISHED;
            }
            if (item == null) {
                inputs.setEnd();
                return RepeatStatus.FINISHED;
            }
            inputs.add(item);
            contribution.incrementReadCount();
            return RepeatStatus.CONTINUABLE;
        }

    });

    return inputs;

}