Example usage for org.springframework.batch.item WriterNotOpenException WriterNotOpenException

List of usage examples for org.springframework.batch.item WriterNotOpenException WriterNotOpenException

Introduction

In this page you can find the example usage for org.springframework.batch.item WriterNotOpenException WriterNotOpenException.

Prototype

public WriterNotOpenException(String message) 

Source Link

Document

Create a new WriterNotOpenException based on a message.

Usage

From source file:org.beanio.spring.BeanIOFlatFileItemWriter.java

public void write(List<? extends T> items) throws Exception {
    if (stream == null) {
        throw new WriterNotOpenException("Writer must be open before it can be written to");
    }//from   ww  w  .  java  2 s.c  o m
    stream.write(items);
}

From source file:egovframework.rte.bat.core.item.file.EgovPartitionFlatFileItemWriter.java

/**
 * Write  //w ww  .  java  2  s. c o m
 * ? state ? ?  ? Write  
 *  ?  ? synchronized  ? 
 * 
 * @param items output Stream ?  itmes 
 */
public synchronized void write(List<? extends T> items) throws Exception {

    if (!getOutputState().isInitialized()) {
        throw new WriterNotOpenException("Writer must be open before it can be written to");
    }

    if (logger.isDebugEnabled()) {
        logger.info("Writing to flat file with " + items.size() + " items.");
    }

    OutputState state = getOutputState();

    StringBuilder lines = new StringBuilder();
    int lineCount = 0;

    for (T item : items) {
        lines.append(lineAggregator.aggregate(item) + lineSeparator);
        lineCount++;
    }

    try {

        state.write(lines.toString());

    } catch (IOException e) {
        throw new WriteFailedException("Could not write data.  The file may be corrupt.", e);
    }
    state.linesWritten += lineCount;
}

From source file:me.andpay.ti.spring.batch.FlatFileItemWriter.java

/**
 * Writes out a string followed by a "new line", where the format of the new
 * line separator is determined by the underlying operating system. If the
 * input is not a String and a converter is available the converter will be
 * applied and then this method recursively called with the result. If the
 * input is an array or collection each value will be written to a separate
 * line (recursively calling this method for each value). If no converter is
 * supplied the input object's toString method will be used.<br/>
 * //from  w  w w. j  av  a  2  s . com
 * @param items list of items to be written to output stream
 * @throws Exception if the transformer or file output fail,
 * WriterNotOpenException if the writer has not been initialized.
 */
public void write(List<? extends T> items) throws Exception {

    if (!getOutputState().isInitialized()) {
        throw new WriterNotOpenException("Writer must be open before it can be written to");
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Writing to flat file with " + items.size() + " items.");
    }

    OutputState state = getOutputState();

    StringBuilder lines = new StringBuilder();
    int lineCount = 0;
    for (T item : items) {
        lines.append(lineAggregator.aggregate(item) + lineSeparator);
        lineCount++;
    }
    try {
        state.write(lines.toString());
    } catch (IOException e) {
        throw new WriteFailedException("Could not write data.  The file may be corrupt.", e);
    }
    state.linesWritten += lineCount;
}

From source file:org.geoserver.backuprestore.writer.CatalogFileWriter.java

@Override
public void write(List<? extends T> items) throws Exception {

    if (!getOutputState().isInitialized()) {
        throw new WriterNotOpenException("Writer must be open before it can be written to");
    }/*from  ww w  . ja v  a 2  s.c o m*/

    if (logger.isDebugEnabled()) {
        logger.debug("Writing to flat file with " + items.size() + " items.");
    }

    OutputState state = getOutputState();

    StringBuilder lines = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n");
    int lineCount = 0;

    if (items.size() > 0) {
        lines.append("<items>\n");
    }

    for (T item : items) {
        lines.append(doWrite(item));
        lineCount++;

        try {
            firePostWrite(item, resource);
        } catch (IOException e) {
            logValidationExceptions((ValidationResult) null,
                    new WriteFailedException("Could not write data.  The file may be corrupt.", e));
        }
    }

    if (items.size() > 0) {
        lines.append("</items>\n");
    }

    try {
        state.write(lines.toString());
    } catch (IOException e) {
        logValidationExceptions((ValidationResult) null,
                new WriteFailedException("Could not write data.  The file may be corrupt.", e));
    }
    state.linesWritten += lineCount;
}

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

/**
 * condition: skippable < fatal; exception is fatal
 *
 * expected: false/* w ww.  ja  va  2 s  .c  o  m*/
 */
@Test
public void testSkippableSubset_fatal() throws Exception {
    assertFalse(getSkippableSubsetSkipPolicy().shouldSkip(new WriterNotOpenException(""), 0));
}

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

/**
 * condition: fatal < skippable; exception is skippable
 *
 * expected: true/*w ww.  ja v a2 s.c o  m*/
 */
@Test
public void testFatalSubsetSkippable() throws Exception {
    assertTrue(getFatalSubsetSkipPolicy().shouldSkip(new WriterNotOpenException(""), 0));
}

From source file:org.springframework.batch.item.file.FlatFileItemWriter.java

/**
 * Writes out a string followed by a "new line", where the format of the new
 * line separator is determined by the underlying operating system. If the
 * input is not a String and a converter is available the converter will be
 * applied and then this method recursively called with the result. If the
 * input is an array or collection each value will be written to a separate
 * line (recursively calling this method for each value). If no converter is
 * supplied the input object's toString method will be used.<br>
 * //from   ww  w  . j a  va 2s  .c  o m
 * @param items list of items to be written to output stream
 * @throws Exception if the transformer or file output fail,
 * WriterNotOpenException if the writer has not been initialized.
 */
@Override
public void write(List<? extends T> items) throws Exception {

    if (!getOutputState().isInitialized()) {
        throw new WriterNotOpenException("Writer must be open before it can be written to");
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Writing to flat file with " + items.size() + " items.");
    }

    OutputState state = getOutputState();

    StringBuilder lines = new StringBuilder();
    int lineCount = 0;
    for (T item : items) {
        lines.append(lineAggregator.aggregate(item) + lineSeparator);
        lineCount++;
    }
    try {
        state.write(lines.toString());
    } catch (IOException e) {
        throw new WriteFailedException("Could not write data.  The file may be corrupt.", e);
    }
    state.linesWritten += lineCount;
}

From source file:org.springframework.batch.item.support.AbstractFileItemWriter.java

/**
 * Writes out a string followed by a "new line", where the format of the new
 * line separator is determined by the underlying operating system.
 * //from   ww  w . j a va 2  s . c o m
 * @param items list of items to be written to output stream
 * @throws Exception if an error occurs while writing items to the output stream
 */
@Override
public void write(List<? extends T> items) throws Exception {
    if (!getOutputState().isInitialized()) {
        throw new WriterNotOpenException("Writer must be open before it can be written to");
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Writing to file with " + items.size() + " items.");
    }

    OutputState state = getOutputState();

    String lines = doWrite(items);
    try {
        state.write(lines);
    } catch (IOException e) {
        throw new WriteFailedException("Could not write data. The file may be corrupt.", e);
    }
    state.setLinesWritten(state.getLinesWritten() + items.size());
}

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

/**
 * Write the value objects and flush them to the file.
 * /*from w  w w  . j av a  2 s  .  co m*/
 * @param items the value object
 * @throws IOException
 * @throws XmlMappingException
 */
@Override
public void write(List<? extends T> items) throws XmlMappingException, Exception {

    if (!this.initialized) {
        throw new WriterNotOpenException("Writer must be open before it can be written to");
    }

    currentRecordCount += items.size();

    for (Object object : items) {
        Assert.state(marshaller.supports(object.getClass()),
                "Marshaller must support the class of the marshalled object");
        Result result = createStaxResult();
        marshaller.marshal(object, result);
    }
    try {
        eventWriter.flush();
        if (forceSync) {
            channel.force(false);
        }
    } catch (XMLStreamException e) {
        throw new WriteFailedException("Failed to flush the events", e);
    } catch (IOException e) {
        throw new WriteFailedException("Failed to flush the events", e);
    }

}