Spring Integration 10 分鐘上手

工程 | Mark Fisher | 2009年2月13日 | ...

Spring Integration 1.0 GA 版本在 SpringOne Americas 上於 2 個月前發布,從那時起我就一直想寫一篇新的、最新的「入門」部落格文章。 嗯,年初總是比較忙碌,所以我的目標是提供一個包含 10 個步驟的實作範例。 每一步大約需要一分鐘......除非你停下來思考 ;)。 那麼,廢話不多說,我們開始吧!

步驟 1:下載 Spring Integration 發布套件

你可以在這裡取得最新版本(我寫這篇文章時是 1.0.1):http://www.springsource.com/download/community?project=Spring%20Integration

下載完成後,解壓縮該檔案。 你會看到一個 'dist' 目錄,其中包含組成 Spring Integration 專案的 JAR。 你還會看到一個 'lib' 目錄,其中包含相依性。

步驟 2:建立專案

我將使用 Eclipse 作為範例,但你當然可以在另一個 IDE 中執行此操作。 你也可以使用 Maven 或 Ivy,但這裡的範例非常簡單,只需建立一個目錄並新增 JAR 即可。

建立一個新的「Java 專案」(在「Package Explorer」視圖中,右鍵單擊,然後「New -> Java Project」),並在其中建立一個「lib」目錄。 然後,將 Spring Integration 'dist' 和 'lib' 目錄中的以下 JAR 複製到專案的 'lib' 中。 來自 dist:

  • org.springframework.integration-1.0.1.RELEASE.jar
  • org.springframework.integration-file-1.0.1.RELEASE.jar
來自 lib
  • com.springsource.org.aopalliance-1.0.0.jar
  • com.springsource.org.apache.commons.logging-1.1.1.jar
  • org.springframework.aop-2.5.6.A.jar
  • org.springframework.beans-2.5.6.A.jar
  • org.springframework.context-2.5.6.A.jar
  • org.springframework.core-2.5.6.A.jar
  • org.springframework.transaction-2.5.6.A.jar

在 Eclipse 中重新整理 'lib' 目錄(按 F5)並將這些 JAR 新增到建置路徑(選取 JAR,右鍵單擊,然後選擇「Build Path -> Add to Build Path」)。 最後,在 'src' 目錄中建立一個 'blog' 套件。

步驟 3:開始 Spring 配置

如果你使用 Spring IDESpringSource 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>

我們現在將新增一個元素。 這定義了一個由 Java 記憶體佇列支持的訊息通道,並且一次最多可容納 10 個訊息


<si:channel id="channel">
    <si:queue capacity="10"/>
</si:channel>

步驟 4:定義一個 Bootstrap Java 類別

Spring Integration 元件只是嵌入在任何 Spring 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)。

步驟 5:發送和接收 Spring Integration 訊息

現在,我們可以從上下文中存取通道並發送訊息。 因為我們還沒有任何訂閱者(將在接下來的幾個步驟中介紹),我們也將從同一個通道接收。 這將證明一切都配置正確。 修改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()方法的結果列印到控制台中。

步驟 6:建立服務

Spring Integration 旨在做到非侵入性。 這意味著我們現在將逐步修改範例,以便你沒有任何直接連結到框架的程式碼。 我們要做的第一件事是新增一個 POJO 服務,該服務將成為我們訊息的訂閱者。 這個範例將簡單地將字串轉換為大寫並新增一些驚嘆號


public class Shouter {

    public String shout(String s) {
        return s.toUpperCase().concat("!!!");
    }

}

步驟 7:將服務新增到 Spring 配置

可以將服務作為常規 Bean 新增


<bean id="shouter" class="blog.Shouter"/>

接下來,我們將新增一個「服務激活器」以將服務 Bean 連接到輸入和輸出通道。 我們需要將現有的「通道」重新命名為「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"/>

步驟 8:通過發送訊息來調用服務

修改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);
}

步驟 9:將輸出發送到檔案

我們可以通過新增通道適配器將結果直接發送到檔案,而不是在main()方法中輪詢輸出。 首先,你可以從輸出通道中刪除佇列子元素,因為沒有必要進行輪詢


<si:channel id="output"/>

新增通道適配器。 你可以指定任何現有目錄,並且在 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」的檔案中的輸出(你可以新增一個檔案名產生策略,但這超出了本文的範圍)。

步驟 10:建立閘道介面

最後一步將實現完全非侵入性的目標。 與將訊息發送到訊息通道相比,將字串作為參數發送到一個簡單的介面會更乾淨。 在 'blog' 套件中建立以下介面


public interface Gateway {

    void send(String s);

}

然後,將以下元素新增到你的配置中


<si:gateway id="gateway" service-interface="blog.Gateway" default-request-channel="input"/>

最後,在 main 方法中使用閘道,而不是通道。 現在,你可以簡單地傳遞一個字串


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 提供一個不錯的介紹。 最重要的一點是,你可以輕鬆地建立一個專用的整合層,該整合層使用非侵入性的 Spring 程式設計模型。 當你開始新增多個端點時,真正的價值才會顯現出來。 然後,你可能需要新增過濾器、轉換器和路由器。

Spring Integration 提供的功能遠不止此範例所演示的。 要了解更多資訊,請查看 'org.springframework.integration.samples' 模組中的各種專案(來源可在發布套件的 'src' 目錄中找到),並閱讀 參考文檔。 當然,你可以在 Spring Integration 主頁上找到更多資訊。

取得 Spring 電子報

與 Spring 電子報保持聯繫

訂閱

領先一步

VMware 提供培訓和認證,以加速你的進度。

了解更多

取得支援

Tanzu Spring 在一個簡單的訂閱中提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位檔案。

了解更多

即將舉行的活動

查看 Spring 社群中所有即將舉行的活動。

查看全部