10分鐘掌握Spring Integration

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

Spring Integration 1.0 GA 版本在兩個月前於SpringOne Americas 釋出,從那時起我就一直想寫一篇最新的“入門”部落格文章。嗯,年初總是非常忙碌,所以我的目標是提供一個包含10個步驟的動手示例。每個步驟大約需要一分鐘……除非你停下來思考;)。那麼,事不宜遲,我們開始吧!

步驟1:下載Spring Integration發行版

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

下載完成後,解壓縮檔案。您將看到一個包含Spring Integration專案JAR檔案的“dist”目錄。您還將看到一個包含依賴項的“lib”目錄。

步驟2:建立一個專案

我將在示例中使用Eclipse,但當然您也可以在其他IDE中完成。您也可以使用Maven或Ivy,但這裡的示例非常簡單,只需建立一個目錄並新增JAR檔案就足夠了。

建立一個新的“Java Project”(在“Package Explorer”檢視中,右鍵單擊,然後選擇“New -> Java Project”),並在其中建立一個“lib”目錄。然後,將以下JAR檔案從Spring Integration的“dist”和“lib”目錄複製到專案的“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)“organize imports”。)

步驟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"/>

接下來,我們將新增一個“Service Activator”來將服務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"/>

步驟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:建立一個Gateway介面

最後一步將實現完全非侵入性的目標。與其將訊息傳送到訊息通道,不如將字串作為引數傳遞給一個簡單介面要乾淨得多。在“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的良好介紹。要點是,您可以輕鬆建立一個使用非侵入性Spring程式設計模型的專用整合層。當您開始新增多個端點時,真正的價值就會顯現出來。那時,您可能需要新增過濾器、轉換器和路由器。

Spring Integration提供的功能遠不止本示例所演示的。要了解更多資訊,請檢視“org.springframework.integration.samples”模組中的各種專案(原始碼在發行版的“src”目錄中可用),並閱讀參考文件。當然,您可以在Spring Integration主頁上找到更多資訊。

獲取 Spring 新聞通訊

透過 Spring 新聞通訊保持聯絡

訂閱

領先一步

VMware 提供培訓和認證,助您加速進步。

瞭解更多

獲得支援

Tanzu Spring 提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位制檔案,只需一份簡單的訂閱。

瞭解更多

即將舉行的活動

檢視 Spring 社群所有即將舉行的活動。

檢視所有