在進入程序本身之前,快速回顧一下消息傳遞概念將很有用–消息傳遞是一種集成樣式,其中兩個獨立的應用程序通過中介相互通信–中介被稱為“消息傳遞系統”。
企業集成模式描述了基于消息的應用程序集成中常見的與集成相關的問題及其推薦的解決方案。
例如。 考慮企業集成模式之一– 消息通道 ,引用《 企業集成模式》一書 :
“消息傳遞頻道”正在嘗試解決的問題是:
企業具有兩個需要進行通信的獨立應用程序,最好使用消息傳遞進行通信。
一個應用程序如何通過消息傳遞與另一應用程序通信?
解決方案是:
使用消息通道連接應用程序,其中一個應用程序將信息寫入該通道,而另一個應用程序從該通道讀取該信息。
所有其他企業集成模式均以相同的方式描述。
快速訪問Enterprise Integration Patterns的原因是要設置上下文– Spring Integration與Enterprise Integration Patterns非常緊密地結合在一起,并且是前面提到的“消息系統”。
現在來看使用Spring Integration的Hello World:
首先是一個小的junit:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("helloworld.xml")
public class HelloWorldTest {@Autowired@Qualifier("messageChannel")MessageChannel messageChannel;@Testpublic void testHelloWorld() {Message<String> helloWorld = new GenericMessage<String>("Hello World");messageChannel.send(helloWorld);}
}
在這里,一個MessageChannel被連接到測試中,第一個應用程序(這里是Junit),向Message Channel發送一條Message(在這種情況下為字符串“ Hello World”),然后從“ Message Channel”中讀取消息并寫入將消息發送給系統。
現在,讓我們看一下“某物”如何從消息通道中提取消息并將其寫到系統的其余部分:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:int="http://www.springframework.org/schema/integration"xmlns:int-stream="http://www.springframework.org/schema/integration/stream"xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsdhttp://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream-2.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><int:channel id="messageChannel"></int:channel><int-stream:stdout-channel-adapter channel="messageChannel" append-newline="true"/></beans>
上面是使用Spring Custom名稱空間(這里是Integration命名空間)描述的Spring Integration流。 創建了一個“消息通道”,即想象中的“消息通道”,將“ Hello World”“消息”放入“消息通道”,“通道適配器”從中獲取消息并將其打印到標準輸出中。
這是一個小程序,但是它使用了三種企業集成模式- 消息 (“ Hello World”,它是發送到消息傳遞系統的信息包,是先前介紹的“ 消息通道 ”,而新的是消息傳遞)。 Channel Adapter ,這里是一個出站通道適配器,用于將消息傳遞系統連接到應用程序(在本例中為系統輸出),進一步顯示了Spring Integration如何與帶有其Spring自定義名稱空間的Enterprise Integration Patterns術語保持緊密的一致。
這個簡單的程序介紹了Spring Integration,在接下來的幾節課中,我將使用更多示例來更詳細地介紹Spring Integration。
參考文獻:
1. Spring Integration參考: http : //static.springsource.org/spring-integration/reference/htmlsingle/
2.企業集成模式: http : //www.eaipatterns.com/index.html 3. EIP的Visio模板: http : //www.eaipatterns.com/downloads.html
參考: all和其他博客中的JCG合作伙伴 Biju Kunjummen提供的Spring,Spring Integration,Enterprise Development 。
翻譯自: https://www.javacodegeeks.com/2012/07/spring-integration-session-1-hello.html