Example usage for org.springframework.integration.util WhileLockedProcessor WhileLockedProcessor

List of usage examples for org.springframework.integration.util WhileLockedProcessor WhileLockedProcessor

Introduction

In this page you can find the example usage for org.springframework.integration.util WhileLockedProcessor WhileLockedProcessor.

Prototype

public WhileLockedProcessor(LockRegistry lockRegistry, Object key) 

Source Link

Usage

From source file:org.springframework.integration.file.FileWritingMessageHandler.java

private File handleFileMessage(final File sourceFile, File tempFile, final File resultFile) throws IOException {
    if (FileExistsMode.APPEND.equals(this.fileExistsMode)) {
        File fileToWriteTo = this.determineFileToWrite(resultFile, tempFile);
        final FileOutputStream fos = new FileOutputStream(fileToWriteTo, true);
        final FileInputStream fis = new FileInputStream(sourceFile);
        WhileLockedProcessor whileLockedProcessor = new WhileLockedProcessor(this.lockRegistry,
                fileToWriteTo.getAbsolutePath()) {
            @Override/* w w w  .java2s .  c o m*/
            protected void whileLocked() throws IOException {
                FileCopyUtils.copy(fis, fos);
            }
        };
        whileLockedProcessor.doWhileLocked();
        this.cleanUpAfterCopy(fileToWriteTo, resultFile, sourceFile);
        return resultFile;
    } else {
        if (this.deleteSourceFiles) {
            if (sourceFile.renameTo(resultFile)) {
                return resultFile;
            }
            if (logger.isInfoEnabled()) {
                logger.info(String.format("Failed to move file '%s'. Using copy and delete fallback.",
                        sourceFile.getAbsolutePath()));
            }
        }
        FileCopyUtils.copy(sourceFile, tempFile);
        this.cleanUpAfterCopy(tempFile, resultFile, sourceFile);
        return resultFile;
    }
}

From source file:org.springframework.integration.file.FileWritingMessageHandler.java

private File handleByteArrayMessage(final byte[] bytes, File originalFile, File tempFile, final File resultFile)
        throws IOException {
    File fileToWriteTo = this.determineFileToWrite(resultFile, tempFile);

    final boolean append = FileExistsMode.APPEND.equals(this.fileExistsMode);

    final FileOutputStream fos = new FileOutputStream(fileToWriteTo, append);
    WhileLockedProcessor whileLockedProcessor = new WhileLockedProcessor(this.lockRegistry,
            fileToWriteTo.getAbsolutePath()) {
        @Override//from  ww w.  j av  a 2s. c  o  m
        protected void whileLocked() throws IOException {
            FileCopyUtils.copy(bytes, fos);
        }

    };
    whileLockedProcessor.doWhileLocked();
    this.cleanUpAfterCopy(fileToWriteTo, resultFile, originalFile);
    return resultFile;
}

From source file:org.springframework.integration.file.FileWritingMessageHandler.java

private File handleStringMessage(final String content, File originalFile, File tempFile, final File resultFile)
        throws IOException {
    File fileToWriteTo = this.determineFileToWrite(resultFile, tempFile);

    final boolean append = FileExistsMode.APPEND.equals(this.fileExistsMode);

    final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(fileToWriteTo, append),
            this.charset);
    WhileLockedProcessor whileLockedProcessor = new WhileLockedProcessor(this.lockRegistry,
            fileToWriteTo.getAbsolutePath()) {
        @Override/*  w  ww . j  a  va  2 s .c o  m*/
        protected void whileLocked() throws IOException {
            FileCopyUtils.copy(content, writer);
        }

    };
    whileLockedProcessor.doWhileLocked();

    this.cleanUpAfterCopy(fileToWriteTo, resultFile, originalFile);
    return resultFile;
}