免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
ESB學(xué)習(xí)筆記(Spring Integration實(shí)戰(zhàn))
2009-11-23 來(lái)源:網(wǎng)絡(luò)
介紹
Spring Integration是Spring公司的一套ESB框架。
前面ESB介紹中我也做了一定了解。我們來(lái)看一下它主要做什么的。
Spring Integration is motivated by the following goals:Provide a simple model for implementing complex enterprise integration solutions.(暫時(shí)相信它吧,誰(shuí)讓它搞個(gè)Spring框架,的確給人方便一把。)
Facilitate asynchronous, message-driven behavior within a Spring-based application.(這個(gè)不談,Spring框架就是它玩的。再說(shuō)這一點(diǎn)與它競(jìng)爭(zhēng)只有Mule啦。)
Promote intuitive, incremental adoption for existing Spring users. (也暫時(shí)相信它,別人都只說(shuō)給用戶(hù)提升。)
Spring Integration is guided by the following principles:Components should be loosely coupled for modularity and testability.(松耦合,好像很早很早就聽(tīng)說(shuō)過(guò)。像做夢(mèng)一樣)
The framework should enforce separation of concerns between business logic and integration logic.(分開(kāi)程度要取決業(yè)務(wù)吧。)
Extension points should be abstract in nature but within well-defined boundaries to promote reuse and portability.(美妙現(xiàn)實(shí)世界產(chǎn)品)
源碼下載打開(kāi)它的網(wǎng)頁(yè),http://www.springsource.org/spring-integration
主頁(yè)上也沒(méi)有東東,但有個(gè)下源代碼的地方,svn開(kāi)工啦。
Java代碼 
svn co https://src.springframework.org/svn/spring-integration/trunk  springintegration
svn co https://src.springframework.org/svn/spring-integration/trunk springintegration 下載完后,進(jìn)入build-spring-integration目錄執(zhí)行ant.完成后,導(dǎo)入到Eclipse中。
導(dǎo)入項(xiàng)目會(huì)有很多,先添加時(shí)會(huì)有報(bào)錯(cuò)。這里需要添加一個(gè)變量。
IVY_CACHE=<checkout-dir>/ivy-cache/repository
這里要注意的事,也是我遇到問(wèn)題。執(zhí)行ant時(shí),他會(huì)去下載lvy,如果你本身在%ANT_HOME%\lib里有l(wèi)vy.jar包,由于我暫時(shí)找不到如何處理,我就直接將Ant中的jar刪除掉后就沒(méi)有問(wèn)題。
另外在ant過(guò)程中,測(cè)試步驟可能會(huì)在file模塊中出現(xiàn)問(wèn)題,可以將相關(guān)test類(lèi)中代碼注釋掉。
HelloWorld源碼分析在samples項(xiàng)目中,打開(kāi)helloworld包里面有三個(gè)文件。
Java代碼 
package org.springframework.integration.samples.helloworld;
/**
* @author Mark Fisher
*/
public class HelloService {
public String sayHello(String name) {
return "Hello " + name;
}
}
package org.springframework.integration.samples.helloworld;/** * @author Mark Fisher */public class HelloService { public String sayHello(String name) { return "Hello " + name; }}
helloworldDemo.xml
Xml代碼 
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<channel id="inputChannel"/>
<channel id="outputChannel">
<queue capacity="10"/>
</channel>
<service-activator input-channel="inputChannel"
output-channel="outputChannel"
ref="helloService"
method="sayHello"/>
<beans:bean id="helloService" class="org.springframework.integration.samples.helloworld.HelloService"/>
</beans:beans>
<?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns="http://www.springframework.org/schema/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-1.0.xsd"> <channel id="inputChannel"/> <channel id="outputChannel"> <queue capacity="10"/> </channel> <service-activator input-channel="inputChannel" output-channel="outputChannel" ref="helloService" method="sayHello"/> <beans:bean id="helloService" class="org.springframework.integration.samples.helloworld.HelloService"/></beans:beans>
HelloWorldDemo.java
Java代碼 
package org.springframework.integration.samples.helloworld;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.BeanFactoryChannelResolver;
import org.springframework.integration.channel.ChannelResolver;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.message.StringMessage;
/**
* Demonstrates a basic message endpoint.
*
* @author Mark Fisher
*/
public class HelloWorldDemo {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("helloWorldDemo.xml", HelloWorldDemo.class);
ChannelResolver channelResolver = new BeanFactoryChannelResolver(context);
MessageChannel inputChannel = channelResolver.resolveChannelName("inputChannel");
PollableChannel outputChannel = (PollableChannel) channelResolver.resolveChannelName("outputChannel");
inputChannel.send(new StringMessage("World"));
System.out.println(outputChannel.receive(0).getPayload());
context.stop();
}
}
package org.springframework.integration.samples.helloworld;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.integration.channel.BeanFactoryChannelResolver;import org.springframework.integration.channel.ChannelResolver;import org.springframework.integration.channel.PollableChannel;import org.springframework.integration.core.MessageChannel;import org.springframework.integration.message.StringMessage;/** * Demonstrates a basic message endpoint. * * @author Mark Fisher */public class HelloWorldDemo { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("helloWorldDemo.xml", HelloWorldDemo.class); ChannelResolver channelResolver = new BeanFactoryChannelResolver(context); MessageChannel inputChannel = channelResolver.resolveChannelName("inputChannel"); PollableChannel outputChannel = (PollableChannel) channelResolver.resolveChannelName("outputChannel"); inputChannel.send(new StringMessage("World")); System.out.println(outputChannel.receive(0).getPayload()); context.stop(); }}
Cafe源碼分析 Cafe示例描述的是星巴克的訂單處理故事。
其示例描述在:http://www.enterpriseintegrationpatterns.com/ramblings/18_starbucks.html
這里簡(jiǎn)單描述一下,以免大家看英文太累
文章講在星巴克喝咖啡時(shí),收銀員可能只有一個(gè),而沖咖啡員工會(huì)有多個(gè),如何讓收銀員產(chǎn)生訂單異步發(fā)送給沖咖啡員工。并且沖咖啡員工可能是競(jìng)爭(zhēng)上崗的,就當(dāng)他們是計(jì)件工吧。
這里要考慮問(wèn)題:
1,沖咖啡員工使用不同設(shè)備,不同咖啡沖調(diào)時(shí)間可能不同。
2,沖咖啡員工可能會(huì)將相同類(lèi)型的咖啡同時(shí)一起沖調(diào)。
星巴克如何處理這個(gè)問(wèn)題?
就當(dāng)他解決了這個(gè)問(wèn)題,它是如何把每個(gè)咖啡又送回給每個(gè)客戶(hù)呢?當(dāng)然,星巴克采用“標(biāo)識(shí)關(guān)系模式”,將每個(gè)咖啡杯上標(biāo)上名稱(chēng),并通過(guò)叫喊方式。
但并不是每天都是美好的,總有出錯(cuò)的時(shí)候。例如,收銀員無(wú)法支付?沖調(diào)一杯你不喜歡的咖啡,你要換一杯?沖咖啡的設(shè)備壞了,星巴克要退你錢(qián)...這些異常情況如何處理。
因此就會(huì)有以下三種方式異常處理:
1,關(guān)閉交易,什么都不做。
2,重做,重新發(fā)起行為。
3,修正行為,相當(dāng)于退錢(qián)這種行為。
因此,這里這篇文章后面討論一下兩階段提交為什么不適合星巴克,如果你讓收銀員、沖咖啡員工,買(mǎi)單的人需要在一個(gè)“事務(wù)”中,交易所有完成后,再進(jìn)行下一個(gè)業(yè)務(wù)。估計(jì)星巴克會(huì)馬上倒閉啦。因此星巴克采用“Conversation pattern”模式。
好啦,業(yè)務(wù)了解清楚,我們?cè)賮?lái)看一下完整XML文件。在這里我沒(méi)有采用示例詳細(xì)的xml方式,而沒(méi)有采用annotation方式。
以下是參考文檔中的示例描述圖:
CafeDemo代碼創(chuàng)建了訂單。這家咖啡店有兩種飲料,一種是熱的,一種是冷的,消息將這訂單包裝到一個(gè)"orders"的channel(頻道)。一個(gè)endpoint偵聽(tīng)到訂單頻道并根據(jù)訂單情況進(jìn)行分開(kāi)處理。
完成分開(kāi)處理后,程序交給DrinksRouter經(jīng)過(guò)drink頻道。而DrinkRouter一個(gè)職責(zé)就是將訂單內(nèi)容中的熱咖啡和冷咖啡交給不同的channel處理。
Xml代碼 
<gateway id="cafe" service-interface="org.springframework.integration.samples.cafe.Cafe"/>
<gateway id="cafe" service-interface="org.springframework.integration.samples.cafe.Cafe"/>
這里Gateway主要是根據(jù)接口生成代理類(lèi)。
Java代碼 
Cafe cafe = (Cafe) context.getBean("cafe");
DrinkOrder order = new DrinkOrder();
Drink hotDoubleLatte = new Drink(DrinkType.LATTE, 2, false);
Drink icedTripleMocha = new Drink(DrinkType.MOCHA, 3, true);
order.addDrink(hotDoubleLatte);
order.addDrink(icedTripleMocha);
for (int i = 0; i < 100; i++) {
cafe.placeOrder(order);
}
Cafe cafe = (Cafe) context.getBean("cafe"); DrinkOrder order = new DrinkOrder(); Drink hotDoubleLatte = new Drink(DrinkType.LATTE, 2, false); Drink icedTripleMocha = new Drink(DrinkType.MOCHA, 3, true); order.addDrink(hotDoubleLatte); order.addDrink(icedTripleMocha); for (int i = 0; i < 100; i++) { cafe.placeOrder(order); } Java代碼 
@MessageEndpoint(input="orders", output="drinks")
public class OrderSplitter {
@Splitter
public List<Drink> split(Message<DrinkOrder> orderMessage) {
return orderMessage.getPayload().getDrinks();
}
}
@MessageEndpoint(input="orders", output="drinks")public class OrderSplitter { @Splitter public List<Drink> split(Message<DrinkOrder> orderMessage) { return orderMessage.getPayload().getDrinks(); }} Java代碼 
@MessageEndpoint(input="drinks")
public class DrinkRouter {
@Router
public String resolveDrinkChannel(Drink drink) {
return (drink.isIced()) ? "coldDrinks" : "hotDrinks";
}
}
@MessageEndpoint(input="drinks")public class DrinkRouter { @Router public String resolveDrinkChannel(Drink drink) { return (drink.isIced()) ? "coldDrinks" : "hotDrinks"; }}Xml代碼 
<handler-endpoint handler="coldBarista" input-channel="coldDrinks"
method="prepareColdDrink">
</handler-endpoint>
<handler-endpoint handler="hotBarista" input-channel="hotDrinks"
method="prepareHotDrink">
</handler-endpoint>
<handler-endpoint handler="coldBarista" input-channel="coldDrinks" method="prepareColdDrink"></handler-endpoint><handler-endpoint handler="hotBarista" input-channel="hotDrinks" method="prepareHotDrink"></handler-endpoint> Java代碼 
public void prepareColdDrink(Message<Drink> drinkMessage) {
Drink drink = drinkMessage.getPayload();
//no changes to the rest of the code
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Spring Integration
Spring Integration 1.0 正式發(fā)布
Spring Integration 系統(tǒng)集成
RabbitMQ教程
springboot啟動(dòng)報(bào)錯(cuò),Error starting ApplicationContext. To display the conditions report re-run your appli
spring 技術(shù)內(nèi)幕
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服