Search in sources :

Example 1 with TransactionAwareBufferedWriter

use of org.springframework.batch.support.transaction.TransactionAwareBufferedWriter in project spring-batch by spring-projects.

the class StaxEventItemWriter method open.

/**
 * Helper method for opening output source at given file position
 */
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:
        // https://jira.codehaus.org/browse/WSTX-165) per
        // https://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:
        // https://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);
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) FileChannel(java.nio.channels.FileChannel) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) TransactionAwareBufferedWriter(org.springframework.batch.support.transaction.TransactionAwareBufferedWriter) NoStartEndDocumentStreamWriter(org.springframework.batch.item.xml.stax.NoStartEndDocumentStreamWriter) XMLStreamException(javax.xml.stream.XMLStreamException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) TransactionAwareBufferedWriter(org.springframework.batch.support.transaction.TransactionAwareBufferedWriter)

Example 2 with TransactionAwareBufferedWriter

use of org.springframework.batch.support.transaction.TransactionAwareBufferedWriter in project spring-batch by spring-projects.

the class StaxEventItemWriterBuilderTests method testTransactional.

@Test
public void testTransactional() {
    StaxEventItemWriter<Foo> staxEventItemWriter = new StaxEventItemWriterBuilder<Foo>().name("fooWriter").resource(this.resource).marshaller(this.marshaller).transactional(true).forceSync(true).build();
    ExecutionContext executionContext = new ExecutionContext();
    staxEventItemWriter.open(executionContext);
    Object writer = ReflectionTestUtils.getField(staxEventItemWriter, "bufferedWriter");
    assertTrue(writer instanceof TransactionAwareBufferedWriter);
    assertTrue((Boolean) ReflectionTestUtils.getField(writer, "forceSync"));
}
Also used : ExecutionContext(org.springframework.batch.item.ExecutionContext) TransactionAwareBufferedWriter(org.springframework.batch.support.transaction.TransactionAwareBufferedWriter) Test(org.junit.Test)

Example 3 with TransactionAwareBufferedWriter

use of org.springframework.batch.support.transaction.TransactionAwareBufferedWriter in project spring-batch by spring-projects.

the class StaxEventItemWriter method getPosition.

/*
	 * Get the actual position in file channel. This method flushes any buffered
	 * data before position is read.
	 * 
	 * @return byte offset in file channel
	 */
private long getPosition() {
    long position;
    try {
        eventWriter.flush();
        position = channel.position();
        if (bufferedWriter instanceof TransactionAwareBufferedWriter) {
            position += ((TransactionAwareBufferedWriter) bufferedWriter).getBufferSize();
        }
    } catch (Exception e) {
        throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]", e);
    }
    return position;
}
Also used : DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) TransactionAwareBufferedWriter(org.springframework.batch.support.transaction.TransactionAwareBufferedWriter) WriterNotOpenException(org.springframework.batch.item.WriterNotOpenException) ItemStreamException(org.springframework.batch.item.ItemStreamException) WriteFailedException(org.springframework.batch.item.WriteFailedException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) XmlMappingException(org.springframework.oxm.XmlMappingException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

TransactionAwareBufferedWriter (org.springframework.batch.support.transaction.TransactionAwareBufferedWriter)3 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 FileChannel (java.nio.channels.FileChannel)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 Test (org.junit.Test)1 ExecutionContext (org.springframework.batch.item.ExecutionContext)1 ItemStreamException (org.springframework.batch.item.ItemStreamException)1 WriteFailedException (org.springframework.batch.item.WriteFailedException)1 WriterNotOpenException (org.springframework.batch.item.WriterNotOpenException)1 NoStartEndDocumentStreamWriter (org.springframework.batch.item.xml.stax.NoStartEndDocumentStreamWriter)1 XmlMappingException (org.springframework.oxm.XmlMappingException)1