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

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

Introduction

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

Prototype

public UnexpectedInputException(String msg, Throwable nested) 

Source Link

Document

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

Usage

From source file:org.geoserver.backuprestore.reader.CatalogFileReader.java

/**
 * Move to next fragment and map it to item.
 *///from w  ww .  j a  v  a 2  s.  c  om
@Override
protected T doRead() throws Exception {
    T item = null;
    try {
        if (noInput) {
            return null;
        }

        boolean success = false;
        try {
            success = moveCursorToNextFragment(fragmentReader);
        } catch (NonTransientResourceException e) {
            // Prevent caller from retrying indefinitely since this is fatal
            noInput = true;
            throw e;
        }
        if (success) {
            fragmentReader.markStartFragment();

            try {
                @SuppressWarnings("unchecked")
                T mappedFragment = (T) unmarshal(StaxUtils.getSource(fragmentReader));
                item = mappedFragment;

                try {
                    firePostRead(item, resource);
                } catch (IOException e) {
                    logValidationExceptions((ValidationResult) null,
                            new UnexpectedInputException("Could not write data.  The file may be corrupt.", e));
                }
            } finally {
                fragmentReader.markFragmentProcessed();
            }
        }
    } catch (Exception e) {
        logValidationExceptions((T) null, e);
    }

    return item;
}

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

@Test
public void testTransactionalRestart() throws Exception {
    writer.open(executionContext);/* w ww. j a v a2s. c o m*/

    PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();

    new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus status) {
            try {
                // write item
                writer.write(items);
            } catch (Exception e) {
                throw new UnexpectedInputException("Could not write data", e);
            }
            // get restart data
            writer.update(executionContext);
            return null;
        }
    });
    writer.close();

    // create new writer from saved restart data and continue writing
    writer = createItemWriter();
    writer.open(executionContext);
    new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus status) {
            try {
                writer.write(items);
            } catch (Exception e) {
                throw new UnexpectedInputException("Could not write data", e);
            }
            // get restart data
            writer.update(executionContext);
            return null;
        }
    });
    writer.close();

    // check the output is concatenation of 'before restart' and 'after
    // restart' writes.
    String outputFile = getOutputFileContent();
    assertEquals(2, StringUtils.countOccurrencesOf(outputFile, TEST_STRING));
    assertTrue(outputFile.contains("<root>" + TEST_STRING + TEST_STRING + "</root>"));
}

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

private void testTransactionalRestartWithMultiByteCharacter(String encoding) throws Exception {
    writer.setEncoding(encoding);//w  ww .  j  a  va 2  s . c  o m
    writer.open(executionContext);

    PlatformTransactionManager transactionManager = new ResourcelessTransactionManager();

    new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus status) {
            try {
                // write item
                writer.write(itemsMultiByte);
            } catch (Exception e) {
                throw new UnexpectedInputException("Could not write data", e);
            }
            // get restart data
            writer.update(executionContext);
            return null;
        }
    });
    writer.close();

    // create new writer from saved restart data and continue writing
    writer = createItemWriter();
    writer.setEncoding(encoding);
    writer.open(executionContext);
    new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus status) {
            try {
                writer.write(itemsMultiByte);
            } catch (Exception e) {
                throw new UnexpectedInputException("Could not write data", e);
            }
            // get restart data
            writer.update(executionContext);
            return null;
        }
    });
    writer.close();

    // check the output is concatenation of 'before restart' and 'after
    // restart' writes.
    String outputFile = getOutputFileContent(encoding);
    assertEquals(2, StringUtils.countOccurrencesOf(outputFile, TEST_STRING_MULTI_BYTE));
    assertTrue(outputFile.contains("<root>" + TEST_STRING_MULTI_BYTE + TEST_STRING_MULTI_BYTE + "</root>"));
}