Added namespace support for GatewayProxyFactoryBean with the new <gateway/> element (INT-226).

This commit is contained in:
Mark Fisher
2008-05-21 14:51:23 +00:00
parent c7e8502c94
commit 938ce0a5a5
7 changed files with 293 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ import org.springframework.integration.channel.config.PriorityChannelParser;
import org.springframework.integration.channel.config.QueueChannelParser;
import org.springframework.integration.channel.config.RendezvousChannelParser;
import org.springframework.integration.channel.config.ThreadLocalChannelParser;
import org.springframework.integration.gateway.config.GatewayParser;
import org.springframework.integration.router.config.RouterParser;
import org.springframework.integration.router.config.SplitterParser;
import org.springframework.util.ClassUtils;
@@ -66,6 +67,7 @@ public class IntegrationNamespaceHandler extends NamespaceHandlerSupport {
registerBeanDefinitionParser("source-endpoint", new SourceEndpointParser());
registerBeanDefinitionParser("handler-endpoint", new HandlerEndpointParser());
registerBeanDefinitionParser("target-endpoint", new TargetEndpointParser());
registerBeanDefinitionParser("gateway", new GatewayParser());
registerBeanDefinitionParser("handler", new HandlerParser());
registerBeanDefinitionParser("handler-chain", new HandlerParser());
registerBeanDefinitionParser("router", new RouterParser());

View File

@@ -136,6 +136,24 @@
</xsd:complexType>
</xsd:element>
<xsd:element name="gateway">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a Messaging Gateway.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:ID" use="required"/>
<xsd:attribute name="service-interface" type="xsd:string" use="required"/>
<xsd:attribute name="request-channel" type="xsd:string"/>
<xsd:attribute name="reply-channel" type="xsd:string"/>
<xsd:attribute name="request-timeout" type="xsd:long"/>
<xsd:attribute name="reply-timeout" type="xsd:long"/>
<xsd:attribute name="message-mapper" type="xsd:string"/>
<xsd:attribute name="message-creator" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="source-endpoint">
<xsd:complexType>
<xsd:annotation>

View File

@@ -0,0 +1,62 @@
/*
* 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.gateway.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.core.Conventions;
import org.springframework.integration.gateway.GatewayProxyFactoryBean;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* Parser for the &lt;gateway/&gt; element.
*
* @author Mark Fisher
*/
public class GatewayParser extends AbstractSimpleBeanDefinitionParser {
private static String[] referenceAttributes = new String[] {
"request-channel", "reply-channel", "message-mapper", "message-creator"
};
@Override
protected Class<?> getBeanClass(Element element) {
return GatewayProxyFactoryBean.class;
}
@Override
protected boolean isEligibleAttribute(String attributeName) {
return !ObjectUtils.containsElement(referenceAttributes, attributeName)
&& super.isEligibleAttribute(attributeName);
}
@Override
protected void postProcess(BeanDefinitionBuilder beanDefinition, Element element) {
for (String attributeName : referenceAttributes) {
String beanName = element.getAttribute(attributeName);
if (StringUtils.hasText(beanName)) {
beanDefinition.addPropertyReference(
Conventions.attributeNameToPropertyName(attributeName), beanName);
}
}
}
}

View File

@@ -0,0 +1,102 @@
/*
* 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.gateway.config;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.Executors;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.gateway.TestService;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class GatewayParserTests {
@Test
public void testOneWay() {
ApplicationContext context = new ClassPathXmlApplicationContext("gatewayParserTests.xml", this.getClass());
TestService service = (TestService) context.getBean("oneWay");
service.oneWay("foo");
MessageChannel channel = (MessageChannel) context.getBean("requestChannel");
Message<?> result = channel.receive(1000);
assertEquals("foo", result.getPayload());
}
@Test
public void testSolicitResponse() {
ApplicationContext context = new ClassPathXmlApplicationContext("gatewayParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("replyChannel");
channel.send(new StringMessage("foo"));
TestService service = (TestService) context.getBean("solicitResponse");
String result = service.solicitResponse();
assertEquals("foo", result);
}
@Test
public void testRequestReply() {
ApplicationContext context = new ClassPathXmlApplicationContext("gatewayParserTests.xml", this.getClass());
MessageChannel requestChannel = (MessageChannel) context.getBean("requestChannel");
MessageChannel replyChannel = (MessageChannel) context.getBean("replyChannel");
this.startResponder(requestChannel, replyChannel);
TestService service = (TestService) context.getBean("requestReply");
String result = service.requestReply("foo");
assertEquals("foobar", result);
}
@Test
public void testRequestReplyWithMessageMapper() {
ApplicationContext context = new ClassPathXmlApplicationContext("gatewayParserTests.xml", this.getClass());
MessageChannel requestChannel = (MessageChannel) context.getBean("requestChannel");
MessageChannel replyChannel = (MessageChannel) context.getBean("replyChannel");
this.startResponder(requestChannel, replyChannel);
TestService service = (TestService) context.getBean("requestReplyWithMessageMapper");
String result = service.requestReply("foo");
assertEquals("foobar.mapped", result);
}
@Test
public void testRequestReplyWithMessageCreator() {
ApplicationContext context = new ClassPathXmlApplicationContext("gatewayParserTests.xml", this.getClass());
MessageChannel requestChannel = (MessageChannel) context.getBean("requestChannel");
MessageChannel replyChannel = (MessageChannel) context.getBean("replyChannel");
this.startResponder(requestChannel, replyChannel);
TestService service = (TestService) context.getBean("requestReplyWithMessageCreator");
String result = service.requestReply("foo");
assertEquals("created.foobar", result);
}
private void startResponder(final MessageChannel requestChannel, final MessageChannel replyChannel) {
Executors.newSingleThreadExecutor().execute(new Runnable() {
public void run() {
Message<?> request = requestChannel.receive();
Message<?> reply = new StringMessage(request.getPayload() + "bar");
reply.getHeader().setCorrelationId(request.getId());
replyChannel.send(reply);
}
});
}
}

View File

@@ -0,0 +1,32 @@
/*
* 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.gateway.config;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageCreator;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class TestMessageCreator implements MessageCreator<String, String> {
public Message<String> createMessage(String s) {
return new StringMessage("created." + s);
}
}

View File

@@ -0,0 +1,31 @@
/*
* 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.gateway.config;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageMapper;
/**
* @author Mark Fisher
*/
public class TestMessageMapper implements MessageMapper<String, String> {
public String mapMessage(Message<String> message) {
return message.getPayload() + ".mapped";
}
}

View File

@@ -0,0 +1,46 @@
<?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-core-1.0.xsd">
<message-bus/>
<channel id="requestChannel"/>
<channel id="replyChannel"/>
<gateway id="oneWay"
service-interface="org.springframework.integration.gateway.TestService"
request-channel="requestChannel"/>
<gateway id="solicitResponse"
service-interface="org.springframework.integration.gateway.TestService"
reply-channel="replyChannel"
reply-timeout="3000"/>
<gateway id="requestReply"
service-interface="org.springframework.integration.gateway.TestService"
request-channel="requestChannel"
reply-channel="replyChannel"/>
<gateway id="requestReplyWithMessageMapper"
service-interface="org.springframework.integration.gateway.TestService"
request-channel="requestChannel"
reply-channel="replyChannel"
message-mapper="mapper"/>
<gateway id="requestReplyWithMessageCreator"
service-interface="org.springframework.integration.gateway.TestService"
request-channel="requestChannel"
reply-channel="replyChannel"
message-creator="creator"/>
<beans:bean id="mapper" class="org.springframework.integration.gateway.config.TestMessageMapper"/>
<beans:bean id="creator" class="org.springframework.integration.gateway.config.TestMessageCreator"/>
</beans:beans>