Spring Integration为集成系统所涉及的某些复杂性提供了非常好的抽象-Spring Integration从Integration的角度来看非常适合Facade的定义-简化了对复杂底层系统的访问。
为了说明这一点,请考虑一个简单的系统,该系统仅接收一条消息,然后将其发送回大写,称其为Echo网关:
public interface EchoGateway { String echo(String message);
}
并为此进行测试:
@Testpublic void testEcho() {String response = echoGateway.echo('Hello');assertThat(response, is('HELLO'));}
到目前为止听起来很简单,使用spring集成的实现将通过转换为大写字母并返回增强后的消息来接受“消息”并对其进行“转换”。
<?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/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd'><channel id='requestChannel'/><gateway id='echoGateway' service-interface='rube.simple.EchoGateway' default-request-channel='requestChannel' /><transformer input-channel='requestChannel' expression='payload.toUpperCase()' /> </beans:beans>
做工精美!
Spring Integration的优点在于,即使Integration场景变得复杂,它呈现给应用程序的外观仍然保持简单,
考虑一个Rube Goldberg集成方案:
首先是描述旋流的图表:
那么它到底是做什么的:
- 它接收到这样的消息:“来自Spring整合的你好”,
- 将其拆分为单个单词(您好,来自spring,integeg),
- 将每个单词发送到ActiveMQ队列,
- 从队列中,单词片段由浓缩器拾取以大写每个单词,
- 将响应放回响应队列,
- 根据单词的原始顺序对其进行重新排序,
- 聚合成一个句子(“ HELLO FROM SPRING INTEG”),
- 返回到应用程序。
这是这种流程的Spring Integration配置的样子:
<?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:int-jms='http://www.springframework.org/schema/integration/jms'xmlns:beans='http://www.springframework.org/schema/beans'xsi:schemaLocation='http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsdhttp://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsdhttp://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms-2.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd'><beans:import resource='broker.xml'/><channel id='requestChannel'><queue/> </channel><channel id='responseChannel'><queue/></channel><gateway id='echoGateway' service-interface='rube.complicated.EchoGateway' default-request-channel='requestChannel' default-reply-channel='responseChannel' default-reply-timeout='5000' /><channel id='toJmsOutbound'/><splitter input-channel='requestChannel' output-channel='toJmsOutbound' expression='payload.split('\s')'></splitter><channel id='sequenceChannel'></channel><int-jms:outbound-gateway request-channel='toJmsOutbound' reply-channel='sequenceChannel' request-destination='amq.outbound' extract-request-payload='true' /><channel id='enhanceMessageChannel'/><channel id='toReplyQueueChannel'/><int-jms:inbound-gateway request-channel='enhanceMessageChannel' request-destination='amq.outbound' reply-channel='toReplyQueueChannel'/><transformer input-channel='enhanceMessageChannel' expression='(payload + '').toUpperCase()' output-channel='toReplyQueueChannel'/><resequencer input-channel='sequenceChannel' output-channel='aggregateChannel' release-partial-sequences='false'></resequencer><aggregator input-channel='aggregateChannel' output-channel='responseChannel' expression='T(com.google.common.base.Joiner).on(' ').join(![payload].toArray())'/><poller id='poller' fixed-delay='500' default='true'/></beans:beans>
这个流程有太多的复杂性(因此Rube Goldberg),但是Spring Integration提供给应用程序的外观仍然非常简单。
@Testpublic void testEcho() throws Exception{String amessage = 'Hello from Spring Integration';String response = echoGateway.echo(amessage);assertThat(response, is('HELLO FROM SPRING INTEGRATION'));}
我认为这是Spring Integration的本质
我在https://github.com/bijukunjummen/rg-si.git上有此代码的github存储库
参考: all和其他博客中的Rube Goldberg Spring Integration,来自我们的JCG合作伙伴 Biju Kunjummen。
翻译自: https://www.javacodegeeks.com/2012/06/rube-goldberg-spring-integration_22.html