javascript
Spring集成–第2节–更多世界
第一部分是使用Spring Integration的簡(jiǎn)單Hello World應(yīng)用程序。 我想通過(guò)考慮其他一些方案來(lái)進(jìn)一步介紹它。
因此,對(duì)Hello World應(yīng)用程序的第一個(gè)更改是添加網(wǎng)關(guān)組件。 要快速重新訪問(wèn)較早的測(cè)試程序,請(qǐng)執(zhí)行以下操作:
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);} }在上面突出顯示的行中,測(cè)試依賴于特定于Spring Integration的組件-消息通道,并且在測(cè)試中,構(gòu)造了顯式的Spring Integration Message并將其發(fā)送到消息通道。 與Spring Integration(這里的消息傳遞系統(tǒng))的耦合有點(diǎn)過(guò)多。
網(wǎng)關(guān)組件為消息傳遞系統(tǒng)提供了外觀,從而將用戶應(yīng)用程序(在本例中為單元測(cè)試)與消息傳遞系統(tǒng)的詳細(xì)信息(消息傳遞通道,消息和消息的顯式發(fā)送)隔離開(kāi)來(lái)。
首先通過(guò)一個(gè)示例來(lái)說(shuō)明在使用網(wǎng)關(guān)組件的情況下測(cè)試的外觀:
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接口是網(wǎng)關(guān)組件。 既然已經(jīng)引入了該組件,那么在此測(cè)試中就不再依賴于Spring Integration了-在代碼中根本沒(méi)有提到Message,Message Channel。
網(wǎng)關(guān)組件也是這樣定義的非常簡(jiǎn)單的Java接口:
package org.bk.si.helloworld.hw2;public interface Greeter {public void sendGreeting(String message); }所以現(xiàn)在的問(wèn)題是,誰(shuí)來(lái)負(fù)責(zé)創(chuàng)建消息傳遞并將消息發(fā)送到消息通道–是通過(guò)Spring Integration配置進(jìn)行的:
<?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界面創(chuàng)建了Gateway組件,在后臺(tái)創(chuàng)建了一個(gè)代理,該代理處理了之前明確進(jìn)行的所有操作-創(chuàng)建消息傳遞并將消息發(fā)送到消息通道。
現(xiàn)在為“ Hello World”示例增加更多的復(fù)雜性:
考慮以下測(cè)試:
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));} }這與先前的單元測(cè)試相同,除了在這種情況下,“ Hello World”消息被發(fā)送了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>如果我現(xiàn)在運(yùn)行此測(cè)試,則輸出如下:
紅色的行打印到syserr,黑色的行打印到sysout。
因此,問(wèn)題在于為什么其中一些將進(jìn)入sysout,而另一些將進(jìn)入syserr,為什么不同時(shí)使用呢?
答案是因?yàn)橥ǖ赖念愋?上面的“ messagesChannel”是Spring Integration術(shù)語(yǔ)中的“直接通道”,并且具有“點(diǎn)對(duì)點(diǎn)”語(yǔ)義。 點(diǎn)對(duì)點(diǎn)語(yǔ)義基本上意味著當(dāng)一條消息進(jìn)入Messaging Channel時(shí),只有1個(gè)接收者接收到該消息–因此,在這種情況下,標(biāo)準(zhǔn)輸出適配器或標(biāo)準(zhǔn)err適配器最終都會(huì)打印進(jìn)入該消息的消息。消息通道。
因此,要打印到兩個(gè)適配器,解決方法是簡(jiǎn)單地更改通道的語(yǔ)義–代替點(diǎn)對(duì)點(diǎn)通道,將其設(shè)置為發(fā)布-訂閱通道,該通道是向多個(gè)接收者廣播消息的通道。 使用Spring Integration進(jìn)行更改非常簡(jiǎn)單:
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>現(xiàn)在的輸出將是同時(shí)打印到sysout和syserr的消息
這樣就完成了對(duì)網(wǎng)關(guān)組件,直接通道和發(fā)布訂閱通道的介紹。
參考資料: Spring Integration –第2節(jié)–來(lái)自all和各式博客的JCG合作伙伴 Biju Kunjummen的更多Hello Worlds 。
翻譯自: https://www.javacodegeeks.com/2012/07/spring-integration-session-2-more-hello.html
總結(jié)
以上是生活随笔為你收集整理的Spring集成–第2节–更多世界的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 终极JPA查询和技巧列表–第1部分
- 下一篇: 使用@OrderBy对Spring Da