Added RootCauseErrorMessageRouter (INT-169).

This commit is contained in:
Mark Fisher
2008-05-20 15:14:39 +00:00
parent 5ab34d7da3
commit 3b9cd481ea
2 changed files with 272 additions and 0 deletions

View File

@@ -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<Class<? extends Throwable>, MessageChannel> channelMappings =
new ConcurrentHashMap<Class<? extends Throwable>, MessageChannel>();
private MessageChannel defaultChannel;
public RootCauseErrorMessageRouter() {
this.setChannelResolver(new RootCauseResolver());
}
public void setChannelMappings(Map<Class<? extends Throwable>, 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;
}
}
}

View File

@@ -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<Class<? extends Throwable>, MessageChannel> channelMappings =
new HashMap<Class<? extends Throwable>, 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<Class<? extends Throwable>, MessageChannel> channelMappings =
new HashMap<Class<? extends Throwable>, 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<Class<? extends Throwable>, MessageChannel> channelMappings =
new HashMap<Class<? extends Throwable>, 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<Class<? extends Throwable>, MessageChannel> channelMappings =
new HashMap<Class<? extends Throwable>, 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<Exception>(error);
RootCauseErrorMessageRouter router = new RootCauseErrorMessageRouter();
Map<Class<? extends Throwable>, MessageChannel> channelMappings =
new HashMap<Class<? extends Throwable>, 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<Class<? extends Throwable>, MessageChannel> channelMappings =
new HashMap<Class<? extends Throwable>, 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));
}
}