CAMEL-7050: DeadLetterChannel should make more clear that it handles any new exception also. Added option to configure this behavior so ppl can turn that off and let new exceptions be unhandled, so transactions can rollback.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/42ded87e Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/42ded87e Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/42ded87e Branch: refs/heads/master Commit: 42ded87e1256b2aabdd0bd0ed5f2e87fcac766b1 Parents: 296c5af Author: Claus Ibsen <[email protected]> Authored: Sun Jan 18 14:44:32 2015 +0100 Committer: Claus Ibsen <[email protected]> Committed: Sun Jan 18 14:44:32 2015 +0100 ---------------------------------------------------------------------- .../builder/DefaultErrorHandlerBuilder.java | 9 +++ .../camel/processor/RedeliveryErrorHandler.java | 44 +++++++++------ .../DeadLetterChannelNewExceptionTest.java | 50 +++++++++++++++++ ...dLetterChannelNotHandleNewExceptionTest.java | 58 ++++++++++++++++++++ .../src/test/resources/log4j.properties | 2 +- 5 files changed, 146 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/42ded87e/camel-core/src/main/java/org/apache/camel/builder/DefaultErrorHandlerBuilder.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/DefaultErrorHandlerBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/DefaultErrorHandlerBuilder.java index 417728d..091c166 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/DefaultErrorHandlerBuilder.java +++ b/camel-core/src/main/java/org/apache/camel/builder/DefaultErrorHandlerBuilder.java @@ -366,6 +366,15 @@ public class DefaultErrorHandlerBuilder extends ErrorHandlerBuilderSupport { return this; } + /** + * @deprecated use {@link #deadLetterHandleNewException(boolean)}} with value <tt>false</tt> + */ + @Deprecated + public DefaultErrorHandlerBuilder checkException() { + setDeadLetterHandleNewException(false); + return this; + } + // Properties // ------------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/42ded87e/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java b/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java index c50c87c..8fdea29 100644 --- a/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java +++ b/camel-core/src/main/java/org/apache/camel/processor/RedeliveryErrorHandler.java @@ -890,7 +890,7 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport impleme public void done(boolean sync) { log.trace("Failure processor done: {} processing Exchange: {}", processor, exchange); try { - prepareExchangeAfterFailure(exchange, data, isDeadLetterChannel, shouldHandle, shouldContinue); + prepareExchangeAfterFailure(processor, exchange, data, isDeadLetterChannel, shouldHandle, shouldContinue); // fire event as we had a failure processor to handle it, which there is a event for boolean deadLetterChannel = processor == data.deadLetterProcessor; EventHelper.notifyExchangeFailureHandled(exchange.getContext(), exchange, processor, deadLetterChannel); @@ -904,7 +904,7 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport impleme } else { try { // no processor but we need to prepare after failure as well - prepareExchangeAfterFailure(exchange, data, isDeadLetterChannel, shouldHandle, shouldContinue); + prepareExchangeAfterFailure(processor, exchange, data, isDeadLetterChannel, shouldHandle, shouldContinue); } finally { // callback we are done callback.done(data.sync); @@ -924,19 +924,28 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport impleme return sync; } - protected void prepareExchangeAfterFailure(final Exchange exchange, final RedeliveryData data, + protected void prepareExchangeAfterFailure(final Processor failureProcessor, final Exchange exchange, final RedeliveryData data, final boolean isDeadLetterChannel, final boolean shouldHandle, final boolean shouldContinue) { Exception newException = exchange.getException(); if (newException != null && data.currentRedeliveryPolicy.isLogNewException()) { - logFailedDelivery(false, true, false, false, exchange, null, data, newException); + boolean handled = data.handleNewException; + String msg = "New exception occurred during processing by the failure processor " + failureProcessor + " due " + newException.getMessage(); + if (handled) { + msg += ". The new exception is being handled as deadLetterHandleNewException=true."; + } else { + msg += ". The new exception is not handled as deadLetterHandleNewException=false."; + } + logFailedDelivery(false, true, handled, false, exchange, msg, data, newException); } // we could not process the exchange so we let the failure processor handled it ExchangeHelper.setFailureHandled(exchange); + // special for dead letter channel where it by default handle new exceptions, but its possible to turn that off if (isDeadLetterChannel && shouldHandle) { - if (data.handleNewException) { + boolean handled = data.handleNewException; + if (handled) { // if there is a new exception then log a warning about that log.trace("This exchange is handled so its marked as not failed: {}", exchange); } else { @@ -984,7 +993,8 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport impleme } } - private void logFailedDelivery(boolean shouldRedeliver, boolean newException, boolean handled, boolean continued, Exchange exchange, String message, RedeliveryData data, Throwable e) { + private void logFailedDelivery(boolean shouldRedeliver, boolean newException, boolean handled, boolean continued, + Exchange exchange, String message, RedeliveryData data, Throwable e) { if (logger == null) { return; } @@ -996,22 +1006,22 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport impleme } // if we should not rollback, then check whether logging is enabled - if (handled && !data.currentRedeliveryPolicy.isLogHandled()) { + if (!newException && handled && !data.currentRedeliveryPolicy.isLogHandled()) { // do not log handled return; } - if (continued && !data.currentRedeliveryPolicy.isLogContinued()) { + if (!newException && continued && !data.currentRedeliveryPolicy.isLogContinued()) { // do not log handled return; } - if (shouldRedeliver && !data.currentRedeliveryPolicy.isLogRetryAttempted()) { + if (!newException && shouldRedeliver && !data.currentRedeliveryPolicy.isLogRetryAttempted()) { // do not log retry attempts return; } - if (!shouldRedeliver && !data.currentRedeliveryPolicy.isLogExhausted()) { + if (!newException && !shouldRedeliver && !data.currentRedeliveryPolicy.isLogExhausted()) { // do not log exhausted return; } @@ -1038,11 +1048,14 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport impleme if (newLogLevel == LoggingLevel.ERROR) { newLogLevel = LoggingLevel.WARN; } - // special for logging the new exception - String msg = "New exception " + ExchangeHelper.logIds(exchange); - Throwable cause = exchange.getException() != null ? exchange.getException() : exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class); - if (cause != null) { - msg = msg + " due: " + cause.getMessage(); + String msg = message; + if (msg == null) { + msg = "New exception " + ExchangeHelper.logIds(exchange); + // special for logging the new exception + Throwable cause = e; + if (cause != null) { + msg = msg + " due: " + cause.getMessage(); + } } if (e != null && logStackTrace) { @@ -1057,7 +1070,6 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport impleme msg = msg + " due: " + cause.getMessage(); } - // should we include message history if (!shouldRedeliver && data.currentRedeliveryPolicy.isLogExhaustedMessageHistory()) { String routeStackTrace = MessageHelper.dumpMessageHistoryStacktrace(exchange, exchangeFormatter, false); http://git-wip-us.apache.org/repos/asf/camel/blob/42ded87e/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNewExceptionTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNewExceptionTest.java b/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNewExceptionTest.java new file mode 100644 index 0000000..5b1b1c5 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNewExceptionTest.java @@ -0,0 +1,50 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.processor; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.Handler; +import org.apache.camel.builder.RouteBuilder; + +public class DeadLetterChannelNewExceptionTest extends ContextTestSupport { + + public static class BadErrorHandler { + @Handler + public void onException(Exchange exchange, Exception exception) throws Exception { + throw new RuntimeException("error in errorhandler"); + } + } + + public void testDeadLetterChannelNewException() throws Exception { + template.sendBody("direct:start", "Hello World"); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + errorHandler(deadLetterChannel("bean:" + BadErrorHandler.class.getName())); + + from("direct:start") + .log("Incoming ${body}") + .throwException(new IllegalArgumentException("Forced")); + } + }; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/42ded87e/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNotHandleNewExceptionTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNotHandleNewExceptionTest.java b/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNotHandleNewExceptionTest.java new file mode 100644 index 0000000..df4ed94 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelNotHandleNewExceptionTest.java @@ -0,0 +1,58 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.processor; + +import org.apache.camel.CamelExecutionException; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.Handler; +import org.apache.camel.builder.RouteBuilder; + +public class DeadLetterChannelNotHandleNewExceptionTest extends ContextTestSupport { + + public static class BadErrorHandler { + @Handler + public void onException(Exchange exchange, Exception exception) throws Exception { + throw new RuntimeException("error in errorhandler"); + } + } + + public void testDeadLetterChannelNewException() throws Exception { + try { + template.sendBody("direct:start", "Hello World"); + fail("Should have thrown exception"); + } catch (CamelExecutionException e) { + RuntimeException cause = assertIsInstanceOf(RuntimeException.class, e.getCause()); + assertEquals("error in errorhandler", cause.getMessage()); + } + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + errorHandler(deadLetterChannel("bean:" + BadErrorHandler.class.getName()) + .deadLetterHandleNewException(false)); + + from("direct:start") + .log("Incoming ${body}") + .throwException(new IllegalArgumentException("Forced")); + } + }; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/42ded87e/components/camel-jms/src/test/resources/log4j.properties ---------------------------------------------------------------------- diff --git a/components/camel-jms/src/test/resources/log4j.properties b/components/camel-jms/src/test/resources/log4j.properties index 14beeb0..4732904 100644 --- a/components/camel-jms/src/test/resources/log4j.properties +++ b/components/camel-jms/src/test/resources/log4j.properties @@ -18,7 +18,7 @@ # # The logging properties used # -log4j.rootLogger=INFO, stdout +log4j.rootLogger=INFO, out #log4j.logger.org.apache.activemq.spring=WARN #log4j.logger.org.apache.camel.component.jms=DEBUG
