Added namespace support for configuring endpoint-specific ErrorHandlers (INT-19).

This commit is contained in:
Mark Fisher
2008-02-25 19:06:24 +00:00
parent b7aa312c48
commit 0004375e44
7 changed files with 139 additions and 4 deletions

View File

@@ -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);

View File

@@ -127,6 +127,7 @@
<xsd:attribute name="default-output-channel" type="xsd:string"/>
<xsd:attribute name="handler-ref" type="xsd:string"/>
<xsd:attribute name="handler-method" type="xsd:string"/>
<xsd:attribute name="error-handler" type="xsd:string"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

View File

@@ -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;
}
}

View File

@@ -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<Integer>(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());
}
}

View File

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

View File

@@ -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;
}
}

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<message-bus/>
<channel id="testChannel"/>
<endpoint id="endpoint" input-channel="testChannel" handler-ref="testHandler" error-handler="errorHandler"/>
<beans:bean id="testHandler" class="org.springframework.integration.config.ExceptionThrowingTestHandler"/>
<beans:bean id="errorHandler" class="org.springframework.integration.config.TestErrorHandler"/>
</beans:beans>