在较早的博客文章中,我谈到了虚拟卢布·戈德堡流程,该流程通过一系列复杂的步骤将字符串变成大写,本文的前提是引入Spring Integration Java DSL,作为通过xml配置文件定义集成流程的替代方法。
感谢Artem Bilan ,在写完博客条目后,我学到了一些新东西,并希望在此处记录这些学习内容:
因此,首先是我的原始样本,在这里,我有以下流程(粗体显示):
- 接受此类消息-“春天来了,你好”
- 将其拆分为单个词(您好,来自,春天,完整)
- 将每个单词发送到ActiveMQ队列
- 从队列中拾取单词片段并大写每个单词
- 将响应放回响应队列
- 提取消息,根据单词的原始顺序重新排序
- 聚合成一个句子(“ HELLO FROM SPRING INTEG”),然后
- 将句子返回给调用应用程序。
EchoFlowOutbound.java:
@Beanpublic DirectChannel sequenceChannel() {return new DirectChannel();}@Beanpublic DirectChannel requestChannel() {return new DirectChannel();}@Beanpublic IntegrationFlow toOutboundQueueFlow() {return IntegrationFlows.from(requestChannel()).split(s -> s.applySequence(true).get().getT2().setDelimiters("\\s")).handle(jmsOutboundGateway()).get();}@Beanpublic 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();}@Bean
public JmsOutboundGateway jmsOutboundGateway() {JmsOutboundGateway jmsOutboundGateway = new JmsOutboundGateway();jmsOutboundGateway.setConnectionFactory(this.connectionFactory);jmsOutboundGateway.setRequestDestinationName("amq.outbound");jmsOutboundGateway.setReplyChannel(sequenceChannel());return jmsOutboundGateway;
}
事实证明,根据Artem Bilan的反馈,这里可以优化一些事情。
首先,请注意我是如何明确定义两个直接通道的:“ requestChannel”(用于启动接收字符串消息的流)和“ sequenceChannel”(用于在消息从jms消息队列返回后处理消息),实际上可以完全删除并这种方式使流程更加简洁:
@Bean
public IntegrationFlow toOutboundQueueFlow() {return IntegrationFlows.from("requestChannel").split(s -> s.applySequence(true).get().getT2().setDelimiters("\\s")).handle(jmsOutboundGateway()).resequence().aggregate(aggregate ->aggregate.outputProcessor(g ->Joiner.on(" ").join(g.getMessages().stream().map(m -> (String) m.getPayload()).collect(toList()))), null).get();
}@Bean
public JmsOutboundGateway jmsOutboundGateway() {JmsOutboundGateway jmsOutboundGateway = new JmsOutboundGateway();jmsOutboundGateway.setConnectionFactory(this.connectionFactory);jmsOutboundGateway.setRequestDestinationName("amq.outbound");return jmsOutboundGateway;
}
现在仅通过声明其名称即可隐式创建“ requestChannel”。 序列频道更有趣,引用了Artem Bilan –
不要为AbstractReplyProducingMessageHandler指定outputChannel并依赖DSL
这意味着jmsOutboundGateway是一个AbstractReplyProducingMessageHandler,其答复通道是由DSL隐式派生的。 进一步地,两种方法被合而为一,这两种方法较早地处理了将消息发送到队列然后在消息返回时继续进行的流程。 恕我直言,由于这一变化,它的确读得更好。
第二个不错的变化是本文介绍的主题是Jms命名空间工厂,当我写上一篇博客文章时,DSL支持定义AMQ入站/出站适配器/网关,现在支持基于Jms的入站/ adapter适配器/网关,这进一步简化了流程,流程现在看起来像这样:
@Bean
public IntegrationFlow toOutboundQueueFlow() {return IntegrationFlows.from("requestChannel").split(s -> s.applySequence(true).get().getT2().setDelimiters("\\s")).handle(Jms.outboundGateway(connectionFactory).requestDestination("amq.outbound")).resequence().aggregate(aggregate ->aggregate.outputProcessor(g ->Joiner.on(" ").join(g.getMessages().stream().map(m -> (String) m.getPayload()).collect(toList()))), null).get();
}
该流的入站Jms部分还简化为以下内容:
@Bean
public IntegrationFlow inboundFlow() {return IntegrationFlows.from(Jms.inboundGateway(connectionFactory).destination("amq.outbound")).transform((String s) -> s.toUpperCase()).get();
}
因此,总而言之,Spring Integration Java DSL是一种简洁配置Spring Integration流的令人兴奋的新方法。 如何简化流的可读性已经令人印象深刻,Jms名称空间工厂的引入使基于JMS的流更进一步。
- 我已经用本文列出的更改更新了示例应用程序– https://github.com/bijukunjummen/rg-si。
翻译自: https://www.javacodegeeks.com/2014/07/spring-integration-java-dsl-sample-further-simplification-with-jms-namespace-factories.html