Spring Integration Java DSL示例
現在已經為Spring Integration引入了新的基于Java的DSL ,這使得可以使用基于純Java的配置而不是基于Spring XML的配置來定義Spring Integration消息流。
我嘗試使用DSL來獲得示例集成流–我稱其為Rube Goldberg流 ,因為它在嘗試大寫作為輸入傳遞的字符串時遵循復雜的路徑。 該流程如下所示,并做了一些瘋狂的事情來執行簡單的任務:
從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 {@Beanpublic 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:
和rube-2.xml用于步驟4、5:
現在,使用Spring Integration Java DSL表示Rube Goldberg流,配置又分為兩部分:
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();}和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
總結
以上是生活随笔為你收集整理的Spring Integration Java DSL示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在Spring启动时与mongodb一起
- 下一篇: Java的编年史和低延迟