Spring Integration Java DSL示例–使用Jms名称空间工厂进一步简化
在較早的博客文章中,我談到了虛擬盧布·戈德堡流程,該流程通過一系列復(fù)雜的步驟將字符串變成大寫,本文的前提是引入Spring Integration Java DSL,作為通過xml配置文件定義集成流程的替代方法。
感謝Artem Bilan ,在寫完博客條目后,我學(xué)到了一些新東西,并希望在此處記錄這些學(xué)習(xí)內(nèi)容:
因此,首先是我的原始樣本,在這里,我有以下流程(粗體顯示):
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; }事實(shí)證明,根據(jù)Artem Bilan的反饋,這里可以優(yōu)化一些事情。
首先,請(qǐng)注意我是如何明確定義兩個(gè)直接通道的:“ requestChannel”(用于啟動(dòng)接收字符串消息的流)和“ sequenceChannel”(用于在消息從jms消息隊(duì)列返回后處理消息),實(shí)際上可以完全刪除并這種方式使流程更加簡(jiǎn)潔:
@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; }現(xiàn)在僅通過聲明其名稱即可隱式創(chuàng)建“ requestChannel”。 序列頻道更有趣,引用了Artem Bilan –
不要為AbstractReplyProducingMessageHandler指定outputChannel并依賴DSL
這意味著jmsOutboundGateway是一個(gè)AbstractReplyProducingMessageHandler,其答復(fù)通道是由DSL隱式派生的。 進(jìn)一步地,兩種方法被合而為一,這兩種方法較早地處理了將消息發(fā)送到隊(duì)列然后在消息返回時(shí)繼續(xù)進(jìn)行的流程。 恕我直言,由于這一變化,它的確讀得更好。
第二個(gè)不錯(cuò)的變化是本文介紹的主題是Jms命名空間工廠,當(dāng)我寫上一篇博客文章時(shí),DSL支持定義AMQ入站/出站適配器/網(wǎng)關(guān),現(xiàn)在支持基于Jms的入站/ adapter適配器/網(wǎng)關(guān),這進(jìn)一步簡(jiǎn)化了流程,流程現(xiàn)在看起來像這樣:
@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部分還簡(jiǎn)化為以下內(nèi)容:
@Bean public IntegrationFlow inboundFlow() {return IntegrationFlows.from(Jms.inboundGateway(connectionFactory).destination("amq.outbound")).transform((String s) -> s.toUpperCase()).get(); }因此,總而言之,Spring Integration Java DSL是一種簡(jiǎn)潔配置Spring Integration流的令人興奮的新方法。 如何簡(jiǎn)化流的可讀性已經(jīng)令人印象深刻,Jms名稱空間工廠的引入使基于JMS的流更進(jìn)一步。
- 我已經(jīng)用本文列出的更改更新了示例應(yīng)用程序– https://github.com/bijukunjummen/rg-si。
翻譯自: https://www.javacodegeeks.com/2014/07/spring-integration-java-dsl-sample-further-simplification-with-jms-namespace-factories.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Spring Integration Java DSL示例–使用Jms名称空间工厂进一步简化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 铁和水蒸气反应的化学方程式 铁和水蒸气反
- 下一篇: 针对新手的Java EE7和Maven项