Spring集成–第2节–更多世界
第一部分是使用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节–更多世界相关推荐
- Spring集成–第1节– Hello World
Spring Integration的" Hello World " –考虑一个简单的程序,以使用Spring Integration将" Hello World&quo ...
- 引入Spring集成
在本文中,我们介绍Spring Integration . 如果您以前没有使用过Spring Integration,那么可能会帮助您复习Gregor Hohpe的Enterprise Integra ...
- apache camel_轻量级的开源集成:Apache Camel还是Spring集成?
apache camel 首先,为全面披露信息,在过去的1.5年中, 我一直担任 FuseSource(现为Red Hat) 的顾问,为零售,运输,银行/金融等不同行业的大型和小型公司提供SOA和集成 ...
- Spring集成和Web服务
本文是我们名为" Spring Integration for EAI "的学院课程的一部分. 在本课程中,向您介绍了企业应用程序集成模式以及Spring Integration如 ...
- 轻量级的开源集成:Apache Camel还是Spring集成?
首先,为全面披露信息,在过去的1.5年中, 我一直担任 FuseSource(现为Red Hat) 的顾问,为零售,运输,银行/金融等不同行业的大型和小型公司提供SOA和集成项目支持.我的专长是使用该 ...
- Spring集成–强大的拆分器聚合器
坚固是什么意思? 在本文的上下文中,健壮性是指在不立即返回到调用者的情况下管理流中的异常条件的能力. 在某些处理方案中, n个 m个回答足以做出结论. 通常具有这些趋势的示例处理场景是: 财务,保险和 ...
- spring集成mq_使用Spring Integration Java DSL与Rabbit MQ集成
spring集成mq 我最近参加了在拉斯维加斯举行的2016年Spring大会 ,很幸运地看到了我在软件世界中长期敬佩的一些人. 我亲自遇到了其中的两个人,他们实际上合并了几年前我与Spring In ...
- Spring集成基础知识
本文是我们名为" EAI的Spring集成 "的学院课程的一部分. 在本课程中,向您介绍了企业应用程序集成模式以及Spring Integration如何解决它们. 接下来,您将深 ...
- mule esb 集成_集成框架比较– Spring集成,Mule ESB或Apache Camel
mule esb 集成 公司之间的数据交换增加了很多. 必须集成的应用程序数量也增加了. 这些接口使用不同的技术,协议和数据格式. 然而,这些应用程序的集成应以标准化的方式建模,有效实现并由自动测试支 ...
最新文章
- 自动驾驶中多模态三维目标检测研究综述
- 不学Python迟早会被淘汰?Python真有这么好的前景?
- 对3.0 版本的python依赖无法满足_解决yum对python依赖版本问题
- Java内存结构与垃圾回收机制算法分析
- echart雷达图文字挤在一起_【数据可视化·图表篇】雷达图
- 如何看待开源软件的知识产权问题——陆首群
- ElementUI自定义主题颜色
- ATA/SATA/SCSI/SAS/FC总线简介
- C#中的多线程 - 基础知识 z
- 泛微OA系统Ngnix反向代理
- eclipse svn 没有 connector
- 离散数学 第十三章 欧拉图与哈密顿图
- iOS迅雷V6.01更新,变化重大丨附下载地址
- 如何用饼状图显示数据
- Xcode8快速注释插件无法使用
- linux笔记本 亮度调节,Ubuntu Linux笔记本屏幕背光亮度调节
- 使用 spark sql extensions 实现 skew join
- 电脑显示器屏幕看不清灰色,灰色部分都几乎呈现白色状态的解决办法。
- Matter Test-Harness自动化测试系统
- ue4网格转地形_UE4教程:创建地形材质
热门文章
- (转)漫画:什么是分布式事务?
- What are definitions of ​Model, Inference and Algorithm and its associations ?
- Ubuntu下apt-get方式Git的安装、配置和更新
- cucumber测试_如何在Cucumber中进行后端测试
- aws 堆栈模板_使用Arquillian和LocalStack脱机测试AWS云堆栈
- 字符串url获取参数_如何从URL查询字符串获取示例参数或将其附加到URL查询字符串(示例)?...
- pl/postgresql_将PostgreSQL PL / Java安装为PostgreSQL扩展
- javaserver_如何在JavaServer Pages中使用Salesforce REST API
- 通过委托增强Spring数据存储库
- java整数的因式分解_如何在Java中找到整数的质数-因式分解