http://wangleifire.iteye.com/blog/351749

  • 介绍
    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.(分开程度要取决业务吧。)
    • Extension points should be abstract in nature but within well-defined boundaries to promote reuse and portability.(美妙现实世界产品)
  • 源码下载打开它的网页,http://www.springsource.org/spring-integration
    主页上也没有东东,但有个下源代码的地方,svn开工啦。

    Java代码
    1. svn co https://src.springframework.org/svn/spring-integration/trunk  springintegration
    1. 下载完后,进入build-spring-integration目录执行ant.完成后,导入到Eclipse中。
      导入项目会有很多,先添加时会有报错。这里需要添加一个变量。
      IVY_CACHE=<checkout-dir>/ivy-cache/repository

      这里要注意的事,也是我遇到问题。执行ant时,他会去下载lvy,如果你本身在%ANT_HOME%\lib里有lvy.jar包,由于我暂时找不到如何处理,我就直接将Ant中的jar删除掉后就没有问题。
      另外在ant过程中,测试步骤可能会在file模块中出现问题,可以将相关test类中代码注释掉。

    2. HelloWorld源码分析在samples项目中,打开helloworld包里面有三个文件。
      Java代码
      1. package org.springframework.integration.samples.helloworld;
      2. /**
      3. * @author Mark Fisher
      4. */
      5. public class HelloService {
      6. public String sayHello(String name) {
      7. return "Hello " + name;
      8. }
      9. }

    helloworldDemo.xml

    Xml代码
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans:beans xmlns="http://www.springframework.org/schema/integration"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:beans="http://www.springframework.org/schema/beans"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans
    6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    7. http://www.springframework.org/schema/integration
    8. http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
    9. <channel id="inputChannel"/>
    10. <channel id="outputChannel">
    11. <queue capacity="10"/>
    12. </channel>
    13. <service-activator input-channel="inputChannel"
    14. output-channel="outputChannel"
    15. ref="helloService"
    16. method="sayHello"/>
    17. <beans:bean id="helloService" class="org.springframework.integration.samples.helloworld.HelloService"/>
    18. </beans:beans>

    HelloWorldDemo.java

    Java代码
    1. package org.springframework.integration.samples.helloworld;
    2. import org.springframework.context.support.AbstractApplicationContext;
    3. import org.springframework.context.support.ClassPathXmlApplicationContext;
    4. import org.springframework.integration.channel.BeanFactoryChannelResolver;
    5. import org.springframework.integration.channel.ChannelResolver;
    6. import org.springframework.integration.channel.PollableChannel;
    7. import org.springframework.integration.core.MessageChannel;
    8. import org.springframework.integration.message.StringMessage;
    9. /**
    10. * Demonstrates a basic message endpoint.
    11. *
    12. * @author Mark Fisher
    13. */
    14. public class HelloWorldDemo {
    15. public static void main(String[] args) {
    16. AbstractApplicationContext context = new ClassPathXmlApplicationContext("helloWorldDemo.xml", HelloWorldDemo.class);
    17. ChannelResolver channelResolver = new BeanFactoryChannelResolver(context);
    18. MessageChannel inputChannel = channelResolver.resolveChannelName("inputChannel");
    19. PollableChannel outputChannel = (PollableChannel) channelResolver.resolveChannelName("outputChannel");
    20. inputChannel.send(new StringMessage("World"));
    21. System.out.println(outputChannel.receive(0).getPayload());
    22. context.stop();
    23. }
    24. }

    Cafe源码分析 Cafe示例描述的是星巴克的订单处理故事。
    其示例描述在:http://www.enterpriseintegrationpatterns.com/ramblings/18_starbucks.html
    这里简单描述一下,以免大家看英文太累
    文章讲在星巴克喝咖啡时,收银员可能只有一个,而冲咖啡员工会有多个,如何让收银员产生订单异步发送给冲咖啡员工。并且冲咖啡员工可能是竞争上岗的,就当他们是计件工吧。
    这里要考虑问题:
    1,冲咖啡员工使用不同设备,不同咖啡冲调时间可能不同。 
    2,冲咖啡员工可能会将相同类型的咖啡同时一起冲调。
    星巴克如何处理这个问题?
    就当他解决了这个问题,它是如何把每个咖啡又送回给每个客户呢?当然,星巴克采用“标识关系模式”,将每个咖啡杯上标上名称,并通过叫喊方式。
    但并不是每天都是美好的,总有出错的时候。例如,收银员无法支付?冲调一杯你不喜欢的咖啡,你要换一杯?冲咖啡的设备坏了,星巴克要退你钱...这些异常情况如何处理。
    因此就会有以下三种方式异常处理:
    1,关闭交易,什么都不做。
    2,重做,重新发起行为。
    3,修正行为,相当于退钱这种行为。
    因此,这里这篇文章后面讨论一下两阶段提交为什么不适合星巴克,如果你让收银员、冲咖啡员工,买单的人需要在一个“事务”中,交易所有完成后,再进行下一个业务。估计星巴克会马上倒闭啦。因此星巴克采用“Conversation pattern”模式。

    好啦,业务了解清楚,我们再来看一下完整XML文件。在这里我没有采用示例详细的xml方式,而没有采用annotation方式。

    Xml代码
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans:beans xmlns="http://www.springframework.org/schema/integration"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:beans="http://www.springframework.org/schema/beans"
    5. xmlns:stream="http://www.springframework.org/schema/integration/stream"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    8. http://www.springframework.org/schema/integration
    9. http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
    10. http://www.springframework.org/schema/integration/stream
    11. http://www.springframework.org/schema/integration/stream/spring-integration-stream-1.0.xsd">
    12. <gateway id="cafe" service-interface="org.springframework.integration.samples.cafe.Cafe"/>
    13. <channel id="orders"/>
    14. <splitter input-channel="orders" ref="orderSplitter" method="split" output-channel="drinks"/>
    15. <channel id="drinks"/>
    16. <router input-channel="drinks" ref="drinkRouter" method="resolveOrderItemChannel"/>
    17. <channel id="coldDrinks">
    18. <queue capacity="10"/>
    19. </channel>
    20. <service-activator input-channel="coldDrinks" ref="barista"
    21. method="prepareColdDrink" output-channel="preparedDrinks"/>
    22. <channel id="hotDrinks">
    23. <queue capacity="10"/>
    24. </channel>
    25. <service-activator input-channel="hotDrinks" ref="barista"
    26. method="prepareHotDrink" output-channel="preparedDrinks"/>
    27. <channel id="preparedDrinks"/>
    28. <aggregator input-channel="preparedDrinks" ref="waiter"
    29. method="prepareDelivery" output-channel="deliveries"/>
    30. <stream:stdout-channel-adapter id="deliveries"/>
    31. <beans:bean id="orderSplitter"
    32. class="org.springframework.integration.samples.cafe.xml.OrderSplitter"/>
    33. <beans:bean id="drinkRouter"
    34. class="org.springframework.integration.samples.cafe.xml.DrinkRouter"/>
    35. <beans:bean id="barista" class="org.springframework.integration.samples.cafe.xml.Barista"/>
    36. <beans:bean id="waiter" class="org.springframework.integration.samples.cafe.xml.Waiter"/>
    37. <poller id="poller" default="true">
    38. <interval-trigger interval="1000"/>
    39. </poller>
    40. </beans:beans>

    以下是参考文档中的示例描述图:

    CafeDemo代码创建了订单。这家咖啡店有两种饮料,一种是热的,一种是冷的,消息将这订单包装到一个"orders"的channel(频道)。一个endpoint侦听到订单频道并根据订单情况进行分开处理。

    完成分开处理后,程序交给DrinksRouter经过drink频道。而DrinkRouter一个职责就是将订单内容中的热咖啡和冷咖啡交给不同的channel处理。

    Xml代码
    1. <gateway id="cafe" service-interface="org.springframework.integration.samples.cafe.Cafe"/>

    这里Gateway主要是根据接口生成代理类。

    Java代码
    1. Cafe cafe = (Cafe) context.getBean("cafe");
    2. DrinkOrder order = new DrinkOrder();
    3. Drink hotDoubleLatte = new Drink(DrinkType.LATTE, 2, false);
    4. Drink icedTripleMocha = new Drink(DrinkType.MOCHA, 3, true);
    5. order.addDrink(hotDoubleLatte);
    6. order.addDrink(icedTripleMocha);
    7. for (int i = 0; i < 100; i++) {
    8. cafe.placeOrder(order);
    9. }
    Java代码
    1. @MessageEndpoint(input="orders", output="drinks")
    2. public class OrderSplitter {
    3. @Splitter
    4. public List<Drink> split(Message<DrinkOrder> orderMessage) {
    5. return orderMessage.getPayload().getDrinks();
    6. }
    7. }
    Java代码
    1. @MessageEndpoint(input="drinks")
    2. public class DrinkRouter {
    3. @Router
    4. public String resolveDrinkChannel(Drink drink) {
    5. return (drink.isIced()) ? "coldDrinks" : "hotDrinks";
    6. }
    7. }
    Xml代码
    1. <handler-endpoint handler="coldBarista" input-channel="coldDrinks"
    2. method="prepareColdDrink">
    3. </handler-endpoint>
    4. <handler-endpoint handler="hotBarista" input-channel="hotDrinks"
    5. method="prepareHotDrink">
    6. </handler-endpoint>
    Java代码
    1. public void prepareColdDrink(Message<Drink> drinkMessage) {
    2. Drink drink = drinkMessage.getPayload();
    3. //no changes to the rest of the code
    4. }

ESB学习笔记(Spring Integration实战)相关推荐

  1. [学习笔记]Spring Cloud实战至投降(一)

    开始作战 微服务 Spring Cloud 微服务 首先要说明的就是微服务这个概念.微服务所指的是一种架构风格理念.而我所理解的当前的软件架构主要分为单体架构.微服务架构.而更细的分法是1: 单一应用 ...

  2. Spring学习笔记--spring+mybatis集成

    前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...

  3. jboss esb 学习笔记与html的传输,Jboss_ESB学习笔记1.doc

    Jboss_ESB学习笔记1 JBoss ESB学习笔记1--搭建ESB开发环境 最近由于工作需要用到了JBoss ESB,可在网上这方面的资料实在少的让人不敢恭维,而JBoss ESB的官方文档对初 ...

  4. PCA(主成分分析-principal components analysis)学习笔记以及源代码实战讲解

    PCA(主成分分析-principal components analysis)学习笔记以及源代码实战讲解 文章目录 PCA(主成分分析-principal components analysis)学 ...

  5. SpringCloud学习笔记002---Spring Cloud实战微服务准备

    =============================================================  开始使用Spring Cloud实战微服务 =============== ...

  6. Java学习笔记_15 项目实战之天天酷跑(二):开始游戏界面

    前文,我们完成了登录界面的搭建.本文将完成开始游戏界面的搭建,并建立起登录界面与开始游戏界面的桥梁. 实现在输对用户名和密码后即可进入开始游戏界面的功能. 界面功能需求图: 具体要求: 当鼠标移入开始 ...

  7. Java学习笔记_16 项目实战之天天酷跑(三):缓冲加载游戏界面

    前文,我们完成了开始游戏界面的搭建.本文将实现缓冲加载界面的搭建.并搭建与前面俩界面间的桥梁. 实现输入正确用户名密码后,进入开始游戏界面,点击开始游戏按钮后,进入缓冲加载界面的功能. 界面示意图: ...

  8. Java学习笔记_17 项目实战之天天酷跑(四):游戏主界面

    接上文,本文将实现游戏主界面,功能如下: 移动的背景图片.动态的玩家.玩家的移动功能. 五种障碍物持续出现.玩家和障碍物的碰撞. 暂停.继续功能. 首先,看一下整体效果: 动图实在太大,几秒钟的 Gi ...

  9. 学习笔记(01):SpringBoot实战教程:SpringBoot企业级线上商城项目讲解-Spring Boot 之基础 web 功能开发[一]...

    立即学习:https://edu.csdn.net/course/play/26258/328832?utm_source=blogtoedu DispatcherServletAutoConfigu ...

最新文章

  1. 独家 | 13大技能助你成为超级数据科学家!(附链接)
  2. 90 后利用平台漏洞薅羊毛,获利 45 万被抓捕!网友们却争论不休……
  3. 中呜机器人编程视频教程_清华北大泄露Python436集视频教程,这就是你现在需要的...
  4. 解决 invalid input detected at ‘^’ marker的问题
  5. HDU6428-Calculate-数论函数
  6. vue :key的说明 看到这文章,解决你的疑问
  7. NLP(三) 预处理
  8. 轻松学习分布式|系列2|负载均衡算法。
  9. python while函数_Python:无法在while循环中调用函数
  10. Matlab聚类分析相关函数
  11. matlab跟踪控制程序,机器人轨迹跟踪控制方法研究(含MATLAB程序)
  12. 在Window10系统中安装Pandas并行加速库Modin的流程及效果测试
  13. 国王学院计算机科学,伦敦大学国王学院计算机科学本科.pdf
  14. 一度智信:拼多多怎么投诉商家
  15. 行列式的两种计算方法
  16. 六级考研单词之路-十二
  17. Acrel-2000电力监控系统在生物诊断试剂浙江迪安诊断的应用
  18. 哔哩哔哩助手edge或chrome插件安装使用教程
  19. 赛迪智库:2017年智能技术将呈现八大发展趋势
  20. 计算机常用软件考试试题,计算机常用工具软件试卷试题.docx

热门文章

  1. 核心概念——节点/边/Combo——内置节点——Triangle
  2. 2016年第七届蓝桥杯C/C++ B组国赛 —— 第一题:一步之遥
  3. ubuntu中安装wmware-tools
  4. 【STM32】STM32系列教程汇总(暂时暂停更新...)
  5. 【机器视觉】 dev_set_part算子
  6. 【STM32】通用定时器(TIM2到TIM5)
  7. 树莓派 QT 编程下的硬件中断
  8. csapp学习笔记2021.1.9
  9. Spark(5)——standalone模式
  10. git pull冲突解决