ReplyHandler is now configurable on the DefaultMessageEndpoint. The <endpoint/> element also accepts a 'reply-handler' attribute (INT-129).

This commit is contained in:
Mark Fisher
2008-02-26 02:35:09 +00:00
parent abedfb0b3d
commit 51d02ee8ad
7 changed files with 94 additions and 1 deletions

View File

@@ -80,6 +80,10 @@ public class EndpointParser implements BeanDefinitionParser {
private static final String ERROR_HANDLER_PROPERTY = "errorHandler";
private static final String REPLY_HANDLER_ATTRIBUTE = "reply-handler";
private static final String REPLY_HANDLER_PROPERTY = "replyHandler";
private static final String OBJECT_PROPERTY = "object";
private static final String METHOD_NAME_PROPERTY = "methodName";
@@ -178,6 +182,10 @@ public class EndpointParser implements BeanDefinitionParser {
if (StringUtils.hasText(errorHandlerRef)) {
endpointDef.getPropertyValues().addPropertyValue(ERROR_HANDLER_PROPERTY, new RuntimeBeanReference(errorHandlerRef));
}
String replyHandlerRef = element.getAttribute(REPLY_HANDLER_ATTRIBUTE);
if (StringUtils.hasText(replyHandlerRef)) {
endpointDef.getPropertyValues().addPropertyValue(REPLY_HANDLER_PROPERTY, new RuntimeBeanReference(replyHandlerRef));
}
String beanName = element.getAttribute(ID_ATTRIBUTE);
if (!StringUtils.hasText(beanName)) {
beanName = parserContext.getReaderContext().generateBeanName(endpointDef);

View File

@@ -128,6 +128,7 @@
<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:attribute name="reply-handler" type="xsd:string"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

View File

@@ -71,7 +71,7 @@ public class DefaultMessageEndpoint implements MessageEndpoint, ChannelRegistryA
private final List<MessageSelector> selectors = new CopyOnWriteArrayList<MessageSelector>();
private final ReplyHandler replyHandler = new EndpointReplyHandler();
private volatile ReplyHandler replyHandler = new EndpointReplyHandler();
private volatile long replyTimeout = 1000;
@@ -149,6 +149,11 @@ public class DefaultMessageEndpoint implements MessageEndpoint, ChannelRegistryA
return (this.errorHandler != null);
}
public void setReplyHandler(ReplyHandler replyHandler) {
Assert.notNull(replyHandler, "'replyHandler' must not be null");
this.replyHandler = replyHandler;
}
/**
* Set the timeout in milliseconds to be enforced when this endpoint sends a
* reply message. If the message is not sent successfully within the

View File

@@ -157,4 +157,18 @@ public class EndpointParserTests {
assertEquals(message, exception.getFailedMessage());
}
@Test
public void testCustomReplyHandler() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"endpointWithReplyHandler.xml", this.getClass());
MessageHandler endpoint = (MessageHandler) context.getBean("endpoint");
TestReplyHandler replyHandler = (TestReplyHandler) context.getBean("replyHandler");
assertNull(replyHandler.getLastMessage());
Message<?> message = new StringMessage("test");
endpoint.handle(message);
Message<?> reply = replyHandler.getLastMessage();
assertNotNull(reply);
assertEquals("foo", reply.getPayload());
}
}

View File

@@ -34,6 +34,10 @@ public class TestHandler implements MessageHandler {
private String replyMessageText = null;
public TestHandler() {
this(1);
}
public TestHandler(int countdown) {
this.latch = new CountDownLatch(countdown);
}

View File

@@ -0,0 +1,39 @@
/*
* 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.ReplyHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageHeader;
/**
* @author Mark Fisher
*/
public class TestReplyHandler implements ReplyHandler {
private volatile Message<?> lastMessage;
public void handle(Message<?> replyMessage, MessageHeader originalMessageHeader) {
this.lastMessage = replyMessage;
}
public Message<?> getLastMessage() {
return this.lastMessage;
}
}

View File

@@ -0,0 +1,22 @@
<?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" reply-handler="replyHandler"/>
<beans:bean id="testHandler" class="org.springframework.integration.config.TestHandler">
<beans:property name="replyMessageText" value="foo"/>
</beans:bean>
<beans:bean id="replyHandler" class="org.springframework.integration.config.TestReplyHandler"/>
</beans:beans>