Search in sources :

Example 31 with IOException

use of java.io.IOException in project camel by apache.

the class FileStateRepository method appendToStore.

/**
     * Appends the {@code <key,value>} pair to the file store
     *
     * @param key the state key
     */
private void appendToStore(String key, String value) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Appending {}={} to state filestore: {}", new Object[] { key, value, fileStore });
    }
    FileOutputStream fos = null;
    try {
        // create store parent directory if missing
        File storeParentDirectory = fileStore.getParentFile();
        if (storeParentDirectory != null && !storeParentDirectory.exists()) {
            LOG.info("Parent directory of file store {} doesn't exist. Creating.", fileStore);
            if (fileStore.getParentFile().mkdirs()) {
                LOG.info("Parent directory of file store {} successfully created.", fileStore);
            } else {
                LOG.warn("Parent directory of file store {} cannot be created.", fileStore);
            }
        }
        // create store if missing
        if (!fileStore.exists()) {
            FileUtil.createNewFile(fileStore);
        }
        // append to store
        fos = new FileOutputStream(fileStore, true);
        fos.write(key.getBytes());
        fos.write(KEY_VALUE_DELIMITER.getBytes());
        fos.write(value.getBytes());
        fos.write(STORE_DELIMITER.getBytes());
    } catch (IOException e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
    } finally {
        IOHelper.close(fos, "Appending to file state repository", LOG);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 32 with IOException

use of java.io.IOException in project camel by apache.

the class FileStateRepository method trunkStore.

/**
     * Trunks the file store when the max store size is hit by rewriting the 1st level cache
     * to the file store.
     */
protected void trunkStore() {
    LOG.info("Trunking state filestore: {}", fileStore);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(fileStore);
        for (Map.Entry<String, String> entry : cache.entrySet()) {
            fos.write(entry.getKey().getBytes());
            fos.write(KEY_VALUE_DELIMITER.getBytes());
            fos.write(entry.getValue().getBytes());
            fos.write(STORE_DELIMITER.getBytes());
        }
    } catch (IOException e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
    } finally {
        IOHelper.close(fos, "Trunking file state repository", LOG);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 33 with IOException

use of java.io.IOException in project camel by apache.

the class ContextScopedOnExceptionRouteScopedErrorHandlerRefIssueTwoRoutesTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            onException(IllegalArgumentException.class).handled(true).to("mock:handled").end();
            from("direct:foo").errorHandler(new ErrorHandlerBuilderRef("myDLC")).to("mock:foo").throwException(new IOException("Damn IO"));
            from("direct:start").errorHandler(new ErrorHandlerBuilderRef("myDLC")).to("mock:a").throwException(new IllegalArgumentException("Damn"));
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) IOException(java.io.IOException) ErrorHandlerBuilderRef(org.apache.camel.builder.ErrorHandlerBuilderRef)

Example 34 with IOException

use of java.io.IOException in project camel by apache.

the class ExceptionThrownFromOnExceptionTest method testNoExceptionThrownFromOnExceptionAndHandledWithDeadLetterChannel.

public void testNoExceptionThrownFromOnExceptionAndHandledWithDeadLetterChannel() throws Exception {
    RETRY.set(0);
    ON_EXCEPTION_RETRY.set(0);
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // DLC
            deadLetterChannel("mock:error").redeliveryDelay(0).maximumRedeliveries(3);
            // on exception to catch all IO exceptions and handle them specially
            onException(IOException.class).maximumRedeliveries(3).handled(true).to("mock:b").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    ON_EXCEPTION_RETRY.incrementAndGet();
                // no exception is thrown this time
                }
            }).to("mock:c");
            from("direct:start").to("direct:intermediate").to("mock:result");
            from("direct:intermediate").to("mock:a").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    RETRY.incrementAndGet();
                    throw new IOException("IO error");
                }
            }).to("mock:end");
        }
    });
    context.start();
    getMockEndpoint("mock:a").expectedMessageCount(1);
    getMockEndpoint("mock:b").expectedMessageCount(1);
    getMockEndpoint("mock:c").expectedMessageCount(1);
    getMockEndpoint("mock:result").expectedMessageCount(0);
    getMockEndpoint("mock:end").expectedMessageCount(0);
    // the exception is handled by onException so it goes not in DLC
    getMockEndpoint("mock:error").expectedMessageCount(0);
    // and this time there was no exception thrown from onException,
    // and the exception is handled so the caller should not fail
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
    assertEquals("Should try 4 times (1 first, 3 retry)", 4, RETRY.get());
    assertEquals("Should only invoke onException once", 1, ON_EXCEPTION_RETRY.get());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) IOException(java.io.IOException) CamelExecutionException(org.apache.camel.CamelExecutionException) IOException(java.io.IOException)

Example 35 with IOException

use of java.io.IOException in project camel by apache.

the class ContextScopedOnExceptionNotHandledErrorHandlerRefIssueTwoRoutesTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            errorHandler(new ErrorHandlerBuilderRef("myDLC"));
            onException(IllegalArgumentException.class).handled(false).to("mock:handled").end();
            from("direct:foo").to("mock:foo").throwException(new IOException("Damn IO"));
            from("direct:start").to("mock:a").throwException(new IllegalArgumentException("Damn"));
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) IOException(java.io.IOException) ErrorHandlerBuilderRef(org.apache.camel.builder.ErrorHandlerBuilderRef)

Aggregations

IOException (java.io.IOException)41104 File (java.io.File)7663 InputStream (java.io.InputStream)4105 Test (org.junit.Test)3557 ArrayList (java.util.ArrayList)3023 FileInputStream (java.io.FileInputStream)2674 FileOutputStream (java.io.FileOutputStream)2358 FileNotFoundException (java.io.FileNotFoundException)2146 BufferedReader (java.io.BufferedReader)2028 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1761 InputStreamReader (java.io.InputStreamReader)1677 URL (java.net.URL)1608 HashMap (java.util.HashMap)1552 ByteArrayInputStream (java.io.ByteArrayInputStream)1534 OutputStream (java.io.OutputStream)1349 Path (org.apache.hadoop.fs.Path)1316 Map (java.util.Map)1212 List (java.util.List)1042 Properties (java.util.Properties)994 ServletException (javax.servlet.ServletException)916