先人一步
VMware 提供培訓和認證,助您加速進步。
瞭解更多Spring Integration 1.0 GA 版本於兩個月前在 SpringOne Americas 釋出。自那以後,我一直想寫一篇新的、最新的“入門”部落格文章。嗯,年初總是很忙,所以我的目標是提供一個包含10個步驟的實操示例。每個步驟大約需要一分鐘……除非你停下來思考;)。那麼,話不多說,開始吧!
您可以從這裡獲取最新版本(撰寫本文時的版本為 1.0.1):http://www.springsource.com/download/community?project=Spring%20Integration
下載完成後,解壓檔案。您將看到一個 'dist' 目錄,其中包含構成 Spring Integration 專案的 JAR 檔案。您還將看到一個 'lib' 目錄,其中包含依賴項。
我將使用 Eclipse 作為示例,當然您也可以在其他 IDE 中完成。您也可以使用 Maven 或 Ivy,但這裡的示例非常簡單,只需建立一個目錄並新增 JAR 檔案即可。
建立一個新的“Java Project”(在“Package Explorer”檢視中,右鍵單擊,然後選擇“New -> Java Project”),並在其中建立一個“lib”目錄。然後,將 Spring Integration 的“dist”和“lib”目錄中的以下 JAR 檔案複製到專案中的“lib”目錄。來自 dist:
在 Eclipse 中重新整理“lib”目錄(按 F5),並將這些 JAR 新增到構建路徑(選擇 JAR 檔案,右鍵單擊,然後選擇“Build Path -> Add to Build Path”)。最後,在“src”目錄中建立一個“blog”包。
如果您正在使用 Spring IDE 或 SpringSource Tool Suite,您可以新增 Spring 專案特性,然後右鍵單擊“blog”包來建立新的 bean 定義檔案。否則,只需建立以下內容並將其命名為“config.xml”即可
<?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:si="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
</beans>
現在我們將新增一個單獨的元素。這定義了一個訊息通道(Message Channel),它由 Java 記憶體佇列支援,並且一次最多可以容納 10 條訊息(Message)。
<si:channel id="channel">
<si:queue capacity="10"/>
</si:channel>
Spring Integration 元件可以簡單地嵌入到任何 Spring ApplicationContext 中。因此,我們所需要做的就是基於此配置檔案建立一個 ApplicationContext。如果在 web 應用程式中執行,您可以使用 Spring 的 ContextLoaderListener 進行引導;如果您在 dm Server 中執行,它也會為您處理此事。對於這個簡單的示例,我們只需在一個名為 Bootstrap 的類中(位於“blog”包中)建立一個 main() 方法
public class Bootstrap {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("blog/config.xml");
}
}
(在任何時候,若要在 Eclipse 中快速解決匯入問題,您可以使用 Ctrl+Shift+O 進行“組織匯入”...或在 Mac 上使用 Command+Shift+O)。
現在,我們可以從上下文中訪問通道併發送一條訊息。由於我們還沒有任何訂閱者(將在接下來的幾個步驟中新增),我們也將從同一個通道接收。這將證明一切都已正確配置。修改 main() 方法,使其如下所示
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("blog/config.xml");
PollableChannel channel = (PollableChannel) context.getBean("channel");
channel.send(new StringMessage("Spring Integration rocks"));
Message<?> reply = channel.receive();
System.out.println("received: " + reply);
}
當您執行它時,您應該會看到該訊息的 toString() 方法的結果輸出到控制檯。
Spring Integration 的目標是無侵入性。這意味著我們現在將逐步修改示例,以便您的程式碼與框架之間沒有直接耦合。然而,我們首先要做的就是新增一個 POJO Service,它將成為我們訊息的訂閱者。這個示例將簡單地將字串轉換為大寫並新增一些感嘆號
public class Shouter {
public String shout(String s) {
return s.toUpperCase().concat("!!!");
}
}
該 Service 可以作為一個普通的 bean 新增
<bean id="shouter" class="blog.Shouter"/>
接下來,我們將新增一個“Service Activator”來將 Service bean 連線到輸入和輸出通道。我們需要將現有的“channel”重新命名為“output”,然後為輸入新增一個簡單的非緩衝通道。完整的配置應如下所示
<si:channel id="input"/>
<si:channel id="output">
<si:queue capacity="10"/>
</si:channel>
<si:service-activator input-channel="input"
output-channel="output"
ref="shouter"
method="shout"/>
<bean id="shouter" class="blog.Shouter"/>
修改 main() 方法,使其向輸入通道傳送訊息並從輸出通道接收。請注意,我們也在修改依賴查詢,並且通道被轉換為不同的型別(“input”通道是非緩衝的,因此不像輸出通道那樣是 PollableChannel)
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("blog/config.xml");
MessageChannel input = (MessageChannel) context.getBean("input");
PollableChannel output = (PollableChannel) context.getBean("output");
input.send(new StringMessage("Spring Integration rocks"));
Message<?> reply = output.receive();
System.out.println("received: " + reply);
}
與在 main() 方法中輪詢輸出不同,我們可以透過新增 Channel Adapter 將結果直接傳送到檔案。首先,您可以從輸出通道中刪除 queue 子元素,因為不再需要輪詢
<si:channel id="output"/>
新增 Channel Adapter。您可以指定任何現有目錄,在 Windows 上您應該包含驅動器磁碟機代號(例如 "C:/tmp")
<file:outbound-channel-adapter channel="output" directory="/tmp"/>
接下來,您可以更新 Bootstrap 的 main() 方法,使其僅傳送
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("blog/config.xml");
MessageChannel input = (MessageChannel) context.getBean("input");
input.send(new StringMessage("Spring Integration rocks"));
}
您還需要將 'file' 名稱空間新增到 XSD 配置中。頂層 'beans' 元素應如下所示
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si="http://www.springframework.org/schema/integration"
xmlns:file="http://www.springframework.org/schema/integration/file"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
http://www.springframework.org/schema/integration/file
http://www.springframework.org/schema/integration/file/spring-integration-file-1.0.xsd">
執行示例,您應該會在一個副檔名為 “.msg” 的檔案中看到輸出(您可以新增一個檔名生成策略,但這超出了本文的範圍)。
最後一步將實現完全無侵入性的目標。與將訊息傳送到訊息通道不同,將字串作為引數傳送到一個簡單的介面會更加簡潔。在“blog”包中建立以下介面
public interface Gateway {
void send(String s);
}
然後,將以下元素新增到您的配置中
<si:gateway id="gateway" service-interface="blog.Gateway" default-request-channel="input"/>
最後,在 main 方法中使用 Gateway 而不是通道。現在,您只需傳遞一個字串
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("blog/config.xml");
Gateway gateway = (Gateway) context.getBean("gateway");
gateway.send("Spring Integration rocks");
}
希望這能為您提供一個不錯的 Spring Integration 入門介紹。主要 takeaway 是您可以輕鬆建立一個使用無侵入性 Spring 程式設計模型的專用整合層。當您開始新增多個端點時,其真正的價值就顯而易見了。那時,您可能需要新增過濾器、轉換器和路由器。
Spring Integration 提供的功能遠不止本示例所展示的。要了解更多資訊,請檢視“org.springframework.integration.samples”模組中的各種專案(原始碼位於分發包的“src”目錄中),並閱讀 參考文件。當然,您可以在 Spring Integration 主頁上找到更多資訊。