Spring集成–第2节–更多世界

这是Spring Integration Session 1的后续活动

第一部分是使用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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/372119.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python中Dict的查找

Dict的类型的查找使用的是lookdict函数 static PyDictKeyEntry * lookdict(PyDictObject *mp, PyObject *key,Py_hash_t hash, PyObject ***value_addr) 函数的参数中&#xff0c;*value_addr是指向匹配slot中值的指针。 这个函数在正确的情况下一定会返回一个指向slot的指针&a…

文字特效代码大全

代码收集来源于网络博友,感谢博友提供,本人只收集,整理,说明. 1.删除线:<FONT style"TEXT-DECORATION: line-through">写上你想写的字</FONT> 效果如下 写上你想写的字 2.文字顶部加横线:<font style"text-decoration:overline">写上你想…

oracle 会话实例,返璞归真:Oracle实例级别和会话级别的参数设置辨析

杨廷琨(yangtingkun)云和恩墨 CTO高级咨询顾问&#xff0c;Oracle ACE 总监&#xff0c;ITPUB Oracle 数据库管理版版主参数文件是Oracle数据库文件中级别最低&#xff0c;也是最基本的文件&#xff0c;但是也是数据库实例启动第一个涉及的文件。如果参数文件缺失或者某些参数设…

ExtJs CheckboxSelectionModel 全选操作后 清空表格头的checkBox

关键代码&#xff1a; var hd Ext.getCmp("interviewSubscriptionGrid").getEl().select(div.x-grid3-hd-checker).first(); if (hd.hasClass(x-grid3-hd-checker-on)) { hd.removeClass(x-grid3-hd-checker-on); } 转自&#xff1a;ExtJs Checkbox…

在多节点集群中运行Cassandra

这篇文章收集了我在多节点中设置Apache Cassandra集群的步骤。 在设置集群时&#xff0c;我已经参考了Cassandra Wiki和Datastax文档。 详细介绍了以下过程&#xff0c;分享了我建立群集的经验。 设置第一个节点 添加其他节点 监视集群– nodetool &#xff0c; jConsole &am…

Oracle 添加 scott 示例用户

学习SQL有一段时间了&#xff0c;但是也忘记的差不多了&#xff0c;今天有赶紧复习复习&#xff0c;然后发现一个问题&#xff0c;为啥之前看的视频教程&#xff0c;马士兵用的Oracle有scott用户和那些表格&#xff0c;而我的没有&#xff1f;难道是Oracle取消了&#xff1f;然…

win8oracle10g安装报错,Win8电脑安装Oracle 10g提示程序异常终止的解决方法

有win8系统用户反映说在安装Oracle 10g的时候&#xff0c;选择高级安装之后&#xff0c;就弹出一个窗口&#xff0c;提示程序异常终止&#xff0c;发生内部错误&#xff0c;导致Oracle 10g安装失败&#xff0c;该怎么解决这样的问题呢&#xff1f;下面随小编一起来看看Win8电脑…

MFC的消息循环

MFC的消息循环 消息分为队列消息(进入线程的消息队列)和非队列消息(不进入线程的消息队列)。对于队列消息&#xff0c;最常见的是鼠标和键盘触发的消息&#xff0c;例如WM_MOUSERMOVE,WM_CHAR等消息&#xff1b;还有例如&#xff1a;WM_PAINT、WM_TIMER和WM_QUIT。当鼠标、键…

<avatar: frontiers of pandora>技术overview

https://www.eurogamer.net/digitalfoundry-2023-avatar-frontiers-of-pandora-and-snowdrop-the-big-developer-tech-interview https://www.youtube.com/watch?vLRI_qgVSwMY&t394s 主要来自euro gamer上digital foundry对于avatar的开发团队Massive工作室的采访&#xf…

使用Hibernate 4,JPA和Maven的架构创建脚本

这种情况很简单–您想要在构建应用程序时生成数据库模式创建脚本&#xff08;然后在目标数据库上执行脚本&#xff09;&#xff0c;这对于Hibernate 3来说相对容易&#xff0c;因为有 hibernate3-maven-plugin &#xff0c;但是与Hibernate 4不兼容。当然&#xff0c;对于每个新…

iOS 启动连续闪退保护方案

版权声明&#xff1a;本文由刘笑江原创文章&#xff0c;转载请注明出处: 文章原文链接&#xff1a;https://www.qcloud.com/community/article/79 来源&#xff1a;腾云阁 https://www.qcloud.com/community 一.引言 “如果某个实体表现出以下任何一种特性&#xff0c;它就具备…

实战Java内存泄漏问题分析 -- hazelcast2.0.3使用时内存泄漏 -- 2

hazelcast 提供了3中方法调用startCleanup:第一种是在ConcuurentMapManager的构造函数中&#xff0c;通过调用node的executorManager中的ScheduledExecutorService来创建每秒运行一次cleanup操作的线程&#xff08;代码例如以下&#xff09;。因为这是ConcuurentMapManager构造…

oracle 11203 ora32701,11G RAC ORA-32701 参考学习

节点1&#xff1a;Wed Feb 13 16:08:06 2019Errors in file /u01/app/oracle/diag/rdbms/testdb/testdb1/trace/testdb1_dia0_9267.trc (incident1248083):ORA-32701: Possible hangs up to hang ID4 detectedIncident details in: /u01/app/oracle/diag/rdbms/testdb/testdb1/…

使用@OrderBy对Spring Data MongoDB集合进行排序

这是关于调整和增强Spring Data MongoDB功能的第三篇文章。 这次&#xff0c;我发现我错过了一个JPA功能– OrderBy批注。 OrderBy指定在检索关联值时集合值关联的元素的顺序。 在本文中&#xff0c;我将展示如何使用Spring Data MongoDB使用OrderBy批注实现排序 。 用例 对…

@SuppressLint(NewApi)和@TargetApi()的区别

转自&#xff1a;http://blog.csdn.NET/wbshuang09/article/details/44920549在Android代码中&#xff0c;我们有时会使用比我们在AndroidManifest中设置的android:minSdkVersion版本更高的方法&#xff0c;此时编译器会提示警告&#xff0c;解决方法是在方法上加上SuppressLin…

零基础自学编程前需要知道的知识

你是否适合编程?学习编程后能做什么?如何选择编程语言?有哪些免费的线上学习网站推荐?今天这篇好文将那些自学编程前需要了解和思考的问题都记录下来&#xff0c;希望能给那些刚刚开始或正准备自学编程的朋友们带去一些启发。 你是否适合自学编程 自学编程会是一个漫长而艰…

oracle系统库名,Oracle 札记之 一:数据库名,数据库实例名,数据库域名,操作系统环境变量...

数据库名是用于区分数据库的一个内部标识&#xff0c;是以二进制方式存储在数据库控制文件中的参数。数据库创建之后不能再修改这个参数。数据库创建后&#xff0c;它被写入数据库参数文件pfile或Spfile中。格式如下&#xff1a;...db_name"orcl"db_domaindbcenter.t…

用于基于SWT的应用程序的RichText编辑器组件

本文将完成使用SWT实现我们自己的RichText编辑器组件的任务。 在为我的一位客户开发基于桌面的应用程序时&#xff0c;我遇到了这样一个可视化组件的需求&#xff0c;并希望添加一项功能&#xff0c;以允许用户使用粗体&#xff0c;斜体&#xff0c;删除线等功能来写富文本注释…

Eclipse设置黑色主题

1点击help--->install new software 2输入 http://eclipse-color-theme.github.com/update 3下载安装eclipse color theme插件如下图 4完成后点击windows--->preferences------>Appearance下多了一个Color Theme 5,点击选择喜欢的主题即可&#xff0c;也可以自己下载主…

wcf rest系列文章

http://www.cnblogs.com/artech/archive/2012/02/15/wcf-rest.html 需要注意的是&#xff0c;发布的服务&#xff0c;可以在web behavior中指定显示help页面。 http://localhost/ApplicationName/ServiceName.svc/help 需要注意的是&#xff0c;访问.svc的页面一定不要多加/;否…