Example usage for org.springframework.batch.support.transaction TransactionAwareBufferedWriter TransactionAwareBufferedWriter

List of usage examples for org.springframework.batch.support.transaction TransactionAwareBufferedWriter TransactionAwareBufferedWriter

Introduction

In this page you can find the example usage for org.springframework.batch.support.transaction TransactionAwareBufferedWriter TransactionAwareBufferedWriter.

Prototype

public TransactionAwareBufferedWriter(FileChannel channel, Runnable closeCallback) 

Source Link

Document

Create a new instance with the underlying file channel provided, and a callback to execute on close.

Usage

From source file:org.emonocot.job.io.StaxEventItemWriter.java

/**
 * Helper method for opening output source at given file position.
 * @param position Set the position/*from  w  w  w . j a  va2s. c  o m*/
 * @param restarted Is this execution being restarted
 */
private void open(final long position, final boolean restarted) {

    File file;
    FileOutputStream os = null;

    try {
        file = resource.getFile();
        FileUtils.setUpOutputFile(file, restarted, overwriteOutput);
        Assert.state(resource.exists(), "Output resource must exist");
        os = new FileOutputStream(file, true);
        channel = os.getChannel();
        setPosition(position);
    } catch (IOException ioe) {
        throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]",
                ioe);
    }

    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();

    if (outputFactory.isPropertySupported("com.ctc.wstx.automaticEndElements")) {
        // If the current XMLOutputFactory implementation is supplied by
        // Woodstox >= 3.2.9 we want to disable its
        // automatic end element feature (see:
        // http://jira.codehaus.org/browse/WSTX-165) per
        // http://jira.springframework.org/browse/BATCH-761.
        outputFactory.setProperty("com.ctc.wstx.automaticEndElements", Boolean.FALSE);
    }

    try {
        if (transactional) {
            bufferedWriter = new TransactionAwareBufferedWriter(new OutputStreamWriter(os, encoding),
                    new Runnable() {
                        public void run() {
                            closeStream();
                        }
                    });
        } else {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, encoding));
        }
        delegateEventWriter = outputFactory.createXMLEventWriter(bufferedWriter);
        eventWriter = new NoStartEndDocumentStreamWriter(delegateEventWriter);
        if (!restarted) {
            startDocument(delegateEventWriter);
        }
    } catch (XMLStreamException xse) {
        throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]",
                xse);
    } catch (UnsupportedEncodingException e) {
        throw new DataAccessResourceFailureException(
                "Unable to write to file resource: [" + resource + "] with encoding=[" + encoding + "]", e);
    }

}

From source file:org.springframework.batch.item.xml.StaxEventItemWriter.java

/**
 * Helper method for opening output source at given file position
 */// w  ww .  j a va  2 s . c o  m
private void open(long position) {

    File file;
    FileOutputStream os = null;
    FileChannel fileChannel = null;

    try {
        file = resource.getFile();
        FileUtils.setUpOutputFile(file, restarted, false, overwriteOutput);
        Assert.state(resource.exists(), "Output resource must exist");
        os = new FileOutputStream(file, true);
        fileChannel = os.getChannel();
        channel = os.getChannel();
        setPosition(position);
    } catch (IOException ioe) {
        throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]",
                ioe);
    }

    XMLOutputFactory outputFactory = createXmlOutputFactory();

    if (outputFactory.isPropertySupported("com.ctc.wstx.automaticEndElements")) {
        // If the current XMLOutputFactory implementation is supplied by
        // Woodstox >= 3.2.9 we want to disable its
        // automatic end element feature (see:
        // http://jira.codehaus.org/browse/WSTX-165) per
        // http://jira.spring.io/browse/BATCH-761).
        outputFactory.setProperty("com.ctc.wstx.automaticEndElements", Boolean.FALSE);
    }
    if (outputFactory.isPropertySupported("com.ctc.wstx.outputValidateStructure")) {
        // On restart we don't write the root element so we have to disable
        // structural validation (see:
        // http://jira.spring.io/browse/BATCH-1681).
        outputFactory.setProperty("com.ctc.wstx.outputValidateStructure", Boolean.FALSE);
    }

    try {
        final FileChannel channel = fileChannel;
        if (transactional) {
            TransactionAwareBufferedWriter writer = new TransactionAwareBufferedWriter(channel, new Runnable() {
                @Override
                public void run() {
                    closeStream();
                }
            });

            writer.setEncoding(encoding);
            writer.setForceSync(forceSync);
            bufferedWriter = writer;
        } else {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, encoding));
        }
        delegateEventWriter = createXmlEventWriter(outputFactory, bufferedWriter);
        eventWriter = new NoStartEndDocumentStreamWriter(delegateEventWriter);
        initNamespaceContext(delegateEventWriter);
        if (!restarted) {
            startDocument(delegateEventWriter);
            if (forceSync) {
                channel.force(false);
            }
        }
    } catch (XMLStreamException xse) {
        throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]",
                xse);
    } catch (UnsupportedEncodingException e) {
        throw new DataAccessResourceFailureException(
                "Unable to write to file resource: [" + resource + "] with encoding=[" + encoding + "]", e);
    } catch (IOException e) {
        throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]", e);
    }
}