From 3b9cd481ea8dd20cfa35fb93a84d09a03c1b5b95 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Tue, 20 May 2008 15:14:39 +0000 Subject: [PATCH] Added RootCauseErrorMessageRouter (INT-169). --- .../router/RootCauseErrorMessageRouter.java | 75 +++++++ .../RootCauseErrorMessageRouterTests.java | 197 ++++++++++++++++++ 2 files changed, 272 insertions(+) create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/router/RootCauseErrorMessageRouter.java create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/router/RootCauseErrorMessageRouterTests.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/router/RootCauseErrorMessageRouter.java b/spring-integration-core/src/main/java/org/springframework/integration/router/RootCauseErrorMessageRouter.java new file mode 100644 index 0000000000..00a64eba62 --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/router/RootCauseErrorMessageRouter.java @@ -0,0 +1,75 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed 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.springframework.integration.router; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.springframework.integration.channel.MessageChannel; +import org.springframework.integration.message.Message; +import org.springframework.util.Assert; + +/** + * A router implementation that resolves the {@link MessageChannel} for messages + * whose payload is an Exception. The channel resolution is based upon the most + * specific cause of the error for which a channel-mapping exists. + * + * @author Mark Fisher + */ +public class RootCauseErrorMessageRouter extends SingleChannelRouter { + + private Map, MessageChannel> channelMappings = + new ConcurrentHashMap, MessageChannel>(); + + private MessageChannel defaultChannel; + + + public RootCauseErrorMessageRouter() { + this.setChannelResolver(new RootCauseResolver()); + } + + + public void setChannelMappings(Map, MessageChannel> channelMappings) { + Assert.notNull(channelMappings, "'channelMappings' must not be null"); + this.channelMappings = channelMappings; + } + + public void setDefaultChannel(MessageChannel defaultChannel) { + this.defaultChannel = defaultChannel; + } + + + private class RootCauseResolver implements ChannelResolver { + + public MessageChannel resolve(Message message) { + MessageChannel channel = null; + Object payload = message.getPayload(); + if (payload != null && (payload instanceof Throwable)) { + Throwable mostSpecificCause = (Throwable) payload; + while (mostSpecificCause != null) { + MessageChannel mappedChannel = channelMappings.get(mostSpecificCause.getClass()); + if (mappedChannel != null) { + channel = mappedChannel; + } + mostSpecificCause = mostSpecificCause.getCause(); + } + } + return channel != null ? channel : defaultChannel; + } + } + +} diff --git a/spring-integration-core/src/test/java/org/springframework/integration/router/RootCauseErrorMessageRouterTests.java b/spring-integration-core/src/test/java/org/springframework/integration/router/RootCauseErrorMessageRouterTests.java new file mode 100644 index 0000000000..84456c5404 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/router/RootCauseErrorMessageRouterTests.java @@ -0,0 +1,197 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed 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.springframework.integration.router; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; + +import org.springframework.integration.channel.MessageChannel; +import org.springframework.integration.channel.QueueChannel; +import org.springframework.integration.message.ErrorMessage; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageDeliveryException; +import org.springframework.integration.message.MessageHandlingException; +import org.springframework.integration.message.StringMessage; + +/** + * @author Mark Fisher + */ +public class RootCauseErrorMessageRouterTests { + + private MessageChannel illegalArgumentChannel = new QueueChannel(); + + private MessageChannel runtimeExceptionChannel = new QueueChannel(); + + private MessageChannel messageHandlingExceptionChannel = new QueueChannel(); + + private MessageChannel messageDeliveryExceptionChannel = new QueueChannel(); + + private MessageChannel defaultChannel = new QueueChannel(); + + + @Test + public void testMostSpecificCause() { + Message failedMessage = new StringMessage("foo"); + IllegalArgumentException rootCause = new IllegalArgumentException("bad argument"); + RuntimeException middleCause = new RuntimeException(rootCause); + MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause); + ErrorMessage message = new ErrorMessage(error); + RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter(); + Map, MessageChannel> channelMappings = + new HashMap, MessageChannel>(); + channelMappings.put(IllegalArgumentException.class, illegalArgumentChannel); + channelMappings.put(RuntimeException.class, runtimeExceptionChannel); + channelMappings.put(MessageHandlingException.class, messageHandlingExceptionChannel); + router.setChannelMappings(channelMappings); + router.setDefaultChannel(defaultChannel); + router.afterPropertiesSet(); + router.handle(message); + assertNotNull(illegalArgumentChannel.receive(1000)); + assertNull(defaultChannel.receive(0)); + assertNull(runtimeExceptionChannel.receive(0)); + assertNull(messageHandlingExceptionChannel.receive(0)); + } + + @Test + public void testFallbackToNextMostSpecificCause() { + Message failedMessage = new StringMessage("foo"); + IllegalArgumentException rootCause = new IllegalArgumentException("bad argument"); + RuntimeException middleCause = new RuntimeException(rootCause); + MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause); + ErrorMessage message = new ErrorMessage(error); + RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter(); + Map, MessageChannel> channelMappings = + new HashMap, MessageChannel>(); + channelMappings.put(RuntimeException.class, runtimeExceptionChannel); + channelMappings.put(MessageHandlingException.class, messageHandlingExceptionChannel); + router.setChannelMappings(channelMappings); + router.setDefaultChannel(defaultChannel); + router.afterPropertiesSet(); + router.handle(message); + assertNotNull(runtimeExceptionChannel.receive(1000)); + assertNull(illegalArgumentChannel.receive(0)); + assertNull(defaultChannel.receive(0)); + assertNull(messageHandlingExceptionChannel.receive(0)); + } + + @Test + public void testFallbackToErrorMessageType() { + Message failedMessage = new StringMessage("foo"); + IllegalArgumentException rootCause = new IllegalArgumentException("bad argument"); + RuntimeException middleCause = new RuntimeException(rootCause); + MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause); + ErrorMessage message = new ErrorMessage(error); + RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter(); + Map, MessageChannel> channelMappings = + new HashMap, MessageChannel>(); + channelMappings.put(MessageHandlingException.class, messageHandlingExceptionChannel); + router.setChannelMappings(channelMappings); + router.setDefaultChannel(defaultChannel); + router.afterPropertiesSet(); + router.handle(message); + assertNotNull(messageHandlingExceptionChannel.receive(1000)); + assertNull(runtimeExceptionChannel.receive(0)); + assertNull(illegalArgumentChannel.receive(0)); + assertNull(defaultChannel.receive(0)); + } + + @Test + public void testFallbackToDefaultChannel() { + Message failedMessage = new StringMessage("foo"); + IllegalArgumentException rootCause = new IllegalArgumentException("bad argument"); + RuntimeException middleCause = new RuntimeException(rootCause); + MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause); + ErrorMessage message = new ErrorMessage(error); + RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter(); + router.setDefaultChannel(defaultChannel); + router.afterPropertiesSet(); + router.handle(message); + assertNotNull(defaultChannel.receive(1000)); + assertNull(runtimeExceptionChannel.receive(0)); + assertNull(illegalArgumentChannel.receive(0)); + assertNull(messageHandlingExceptionChannel.receive(0)); + } + + @Test(expected=MessageHandlingException.class) + public void testNoMatchAndNoDefaultChannel() { + Message failedMessage = new StringMessage("foo"); + IllegalArgumentException rootCause = new IllegalArgumentException("bad argument"); + RuntimeException middleCause = new RuntimeException(rootCause); + MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause); + ErrorMessage message = new ErrorMessage(error); + RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter(); + Map, MessageChannel> channelMappings = + new HashMap, MessageChannel>(); + channelMappings.put(MessageDeliveryException.class, messageDeliveryExceptionChannel); + router.setChannelMappings(channelMappings); + router.afterPropertiesSet(); + router.setResolutionRequired(true); + router.handle(message); + } + + @Test + public void testExceptionPayloadButNotErrorMessage() { + Message failedMessage = new StringMessage("foo"); + IllegalArgumentException rootCause = new IllegalArgumentException("bad argument"); + RuntimeException middleCause = new RuntimeException(rootCause); + MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause); + Message message = new GenericMessage(error); + RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter(); + Map, MessageChannel> channelMappings = + new HashMap, MessageChannel>(); + channelMappings.put(IllegalArgumentException.class, illegalArgumentChannel); + channelMappings.put(RuntimeException.class, runtimeExceptionChannel); + channelMappings.put(MessageHandlingException.class, messageHandlingExceptionChannel); + router.setChannelMappings(channelMappings); + router.setDefaultChannel(defaultChannel); + router.afterPropertiesSet(); + router.handle(message); + assertNotNull(illegalArgumentChannel.receive(1000)); + assertNull(defaultChannel.receive(0)); + assertNull(runtimeExceptionChannel.receive(0)); + assertNull(messageHandlingExceptionChannel.receive(0)); + } + + @Test + public void testIntermediateCauseHasNoMappingButMostSpecificCauseDoes() { + Message failedMessage = new StringMessage("foo"); + IllegalArgumentException rootCause = new IllegalArgumentException("bad argument"); + RuntimeException middleCause = new RuntimeException(rootCause); + MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause); + ErrorMessage message = new ErrorMessage(error); + RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter(); + Map, MessageChannel> channelMappings = + new HashMap, MessageChannel>(); + channelMappings.put(IllegalArgumentException.class, illegalArgumentChannel); + channelMappings.put(MessageHandlingException.class, messageHandlingExceptionChannel); + router.setChannelMappings(channelMappings); + router.setDefaultChannel(defaultChannel); + router.afterPropertiesSet(); + router.handle(message); + assertNotNull(illegalArgumentChannel.receive(1000)); + assertNull(defaultChannel.receive(0)); + assertNull(runtimeExceptionChannel.receive(0)); + assertNull(messageHandlingExceptionChannel.receive(0)); + } + +}