乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      ESB學(xué)習(xí)筆記(Spring Integration實戰(zhàn))

       gyb98 2011-03-16
      ESB學(xué)習(xí)筆記(Spring Integration實戰(zhàn))
       

      2009-11-23 來源:網(wǎng)絡(luò)

       
    • 介紹 
      Spring Integration是Spring公司的一套ESB框架。 
      前面ESB介紹中我也做了一定了解。我們來看一下它主要做什么的。 
      Spring Integration is motivated by the following goals:
      • Provide a simple model for implementing complex enterprise integration solutions.(暫時相信它吧,誰讓它搞個Spring框架,的確給人方便一把。)
      • Facilitate asynchronous, message-driven behavior within a Spring-based application.(這個不談,Spring框架就是它玩的。再說這一點與它競爭只有Mule啦。)
      • Promote intuitive, incremental adoption for existing Spring users. (也暫時相信它,別人都只說給用戶提升。)
      Spring Integration is guided by the following principles:
      • Components should be loosely coupled for modularity and testability.(松耦合,好像很早很早就聽說過。像做夢一樣)
      • The framework should enforce separation of concerns between business logic and integration logic.(分開程度要取決業(yè)務(wù)吧。)
      • Extension points should be abstract in nature but within well-defined boundaries to promote reuse and portability.(美妙現(xiàn)實世界產(chǎn)品)
    • 源碼下載打開它的網(wǎng)頁,http://www./spring-integration 
      主頁上也沒有東東,但有個下源代碼的地方,svn開工啦。 
       
      Java代碼 復(fù)制代碼
      1. svn co https://src./svn/spring-integration/trunk  springintegration   
      svn co https://src./svn/spring-integration/trunk  springintegration 
      
      下載完后,進(jìn)入build-spring-integration目錄執(zhí)行ant.完成后,導(dǎo)入到Eclipse中。 
      導(dǎo)入項目會有很多,先添加時會有報錯。這里需要添加一個變量。 
      IVY_CACHE=<checkout-dir>/ivy-cache/repository 

      這里要注意的事,也是我遇到問題。執(zhí)行ant時,他會去下載lvy,如果你本身在%ANT_HOME%\lib里有l(wèi)vy.jar包,由于我暫時找不到如何處理,我就直接將Ant中的jar刪除掉后就沒有問題。 
      另外在ant過程中,測試步驟可能會在file模塊中出現(xiàn)問題,可以將相關(guān)test類中代碼注釋掉。 

       
    • HelloWorld源碼分析在samples項目中,打開helloworld包里面有三個文件。 
       
      Java代碼 復(fù)制代碼
      1. package org.springframework.integration.samples.helloworld;   
      2.   
      3. /**  
      4.  * @author Mark Fisher  
      5.  */  
      6. public class HelloService {   
      7.   
      8.     public String sayHello(String name) {   
      9.         return "Hello " + name;   
      10.     }   
      11.   
      12. }  
      package org.springframework.integration.samples.helloworld;
      
      /**
       * @author Mark Fisher
       */
      public class HelloService {
      
      	public String sayHello(String name) {
      		return "Hello " + name;
      	}
      
      }
      

      helloworldDemo.xml 
       
      Xml代碼 復(fù)制代碼
      1. <?xml version="1.0" encoding="UTF-8"?>  
      2. <beans:beans xmlns="http://www./schema/integration"  
      3.     xmlns:xsi="http://www./2001/XMLSchema-instance"  
      4.     xmlns:beans="http://www./schema/beans"  
      5.     xsi:schemaLocation="http://www./schema/beans   
      6.             http://www./schema/beans/spring-beans-2.5.xsd   
      7.             http://www./schema/integration   
      8.             http://www./schema/integration/spring-integration-1.0.xsd">  
      9.   
      10.     <channel id="inputChannel"/>  
      11.   
      12.     <channel id="outputChannel">  
      13.         <queue capacity="10"/>  
      14.     </channel>  
      15.   
      16.     <service-activator input-channel="inputChannel"  
      17.                        output-channel="outputChannel"  
      18.                        ref="helloService"  
      19.                        method="sayHello"/>  
      20.   
      21.     <beans:bean id="helloService" class="org.springframework.integration.samples.helloworld.HelloService"/>  
      22. </beans:beans>  
      <?xml version="1.0" encoding="UTF-8"?>
      <beans:beans xmlns="http://www./schema/integration"
      	xmlns:xsi="http://www./2001/XMLSchema-instance"
      	xmlns:beans="http://www./schema/beans"
      	xsi:schemaLocation="http://www./schema/beans
      			http://www./schema/beans/spring-beans-2.5.xsd
      			http://www./schema/integration
      			http://www./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代碼 復(fù)制代碼
      1. package org.springframework.integration.samples.helloworld;   
      2.   
      3. import org.springframework.context.support.AbstractApplicationContext;   
      4. import org.springframework.context.support.ClassPathXmlApplicationContext;   
      5. import org.springframework.integration.channel.BeanFactoryChannelResolver;   
      6. import org.springframework.integration.channel.ChannelResolver;   
      7. import org.springframework.integration.channel.PollableChannel;   
      8. import org.springframework.integration.core.MessageChannel;   
      9. import org.springframework.integration.message.StringMessage;   
      10.   
      11. /**  
      12.  * Demonstrates a basic message endpoint.   
      13.  *   
      14.  * @author Mark Fisher  
      15.  */  
      16. public class HelloWorldDemo {   
      17.   
      18.     public static void main(String[] args) {   
      19.         AbstractApplicationContext context = new ClassPathXmlApplicationContext("helloWorldDemo.xml", HelloWorldDemo.class);   
      20.         ChannelResolver channelResolver = new BeanFactoryChannelResolver(context);   
      21.         MessageChannel inputChannel = channelResolver.resolveChannelName("inputChannel");   
      22.         PollableChannel outputChannel = (PollableChannel) channelResolver.resolveChannelName("outputChannel");   
      23.         inputChannel.send(new StringMessage("World"));   
      24.         System.out.println(outputChannel.receive(0).getPayload());   
      25.         context.stop();   
      26.     }   
      27.   
      28. }  
      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./ramblings/18_starbucks.html 
      這里簡單描述一下,以免大家看英文太累 
      文章講在星巴克喝咖啡時,收銀員可能只有一個,而沖咖啡員工會有多個,如何讓收銀員產(chǎn)生訂單異步發(fā)送給沖咖啡員工。并且沖咖啡員工可能是競爭上崗的,就當(dāng)他們是計件工吧。 
      這里要考慮問題: 
      1,沖咖啡員工使用不同設(shè)備,不同咖啡沖調(diào)時間可能不同?!?nbsp;
      2,沖咖啡員工可能會將相同類型的咖啡同時一起沖調(diào)。 
      星巴克如何處理這個問題? 
      就當(dāng)他解決了這個問題,它是如何把每個咖啡又送回給每個客戶呢?當(dāng)然,星巴克采用“標(biāo)識關(guān)系模式”,將每個咖啡杯上標(biāo)上名稱,并通過叫喊方式。 
      但并不是每天都是美好的,總有出錯的時候。例如,收銀員無法支付?沖調(diào)一杯你不喜歡的咖啡,你要換一杯?沖咖啡的設(shè)備壞了,星巴克要退你錢...這些異常情況如何處理。 
      因此就會有以下三種方式異常處理: 
      1,關(guān)閉交易,什么都不做。 
      2,重做,重新發(fā)起行為。 
      3,修正行為,相當(dāng)于退錢這種行為。 
      因此,這里這篇文章后面討論一下兩階段提交為什么不適合星巴克,如果你讓收銀員、沖咖啡員工,買單的人需要在一個“事務(wù)”中,交易所有完成后,再進(jìn)行下一個業(yè)務(wù)。估計星巴克會馬上倒閉啦。因此星巴克采用“Conversation pattern”模式。 

      好啦,業(yè)務(wù)了解清楚,我們再來看一下完整XML文件。在這里我沒有采用示例詳細(xì)的xml方式,而沒有采用annotation方式。 
       
      以下是參考文檔中的示例描述圖: 

       

      CafeDemo代碼創(chuàng)建了訂單。這家咖啡店有兩種飲料,一種是熱的,一種是冷的,消息將這訂單包裝到一個"orders"的channel(頻道)。一個endpoint偵聽到訂單頻道并根據(jù)訂單情況進(jìn)行分開處理。 

      完成分開處理后,程序交給DrinksRouter經(jīng)過drink頻道。而DrinkRouter一個職責(zé)就是將訂單內(nèi)容中的熱咖啡和冷咖啡交給不同的channel處理。 

       
      Xml代碼 復(fù)制代碼
      1. <gateway id="cafe" service-interface="org.springframework.integration.samples.cafe.Cafe"/>  
      	<gateway id="cafe" service-interface="org.springframework.integration.samples.cafe.Cafe"/>
      

      這里Gateway主要是根據(jù)接口生成代理類。 

       
      Java代碼 復(fù)制代碼
      1. Cafe cafe = (Cafe) context.getBean("cafe");   
      2.                 DrinkOrder order = new DrinkOrder();   
      3.                 Drink hotDoubleLatte = new Drink(DrinkType.LATTE, 2false);   
      4.                 Drink icedTripleMocha = new Drink(DrinkType.MOCHA, 3true);   
      5.                 order.addDrink(hotDoubleLatte);   
      6.                 order.addDrink(icedTripleMocha);   
      7.                 for (int i = 0; i < 100; i++) {   
      8.                         cafe.placeOrder(order);   
      9.                 }  
      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代碼 復(fù)制代碼
      1. @MessageEndpoint(input="orders", output="drinks")   
      2. public class OrderSplitter {   
      3.   
      4.         @Splitter  
      5.         public List<Drink> split(Message<DrinkOrder> orderMessage) {   
      6.                 return orderMessage.getPayload().getDrinks();   
      7.         }   
      8.   
      9. }  
      @MessageEndpoint(input="orders", output="drinks")
      public class OrderSplitter {
      
              @Splitter
              public List<Drink> split(Message<DrinkOrder> orderMessage) {
                      return orderMessage.getPayload().getDrinks();
              }
      
      }
      
       
      Java代碼 復(fù)制代碼
      1. @MessageEndpoint(input="drinks")   
      2. public class DrinkRouter {   
      3.   
      4.         @Router  
      5.         public String resolveDrinkChannel(Drink drink) {   
      6.                 return (drink.isIced()) ? "coldDrinks" : "hotDrinks";   
      7.         }   
      8.   
      9. }  
      @MessageEndpoint(input="drinks")
      public class DrinkRouter {
      
              @Router
              public String resolveDrinkChannel(Drink drink) {
                      return (drink.isIced()) ? "coldDrinks" : "hotDrinks";
              }
      
      }
      
      Xml代碼 復(fù)制代碼
      1. <handler-endpoint handler="coldBarista" input-channel="coldDrinks"  
      2.         method="prepareColdDrink">  
      3. </handler-endpoint>  
      4.   
      5. <handler-endpoint handler="hotBarista" input-channel="hotDrinks"  
      6.         method="prepareHotDrink">  
      7. </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代碼 復(fù)制代碼
      1. public void prepareColdDrink(Message<Drink> drinkMessage) {   
      2.                 Drink drink = drinkMessage.getPayload();   
      3.                 //no changes to the rest of the code   
      4.         }  
      • 本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多