Spring Integration Java DSL示例

现在已经为Spring Integration引入了新的基于Java的DSL ,这使得可以使用基于纯Java的配置而不是基于Spring XML的配置来定义Spring Integration消息流。

我尝试使用DSL来获得示例集成流–我称其为Rube Goldberg流 ,因为它在尝试大写作为输入传递的字符串时遵循复杂的路径。 该流程如下所示,并做了一些疯狂的事情来执行简单的任务:

卢布

  1. 它接受了这样的消息–“春天来了,你好”
  2. 将其拆分为单个词(您好,来自,春天,完整)
  3. 将每个单词发送到ActiveMQ队列
  4. 从队列中,单词片段由浓缩器拾取以大写每个单词
  5. 将响应放回响应队列
  6. 根据单词的原始顺序对其进行拾取,重新排序
  7. 聚合成一个句子(“ HELLO FROM SPRING INTEG”),
  8. 返回到应用程序。

从Spring Integration Java DSL开始,一个简单的基于Xml的配置将大写的String变为:

<channel id="requestChannel"/>

<gateway id="echoGateway" service-interface="rube.simple.EchoGateway" default-request-channel="requestChannel" />

<transformer input-channel="requestChannel" expression="payload.toUpperCase()" />

这里没有什么大不了的事,消息传递网关接收从应用程序传递来的消息,在转换器中将其大写,然后将其返回给应用程序。

在Spring Integration Java DSL中表达这一点:

@Configuration
@EnableIntegration
@IntegrationComponentScan
@ComponentScan
public class EchoFlow {

 @Bean
 public IntegrationFlow simpleEchoFlow() {
  return IntegrationFlows.from("requestChannel")
    .transform((String s) -> s.toUpperCase())
    .get();
 }
}

@MessagingGateway
public interface EchoGateway {
 @Gateway(requestChannel = "requestChannel")
 String echo(String message);
}

请注意,@MessagingGateway批注不是Spring Integration Java DSL的一部分,它是Spring Integration中的现有组件,其作用与基于XML的配置中的网关组件相同。 我喜欢这样的事实,即可以使用类型安全的Java 8 lambda表达式而不是Spring-EL表达式来表示转换。 请注意,转换表达式可以用很少的其他方式进行编码:

??.transform((String s) -> s.toUpperCase())

要么:

??.<String, String>transform(s -> s.toUpperCase())

或使用方法引用:

??.<String, String>transform(String::toUpperCase)

再次从基于XML的配置开始,移至更复杂的Rube Goldberg流以完成相同的任务。 有两种配置来表达此流程:

rube-1.xml:此配置负责步骤1、2、3、6、7、8:

  1. 它接受了这样的消息–“春天来了,你好”
  2. 将其拆分为单个词(您好,来自,春天,完整)
  3. 将每个单词发送到ActiveMQ队列
  4. 从队列中,单词片段由浓缩器拾取以大写每个单词
  5. 将响应放回响应队列
  6. 根据单词的原始顺序对其进行拾取,重新排序
  7. 聚合成一个句子(“ HELLO FROM SPRING INTEG”),
  8. 返回到应用程序。
<channel id="requestChannel"/>

<!--Step 1, 8-->
<gateway id="echoGateway" service-interface="rube.complicated.EchoGateway" default-request-channel="requestChannel"
   default-reply-timeout="5000"/>

<channel id="toJmsOutbound"/>

<!--Step 2-->
<splitter input-channel="requestChannel" output-channel="toJmsOutbound" expression="payload.split('\s')"
    apply-sequence="true"/>

<channel id="sequenceChannel"/>

<!--Step 3-->
<int-jms:outbound-gateway request-channel="toJmsOutbound" reply-channel="sequenceChannel"
        request-destination="amq.outbound" extract-request-payload="true"/>


<!--On the way back from the queue-->
<channel id="aggregateChannel"/>

<!--Step 6-->
<resequencer input-channel="sequenceChannel" output-channel="aggregateChannel" release-partial-sequences="false"/>

<!--Step 7-->
<aggregator input-channel="aggregateChannel"
   expression="T(com.google.common.base.Joiner).on(' ').join(![payload])"/>

和rube-2.xml用于步骤4、5:

  1. 它接受了这样的消息–“春天来了,你好”
  2. 将其拆分为单个词(您好,来自,春天,完整)
  3. 将每个单词发送到ActiveMQ队列
  4. 从队列中,单词片段由浓缩器拾取以大写每个单词
  5. 将响应放回响应队列
  6. 根据单词的原始顺序对其进行拾取,重新排序
  7. 聚合成一个句子(“ HELLO FROM SPRING INTEG”),
  8. 返回到应用程序。
<channel id="enhanceMessageChannel"/>

<int-jms:inbound-gateway request-channel="enhanceMessageChannel" request-destination="amq.outbound"/>

<transformer input-channel="enhanceMessageChannel" expression="(payload + '').toUpperCase()"/>

现在,使用Spring Integration Java DSL表示Rube Goldberg流,配置又分为两部分:

EchoFlowOutbound.java:

@Bean
 public DirectChannel sequenceChannel() {
  return new DirectChannel();
 }

 @Bean
 public DirectChannel requestChannel() {
  return new DirectChannel();
 }

 @Bean
 public IntegrationFlow toOutboundQueueFlow() {
  return IntegrationFlows.from(requestChannel())
    .split(s -> s.applySequence(true).get().getT2().setDelimiters("\\s"))
    .handle(jmsOutboundGateway())
    .get();
 }

 @Bean
 public IntegrationFlow flowOnReturnOfMessage() {
  return IntegrationFlows.from(sequenceChannel())
    .resequence()
    .aggregate(aggregate ->
      aggregate.outputProcessor(g ->
        Joiner.on(" ").join(g.getMessages()
          .stream()
          .map(m -> (String) m.getPayload()).collect(toList())))
      , null)
    .get();
 }

和EchoFlowInbound.java:

@Bean
public JmsMessageDrivenEndpoint jmsInbound() {
 return new JmsMessageDrivenEndpoint(listenerContainer(), messageListener());
}

@Bean
public IntegrationFlow inboundFlow() {
 return IntegrationFlows.from(enhanceMessageChannel())
   .transform((String s) -> s.toUpperCase())
   .get();
}

同样,这里的代码是完全类型安全的,并且在开发时而不是在运行时(如基于XML的配置)检查任何错误。 我再次喜欢这样一个事实,即转换,聚合语句可以使用Java 8 lamda表达式而不是Spring-EL表达式来简洁地表达。

我在这里未显示的是一些支持代码,用于设置activemq测试基础结构 ,该配置继续保留为xml,我已将此代码包含在示例github项目中。

总而言之,我很高兴看到这种使用纯Java来表达Spring Integration消息流的新方法,并且我期待看到它的持续发展,甚至可能尝试以较小的方式参与其发展。

这是github存储库中的整个工作代码:https://github.com/bijukunjummen/rg-si

资源和致谢:

  • Artem Bilan的 Spring Integration Java DSL 简介博客文章 :https://spring.io/blog/2014/05/08/spring-integration-java-dsl-milestone-1-released
  • Spring Integration Java DSL 网站和Wiki :https://github.com/spring-projects/spring-integration-extensions/wiki/Spring-Integration-Java-DSL-Reference。 我无耻地从这个Wiki复制了很多代码! 另外,非常感谢Artem 对我提出的问题的指导
  • Gary Russell在Spring Integration 4.0上的网络研讨会,其中详细介绍了Spring Integration Java DSL。

翻译自: https://www.javacodegeeks.com/2014/06/spring-integration-java-dsl-sample.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值