From 0004375e442b08d693faf167914d6d066a112b9c Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Mon, 25 Feb 2008 19:06:24 +0000 Subject: [PATCH] Added namespace support for configuring endpoint-specific ErrorHandlers (INT-19). --- .../integration/config/EndpointParser.java | 8 ++++ .../config/spring-integration-1.0.xsd | 1 + .../message/MessageHandlingException.java | 28 ++++++++++++-- .../config/EndpointParserTests.java | 17 +++++++++ .../config/ExceptionThrowingTestHandler.java | 32 ++++++++++++++++ .../integration/config/TestErrorHandler.java | 37 +++++++++++++++++++ .../config/endpointWithErrorHandler.xml | 20 ++++++++++ 7 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/config/ExceptionThrowingTestHandler.java create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/config/TestErrorHandler.java create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/config/endpointWithErrorHandler.xml diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/EndpointParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/EndpointParser.java index 63f5d233e4..2f07a0cc0e 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/EndpointParser.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/EndpointParser.java @@ -76,6 +76,10 @@ public class EndpointParser implements BeanDefinitionParser { private static final String HANDLER_PROPERTY = "handler"; + private static final String ERROR_HANDLER_ATTRIBUTE = "error-handler"; + + private static final String ERROR_HANDLER_PROPERTY = "errorHandler"; + private static final String OBJECT_PROPERTY = "object"; private static final String METHOD_NAME_PROPERTY = "methodName"; @@ -170,6 +174,10 @@ public class EndpointParser implements BeanDefinitionParser { endpointDef.getPropertyValues().addPropertyValue(HANDLER_PROPERTY, new RuntimeBeanReference(handlerRef)); } } + String errorHandlerRef = element.getAttribute(ERROR_HANDLER_ATTRIBUTE); + if (StringUtils.hasText(errorHandlerRef)) { + endpointDef.getPropertyValues().addPropertyValue(ERROR_HANDLER_PROPERTY, new RuntimeBeanReference(errorHandlerRef)); + } String beanName = element.getAttribute(ID_ATTRIBUTE); if (!StringUtils.hasText(beanName)) { beanName = parserContext.getReaderContext().generateBeanName(endpointDef); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/spring-integration-1.0.xsd b/spring-integration-core/src/main/java/org/springframework/integration/config/spring-integration-1.0.xsd index 2952f6721d..d1e4172e59 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/spring-integration-1.0.xsd +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/spring-integration-1.0.xsd @@ -127,6 +127,7 @@ + diff --git a/spring-integration-core/src/main/java/org/springframework/integration/message/MessageHandlingException.java b/spring-integration-core/src/main/java/org/springframework/integration/message/MessageHandlingException.java index 21af3caf63..9df16ece94 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/message/MessageHandlingException.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/message/MessageHandlingException.java @@ -26,16 +26,36 @@ import org.springframework.integration.MessagingException; @SuppressWarnings("serial") public class MessageHandlingException extends MessagingException { + private Message failedMessage; + + public MessageHandlingException() { super(); } - public MessageHandlingException(String message) { - super(message); + public MessageHandlingException(Message failedMessage) { + this.failedMessage = failedMessage; } - public MessageHandlingException(String message, Throwable cause) { - super(message, cause); + public MessageHandlingException(String description) { + super(description); + } + + public MessageHandlingException(Message failedMessage, String description) { + super(description); + this.failedMessage = failedMessage; + } + + public MessageHandlingException(String description, Throwable cause) { + super(description, cause); + } + + + /** + * Return the failed {@link Message} if available, may be null. + */ + public Message getFailedMessage() { + return this.failedMessage; } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/EndpointParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/EndpointParserTests.java index 8c7eea4412..68feeee6cd 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/EndpointParserTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/EndpointParserTests.java @@ -33,8 +33,10 @@ import org.springframework.integration.endpoint.DefaultMessageEndpoint; import org.springframework.integration.handler.MessageHandler; import org.springframework.integration.message.GenericMessage; import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageHandlingException; import org.springframework.integration.message.StringMessage; import org.springframework.integration.message.selector.MessageSelectorRejectedException; +import org.springframework.integration.util.ErrorHandler; /** * @author Mark Fisher @@ -140,4 +142,19 @@ public class EndpointParserTests { endpoint.handle(new GenericMessage(123)); } + @Test + public void testCustomErrorHandler() { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( + "endpointWithErrorHandler.xml", this.getClass()); + MessageHandler endpoint = (MessageHandler) context.getBean("endpoint"); + TestErrorHandler errorHandler = (TestErrorHandler) context.getBean("errorHandler"); + assertNull(errorHandler.getLastError()); + Message message = new StringMessage("test"); + endpoint.handle(message); + Throwable error = errorHandler.getLastError(); + assertEquals(MessageHandlingException.class, error.getClass()); + MessageHandlingException exception = (MessageHandlingException) error; + assertEquals(message, exception.getFailedMessage()); + } + } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/ExceptionThrowingTestHandler.java b/spring-integration-core/src/test/java/org/springframework/integration/config/ExceptionThrowingTestHandler.java new file mode 100644 index 0000000000..a8708b0d4e --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/ExceptionThrowingTestHandler.java @@ -0,0 +1,32 @@ +/* + * Copyright 2002-2007 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.config; + +import org.springframework.integration.handler.MessageHandler; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessageHandlingException; + +/** + * @author Mark Fisher + */ +public class ExceptionThrowingTestHandler implements MessageHandler { + + public Message handle(Message message) { + throw new MessageHandlingException(message, "intentional test failure"); + } + +} diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/TestErrorHandler.java b/spring-integration-core/src/test/java/org/springframework/integration/config/TestErrorHandler.java new file mode 100644 index 0000000000..7328c051bf --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/TestErrorHandler.java @@ -0,0 +1,37 @@ +/* + * Copyright 2002-2007 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.config; + +import org.springframework.integration.util.ErrorHandler; + +/** + * @author Mark Fisher + */ +public class TestErrorHandler implements ErrorHandler { + + private volatile Throwable lastError; + + + public void handle(Throwable t) { + this.lastError = t; + } + + public Throwable getLastError() { + return this.lastError; + } + +} diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/endpointWithErrorHandler.xml b/spring-integration-core/src/test/java/org/springframework/integration/config/endpointWithErrorHandler.xml new file mode 100644 index 0000000000..5e6d7af4a7 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/endpointWithErrorHandler.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + +