上下文設置
在第一篇文章中,我們創建了一個簡單的Java應用程序,其中
- 通過頻道發送了一條消息,
- 它被服務(即POJO)攔截并進行了修改。
- 然后通過另一個渠道發送
- 從通道讀取并顯示修改后的消息。
但是,在執行此操作時(請記住,我們只是在此處介紹概念),我們在應用程序中編寫了一些特定于Spring的代碼,即測試類。 在本文中,我們將解決這個問題,并使我們的應用程序代碼與Spring Integration api盡可能隔離。
這是通過Spring Integration稱為gateways來完成的 。 存在網關的唯一目的是將消息傳遞相關的“管道”代碼從“業務”代碼中抽象出來。 業務邏輯可能實際上并不在乎是通過通道發送消息還是通過進行SOAP調用來實現功能。 到目前為止,這種抽象雖然合乎邏輯且合乎需要,但并非十分實用。
此時可能值得快速瀏覽一下《 Spring Integration參考手冊》 。 但是,如果您剛剛開始使用Spring Integration,那么暫時最好閱讀本文。 我建議您先洗手,然后再返回參考手冊,該手冊非常好,但也非常詳盡,因此對于初學者來說可能不勝枚舉。
網關可以是帶有批注的POJO(這很方便,但在我看來卻超出了整個目的)或具有XML配置(如果未經檢查,它很快會變成任何體面大小的應用程序的噩夢)。 歸根結底,這確實是您的選擇,但是我喜歡走XML路線。 兩種樣式的配置選項在參考實現的本節中詳細介紹。
Spring與網關的集成
因此,讓我們為HelloWorld服務創建另一個帶有網關拋出的測試(有關更多上下文,請參閱本系列的第一篇文章 )。 讓我們從測試的Spring配置開始。
文件:src / test / resources / org / academy / integration / HelloWorld1Test-context.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:p='http://www.springframework.org/schema/p'xmlns:int='http://www.springframework.org/schema/integration'xsi:schemaLocation='http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-2.1.xsd'><int:channel id='inputChannel'></int:channel><int:channel id='outputChannel'><int:queue capacity='10' /></int:channel><int:service-activator input-channel='inputChannel'output-channel='outputChannel' ref='helloService' method='greet' /><bean id='helloService' class='org.academy.integration.HelloWorld' /><int:gateway service-interface='org.academy.integration.Greetings'default-request-channel='inputChannel' default-reply-channel='outputChannel'></int:gateway></beans>
在這種情況下,所有不同之處在于我們添加了一個網關。 這是一個稱為org.academy.integration.Greetings的接口。 它與“ inputChannel”和“ outputChannel”交互,分別發送和讀取消息。 讓我們編寫界面。
文件:/src/main/java/org/academy/integration/Greetings.java
package org.academy.integration;public interface Greetings {public void send(String message);public String receive();}
然后,我們添加此接口的實現。 等待。 沒有實現。 而且我們不需要任何實現。 Spring使用一種名為GatewayProxyFactoryBean的東西向該網關注入一些基本代碼,從而使它可以讀取基于字符串的簡單消息,而無需我們做任何事情。 那就對了。 沒事
注–假設您不是在使用Spring Integration框架只是推敲字符串, 就需要為大多數生產方案添加更多代碼 。 因此,不要習慣免費午餐。 但是,盡管它在這里,讓我們深入研究。
現在,讓我們使用網關編寫一個新的測試類(并且完全不與通道和消息進行交互)。
文件:/src/test/java/org/academy/integration/HelloWorld1Test.java
package org.academy.integration;import static org.junit.Assert.*;import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class HelloWorld1Test {private final static Logger logger = LoggerFactory.getLogger(HelloWorld1Test.class);@AutowiredGreetings greetings;@Testpublic void test() {greetings.send('World');assertEquals(greetings.receive(), 'Hello World');logger.debug('Spring Integration with gateways.');}}
現在我們的測試班要干凈得多。 它根本不了解頻道,消息或與Spring Integration相關的任何信息。 它只知道一個問候實例(通過.send()方法向其提供一些數據),并通過.receive()方法獲取修改后的數據。 因此, 業務邏輯忽略了管道邏輯 ,從而使代碼更加簡潔。
現在,只需鍵入“ mvn -e clean install”(或使用m2e插件),您就應該能夠運行單元測試并確認給定的字符串“ World”,HelloWorld服務的確在整個通道安排中確實返回了“ Hello World”和消息。
同樣,可選但我強烈建議您運行“ mvn -e全新安裝站點”。 假設您已正確配置了一些代碼覆蓋率工具(在我的情況下為cobertura),將為您提供一個不錯HTML報告,其中顯示了代碼覆蓋率。 在這種情況下,它將是100%。 我已經寫了一系列關于代碼質量的文章 ,詳細介紹了該主題,但是總而言之,確保我使用和推薦使用的任何編碼實踐/框架都符合一些基本的代碼質量標準對我來說非常重要。 。 能夠進行單元測試和測量是我所做的這樣一項基本檢查。 毋庸置疑,一般來說,Spring(包括Spring集成)會通過帶有鮮艷色彩的檢查。
結論
本文就是這樣。 快樂的編碼。
建議進一步閱讀...
以下是本系列早期文章的鏈接:
- Hello World with Spring 3 MVC
- 使用Spring 3 MVC處理表單
- 使用Spring 3進行單元測試和記錄
- 使用Spring 3 MVC處理表單驗證
- 引入Spring集成
這些是我可以推薦的出色材料:
- Spring Integration入門
- Spring Integration的示例代碼
- Spring集成–第1節– Hello World
- Spring集成–第2節–更多世界
參考: Tech for Enterprise博客上我們JCG合作伙伴 Partho的Spring與Gatways的集成 。
翻譯自: https://www.javacodegeeks.com/2012/08/spring-integration-with-gateways.html