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

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

Introduction

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

Prototype

public ItemStreamException(String msg, Throwable nested) 

Source Link

Document

Constructs a new instance with a message and nested exception.

Usage

From source file:lcn.module.batch.web.guide.support.ExPartitionFlatFileItemWriter.java

/**
 * Close  //ww w  . j  a va  2  s .c  o  m
 * state ? ? fileCount ? ?   state Close 
 *  ?  ? synchronized  ? 
 * @see ItemStream#close()
 */
public synchronized void close() {
    fileCount--; // increment for close Counting

    if (state != null && fileCount == 0) {
        try {

            if (footerCallback != null && state.outputBufferedWriter != null) {
                footerCallback.writeFooter(state.outputBufferedWriter);
                state.outputBufferedWriter.flush();
            }

        } catch (IOException e) {
            throw new ItemStreamException("Failed to write footer before closing", e);
        } finally {

            state.close();
            try {
                fileCloseTime = resource.getFile().lastModified();

            } catch (IOException e1) {
                logger.error(e1);
            }

            if (state.linesWritten == 0 && shouldDeleteIfEmpty) {
                try {
                    resource.getFile().delete();
                } catch (IOException e) {
                    throw new ItemStreamException("Failed to delete empty file on close", e);
                }
            }

            state = null;

        }
    }
}

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

/**
 * Close  //from w  w w .  j a v a2s  .c  o m
 * state ? ? fileCount ? ?   state Close 
 *  ?  ? synchronized  ? 
 * @see ItemStream#close()
 */
public synchronized void close() {
    fileCount--; // increment for close Counting

    if (state != null && fileCount == 0) {
        try {

            if (footerCallback != null && state.outputBufferedWriter != null) {
                footerCallback.writeFooter(state.outputBufferedWriter);
                state.outputBufferedWriter.flush();
            }

            fileCloseTime = resource.getFile().lastModified();

            if (state.linesWritten == 0 && shouldDeleteIfEmpty) {
                try {
                    resource.getFile().delete();
                } catch (IOException e) {
                    throw new ItemStreamException("Failed to delete empty file on close", e);
                }
            }

        } catch (ItemStreamException ie) {
            throw ie;
        } catch (IOException e) {
            throw new ItemStreamException("Failed to write footer before closing", e);
        } finally {

            state.close();

        }
    }
}

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

/**
 * @see ItemStream#close()/*from  ww  w  .  j  av  a2 s .  c  o m*/
 */
public void close() {
    if (state != null) {
        try {
            if (footerCallback != null && state.outputBufferedWriter != null) {
                footerCallback.writeFooter(state.outputBufferedWriter, (int) state.linesWritten);
                state.outputBufferedWriter.flush();
            }
        } catch (IOException e) {
            throw new ItemStreamException("Failed to write footer before closing", e);
        } finally {
            state.close();
            if (state.linesWritten == 0 && shouldDeleteIfEmpty) {
                try {
                    resource.getFile().delete();
                } catch (IOException e) {
                    throw new ItemStreamException("Failed to delete empty file on close", e);
                }
            }
            state = null;
        }
    }
}

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

/**
 * ?? open ? ?/*from w  w w . j  a v a  2 s . c  o  m*/
 * state ? BufferedWriter  
 * @see ItemStream#close()
 */
private void doOpen(ExecutionContext executionContext) throws ItemStreamException {

    OutputState outputState = getOutputState();

    if (executionContext.containsKey(getKey(RESTART_DATA_NAME))) {
        outputState.restoreFrom(executionContext);
    }
    try {
        outputState.initializeBufferedWriter();
        fileOpenTime = resource.getFile().lastModified();
        // ? Thread ?   ? Close ? Stream ?  ??    
        if ((fileOpenTime - fileCloseTime) / 1000.0 < 1) {

            throw new IOException("Failed to initialize writer");
        }
    } catch (IOException ioe) {
        throw new ItemStreamException("Failed to initialize writer", ioe);
    }
    if (outputState.lastMarkedByteOffsetPosition == 0 && !outputState.appending) {
        if (headerCallback != null) {
            try {
                headerCallback.writeHeader(outputState.outputBufferedWriter);
                outputState.write(lineSeparator);
            } catch (IOException e) {
                throw new ItemStreamException("Could not write headers.  The file may be corrupt.", e);
            }
        }
    }
}

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

private void doOpen(ExecutionContext executionContext) throws ItemStreamException {
    OutputState outputState = getOutputState();
    if (executionContext.containsKey(getKey(RESTART_DATA_NAME))) {
        outputState.restoreFrom(executionContext);
    }//from  w  w w  .j  ava  2 s  .  c o m
    try {
        outputState.initializeBufferedWriter();
    } catch (IOException ioe) {
        throw new ItemStreamException("Failed to initialize writer", ioe);
    }
    if (outputState.lastMarkedByteOffsetPosition == 0 && !outputState.appending) {
        if (headerCallback != null) {
            try {
                headerCallback.writeHeader(outputState.outputBufferedWriter);
                outputState.write(lineSeparator);
            } catch (IOException e) {
                throw new ItemStreamException("Could not write headers.  The file may be corrupt.", e);
            }
        }
    }
}

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

/**
 * ??   OutputState ? ?   //from w  w w .j a  va  2s .c  o m
 * @return
 */
private OutputState getOutputState() {
    if (state == null) {
        File file;
        try {
            file = resource.getFile();
        } catch (IOException e) {
            throw new ItemStreamException("Could not convert resource to file: [" + resource + "]", e);
        }
        Assert.state(!file.exists() || file.canWrite(), "Resource is not writable: [" + resource + "]");
        state = new OutputState();
        state.setDeleteIfExists(shouldDeleteIfExists);
        state.setAppendAllowed(append);
        state.setEncoding(encoding);
    }
    return (OutputState) state;
}

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

protected OutputState getOutputState() {
    if (state == null) {
        File file;/*from   w  w  w.ja v a  2 s  . c o  m*/
        try {
            file = resource.getFile();
        } catch (IOException e) {
            throw new ItemStreamException("Could not convert resource to file: [" + resource + "]", e);
        }
        Assert.state(!file.exists() || file.canWrite(), "Resource is not writable: [" + resource + "]");
        state = new OutputState();
        state.setDeleteIfExists(shouldDeleteIfExists);
        state.setAppendAllowed(append);
        state.setEncoding(encoding);
    }
    return (OutputState) state;
}

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

protected void newFile() {
    this.state = null;
    OutputState outputState = getOutputState();

    try {//from w  ww.j  a v a2 s  .c  om
        outputState.initializeBufferedWriter();
    } catch (IOException ioe) {
        throw new ItemStreamException("Failed to initialize writer", ioe);
    }

    if (outputState.lastMarkedByteOffsetPosition == 0 && !outputState.appending) {
        if (headerCallback != null) {
            try {
                headerCallback.writeHeader(outputState.outputBufferedWriter);
                outputState.write(lineSeparator);
            } catch (IOException e) {
                throw new ItemStreamException("Could not write headers.  The file may be corrupt.", e);
            }
        }
    }
}

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

/**
 * Open the output source.//from   w  w  w.j  a  v  a  2s.c  om
 * @param newExecutionContext Set the execution context
 * @see org.springframework.batch.item.ItemStream#open(ExecutionContext)
 */
public final void open(final ExecutionContext newExecutionContext) {

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

    long startAtPosition = 0;
    boolean restarted = false;

    // if restart data is provided, restart from provided offset
    // otherwise start from beginning
    if (newExecutionContext.containsKey(getKey(RESTART_DATA_NAME))) {
        startAtPosition = newExecutionContext.getLong(getKey(RESTART_DATA_NAME));
        restarted = true;
    }

    open(startAtPosition, restarted);

    if (startAtPosition == 0) {
        try {
            if (headerCallback != null) {
                headerCallback.write(delegateEventWriter);
            }
        } catch (IOException e) {
            throw new ItemStreamException("Failed to write headerItems", e);
        }
    }

}

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

/**
 * Flush and close the output source./*from  www.  ja va2 s  .  c  o m*/
 *
 * @see org.springframework.batch.item.ItemStream#close()
 */
public final void close() {

    // harmless event to close the root tag if there were no items
    XMLEventFactory factory = XMLEventFactory.newInstance();
    try {
        delegateEventWriter.add(factory.createCharacters(""));
    } catch (XMLStreamException e) {
        log.error(e);
    }

    try {
        if (footerCallback != null) {
            footerCallback.write(delegateEventWriter);
        }
        delegateEventWriter.flush();
        endDocument(delegateEventWriter);
    } catch (IOException e) {
        throw new ItemStreamException("Failed to write footer items", e);
    } catch (XMLStreamException e) {
        throw new ItemStreamException("Failed to write end document tag", e);
    } finally {

        try {
            eventWriter.close();
        } catch (XMLStreamException e) {
            log.error("Unable to close file resource: [" + resource + "] " + e);
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                log.error("Unable to close file resource: [" + resource + "] " + e);
            } finally {
                if (!transactional) {
                    closeStream();
                }
            }
        }
    }
}