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);
}
}
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);
}
}
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"));
}
};
}
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());
}
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"));
}
};
}
Aggregations