日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring集成–第2节–更多世界

發布時間:2023/12/3 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring集成–第2节–更多世界 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這是Spring Integration Session 1的后續活動

第一部分是使用Spring Integration的簡單Hello World應用程序。 我想通過考慮其他一些方案來進一步介紹它。

因此,對Hello World應用程序的第一個更改是添加網關組件。 要快速重新訪問較早的測試程序,請執行以下操作:

package org.bk.si.helloworld.hw1;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; import org.springframework.integration.message.GenericMessage; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@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);} }

在上面突出顯示的行中,測試依賴于特定于Spring Integration的組件-消息通道,并且在測試中,構造了顯式的Spring Integration Message并將其發送到消息通道。 與Spring Integration(這里的消息傳遞系統)的耦合有點過多。

網關組件為消息傳遞系統提供了外觀,從而將用戶應用程序(在本例中為單元測試)與消息傳遞系統的詳細信息(消息傳遞通道,消息和消息的顯式發送)隔離開來。

首先通過一個示例來說明在使用網關組件的情況下測試的外觀:

package org.bk.si.helloworld.hw2;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("helloworld.xml") public class HelloWorldTest {@Autowired Greeter greeter;@Testpublic void testHelloWorld(){this.greeter.sendGreeting("Hello World");} }

上面的Greeter接口是網關組件。 既然已經引入了該組件,那么在此測試中就不再依賴于Spring Integration了-在代碼中根本沒有提到Message,Message Channel。

網關組件也是這樣定義的非常簡單的Java接口:

package org.bk.si.helloworld.hw2;public interface Greeter {public void sendGreeting(String message); }

所以現在的問題是,誰來負責創建消息傳遞并將消息發送到消息通道–是通過Spring Integration配置進行的:

<?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="messagesChannel"></int:channel><int:gateway service-interface="org.bk.si.helloworld.hw2.Greeter" default-request-channel="messagesChannel"></int:gateway><int-stream:stdout-channel-adapter channel="messagesChannel" append-newline="true"/></beans>

上面突出顯示的行從Greeter界面創建了Gateway組件,在后臺創建了一個代理,該代理處理了之前明確進行的所有操作-創建消息傳遞并將消息發送到消息通道。

現在為“ Hello World”示例增加更多的復雜性:

考慮以下測試:

package org.bk.si.helloworld.hw3;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("helloworld.xml") public class HelloWorldTest {@Autowired Greeter greeter;@Testpublic void testHelloWorld(){System.out.println("Started..");long start = System.nanoTime();for (int i=0;i<10;i++){this.greeter.sendMessage(String.format("Hello World %d",i));}System.out.println("Completed..");System.out.println(String.format("Took %f ms", (System.nanoTime()-start)/10e6));} }

這與先前的單元測試相同,除了在這種情況下,“ Hello World”消息被發送了10次。 支持的Spring Integration配置文件如下:

<?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:publish-subscribe-channel id="messagesChannel"/><int:gateway service-interface="org.bk.si.helloworld.hw3.Greeter" default-request-channel="messagesChannel"></int:gateway><int-stream:stderr-channel-adapter channel="messagesChannel" append-newline="true"/><int-stream:stdout-channel-adapter channel="messagesChannel" append-newline="true"/></beans>

如果我現在運行此測試,則輸出如下:

紅色的行打印到syserr,黑色的行打印到sysout。

因此,問題在于為什么其中一些將進入sysout,而另一些將進入syserr,為什么不同時使用呢?

答案是因為通道的類型-上面的“ messagesChannel”是Spring Integration術語中的“直接通道”,并且具有“點對點”語義。 點對點語義基本上意味著當一條消息進入Messaging Channel時,只有1個接收者接收到該消息–因此,在這種情況下,標準輸出適配器或標準err適配器最終都會打印進入該消息的消息。消息通道。

因此,要打印到兩個適配器,解決方法是簡單地更改通道的語義–代替點對點通道,將其設置為發布-訂閱通道,該通道是向多個接收者廣播消息的通道。 使用Spring Integration進行更改非常簡單:

file:/C:/learn/scratch/target/test-classes/org/bk/htmlencode/content.txt <?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:publish-subscribe-channel id="messagesChannel"/><int:gateway service-interface="org.bk.si.helloworld.hw3.Greeter" default-request-channel="messagesChannel"></int:gateway><int-stream:stderr-channel-adapter channel="messagesChannel" append-newline="true"/><int-stream:stdout-channel-adapter channel="messagesChannel" append-newline="true"/></beans>

現在的輸出將是同時打印到sysout和syserr的消息

這樣就完成了對網關組件,直接通道和發布訂閱通道的介紹。

參考資料: Spring Integration –第2節–來自all和各式博客的JCG合作伙伴 Biju Kunjummen的更多Hello Worlds 。


翻譯自: https://www.javacodegeeks.com/2012/07/spring-integration-session-2-more-hello.html

總結

以上是生活随笔為你收集整理的Spring集成–第2节–更多世界的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。