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

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

Introduction

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

Prototype

public WriteFailedException(String message, Throwable cause) 

Source Link

Document

Create a new WriteFailedException based on a message and another exception.

Usage

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

/**
 * Write  //  w  w w .java2 s .com
 * ? 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  a va 2  s.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.
 */
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.emonocot.job.io.StaxEventItemWriter.java

/**
 * Write the value objects and flush them to the file.
 *
 * @param items//ww w.j a va  2  s.  c  o  m
 *            the value object
 * @throws IOException
 *             if there is a problem writing to the resource
 */
public final void write(final List<? extends T> items) throws IOException {

    currentRecordCount += items.size();

    for (Object object : items) {
        Assert.state(marshaller.supports(object.getClass()),
                "Marshaller must support the class of the marshalled object");
        try {
            marshaller.marshal(object, StaxUtils.createStaxResult(eventWriter));
        } catch (XmlMappingException e) {
            throw new IOException(e.getMessage());
        } catch (XMLStreamException e) {
            throw new IOException(e.getMessage());
        }
    }
    try {
        eventWriter.flush();
    } catch (XMLStreamException e) {
        throw new WriteFailedException("Failed to flush the events", e);
    }

}

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");
    }//w  w  w .  j  ava  2s.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.geoserver.backuprestore.writer.CatalogFileWriter.java

/**
 * Initialize the reader. This method may be called multiple times before close is called.
 * //from w  ww  .j a va 2  s . com
 * @throws Exception
 * 
 * @see ItemStream#open(ExecutionContext)
 */
@Override
public void open(ExecutionContext executionContext) {
    super.open(executionContext);

    Assert.notNull(resource, "The resource must be set");

    if (!getOutputState().isInitialized()) {
        try {
            doOpen(executionContext);
        } catch (ItemStreamException e) {
            logValidationExceptions((T) null,
                    new WriteFailedException("Could not write data.  The file may be corrupt.", e));
        }
    }
}

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  w ww  .  ja  v  a  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 w w  w .j a va2 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.
 * //  www . j a  va  2 s  .  c om
 * @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);
    }

}