在本文中,我们介绍Spring Integration 。 如果您以前没有使用过Spring Integration,那么可能会帮助您复习Gregor Hohpe的Enterprise Integration Patterns 。 我还将推荐Josh Long 撰写的这篇出色的介绍性文章 。

上下文设置

简而言之, 企业集成模式就是如何使两个应用程序(可能位于不同的技术堆栈,不同的机器,不同的网络上)相互通信,以提供单个业务功能。 面临的挑战是如何确保这种通信对业务用户保持透明,同时又对应用程序可靠且容易。 消息传递是模式之一。 使用此模式,应用程序可以使用可自定义的格式频繁,立即,可靠和异步地相互通信。 应用程序通过在虚拟管道(称为Channels )上发送数据(称为Messages )来相互交谈。 这是对该概念的过于简单的介绍,但希望足以理解本文的其余部分。

Spring Integration不是任何模式的实现,但是它支持这些模式,主要是消息传递。

本文的其余部分将动手实践,并且是Spring 3系列的扩展。本系列的早期文章包括:

  1. Hello World with Spring 3 MVC
  2. 使用Spring 3 MVC处理表单
  3. 使用Spring 3进行单元测试和记录
  4. 使用Spring 3 MVC处理表单验证

事不宜迟,让我们开始吧。

裸露骨骼的Spring集成示例

在撰写本文时,Spring的最新版本是3.1.2.RELEASE。 但是,最新版本的Spring Integration是2.1.3.RELEASE,可在Maven Central中找到。 我有些不满意-回想起来不合逻辑-对Spring和Spring Integration应该具有不同的最新版本感到吃惊,但是,嘿,就是这样。 这意味着我们的pom.xml现在应该添加一个(如果您想知道那是从何而来的,至少在很高的层次上,我需要在本文前面提到的Spring 3系列继续学习)。

文件:/pom.xml

<!-- Spring integration -->
<dependency>                                          <groupId>org.springframework.integration</groupId><artifactId>spring-integration-core</artifactId>  <version>2.1.3.RELEASE</version>
</dependency>

pom中的这一依赖性现在允许我的应用程序通过channel发送消息 。 请注意,现在我们在Spring Integration领域中引用消息和通道,这不一定与本文前面在Enterprise Integration Patterns领域中引用的相同概念完全相同。 此时可能值得快速浏览一下《 Spring Integration参考手册》 。 但是,如果您刚刚开始使用Spring Integration,那么暂时最好阅读本文。 我建议您先洗手,然后再返回参考手册,该手册非常好,但也非常详尽,因此对于初学者来说可能不胜枚举。

为简单起见,由于我通常尝试(尽可能)尝试第一种方法,因此让我们尝试编写一些单元测试以创建消息,然后通过通道发送它,然后再接收它。 我在这里写了关于如何在Spring 3应用程序中使用JUnit和Logback的博客 。 继续相同的原理,假设我们要编写一个HelloWorldTest.java,让我们为测试设置Spring配置。

文件:\ src \ test \ resources \ org \ academy \ integration \ HelloWorldTest-context.xml

<?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:p='http://www.springframework.org/schema/p' xmlns:int='http://www.springframework.org/schema/integration' xsi:schemaLocation='http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-2.1.xsd'><int:channel id='inputChannel'></int:channel><int:channel id='outputChannel'><int:queue capacity='10' /></int:channel><int:service-activator input-channel='inputChannel'output-channel='outputChannel' ref='helloService' method='greet' /><bean id='helloService'class='org.academy.integration.HelloWorld' /> </beans>

那么,我们只是做什么? 我们已经要求Spring Integration创建一个“ inputChannel”来发送消息。 从中读取消息的“ outputChannel”。 我们还配置了将“ inputChannel”上的所有消息都移交给“ helloService”的功能。 此“ helloService”是org.academy.integration.HelloWorld类的实例,应具有对消息进行某些处理的能力。 之后,我们还配置了“ helloService”的输出,即在这种情况下修改后的消息,将被移交给“ outputChannel”。 很简单,不是吗? 坦率地说,当我几年前第一次与Spring Integration合作时,我发现所有这些都令人困惑。 直到我看到这个工作对我来说,这没有多大意义。 因此,让我们继续前进。 让我们添加关键业务 HelloWorld类。

文件:/src/main/java/org/academy/integration/HelloWorld.java

package org.academy.integration;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;public class HelloWorld {private final static Logger logger = LoggerFactory.getLogger(HelloWorld.class);public String greet(String name){logger.debug('Greeting {}', name); return 'Hello ' + name; }
}

如您所见,给定一个“名称”,它将返回“ Hello {name}”。 现在,让我们添加单元测试以实际执行此操作。

文件:/src/test/java/org/academy/integration/HelloWorldTest.java

package org.academy.integration;import static org.junit.Assert.*;import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class HelloWorldTest {private final static Logger logger = LoggerFactory.getLogger(HelloWorldTest.class);@Autowired@Qualifier('inputChannel')MessageChannel inputChannel;@Autowired@Qualifier('outputChannel')PollableChannel outputChannel;@Testpublic void test() {inputChannel.send(new GenericMessage<String>('World'));assertEquals(outputChannel.receive().getPayload(), 'Hello World');logger.debug('Checked basic Hello World with Spring Integration');}}

尽管不是强制性的,但我发现使用以下登录设置更容易。 如果您愿意,可以随时使用它。

文件:/src/main/resources/logback.xml

<?xml version='1.0' encoding='UTF-8'?>
<configuration><appender name='CONSOLE' class='ch.qos.logback.core.ConsoleAppender'><encoder><pattern>%d %5p | %t | %-55logger{55} | %m %n</pattern></encoder></appender><logger name='org.springframework'><level value='ERROR' /><!-- level value='INFO' />  --><!--  level value='DEBUG' />  --></logger><root><level value='DEBUG' /><appender-ref ref='CONSOLE' /></root>
</configuration>

现在,只需键入“ mvn -e clean install”(或使用m2e插件),您就应该能够运行单元测试并确认给定的字符串“ World”,HelloWorld服务的确在整个通道安排中确实返回了“ Hello World”和消息。

同样,可选但我强烈建议您运行“ mvn -e全新安装站点”。 假设您已正确配置了一些代码覆盖率工具(在我的情况下为cobertura),将为您提供一个不错HTML报告,其中显示了代码覆盖率。 在这种情况下,它将是100%。 我已经写了一系列关于代码质量的文章 ,详细介绍了该主题,但是总而言之,确保我使用和推荐使用的任何编码实践/框架都符合一些基本的代码质量标准对我来说非常重要。 。 能够进行单元测试和测量是我所做的这样一项基本检查。 毋庸置疑,一般来说,Spring(包括Spring集成)会通过带有鲜艳色彩的检查。

结论

本文就是这样。 在下一篇文章中,我们将了解如何将应用程序代码与我们当前的JUnit测试中具有的Spring Integration特定代码 (即inputChannel.send(…)等) 隔离 。

建议进一步阅读...
以下是本系列早期文章的链接:

  1. Hello World with Spring 3 MVC
  2. 使用Spring 3 MVC处理表单
  3. 使用Spring 3进行单元测试和记录
  4. 使用Spring 3 MVC处理表单验证

这些是我可以推荐的出色材料:

  1. Spring Integration入门
  2. Spring Integration的示例代码
  3. Spring集成–第1节– Hello World
  4. Spring集成–第2节–更多世界

继续与网关进行Spring集成

参考:在Tech for Enterprise博客中,我们的JCG合作伙伴 Partho 介绍了Spring Integration 。

翻译自: https://www.javacodegeeks.com/2012/08/introducing-spring-integration.html

引入Spring集成相关推荐

  1. Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)

    说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...

  2. Spring集成Shiro框架实战

    文章目录 一:什么是Shiro框架 二:Shiro框架简介 1.Shiro基础功能点介绍 2.Shiro的工作原理 3.Shiro的内部工作结构 4.Shiro的身份认证流程 三:Spring集成Sh ...

  3. apache camel_轻量级的开源集成:Apache Camel还是Spring集成?

    apache camel 首先,为全面披露信息,在过去的1.5年中, 我一直担任 FuseSource(现为Red Hat) 的顾问,为零售,运输,银行/金融等不同行业的大型和小型公司提供SOA和集成 ...

  4. 轻量级的开源集成:Apache Camel还是Spring集成?

    首先,为全面披露信息,在过去的1.5年中, 我一直担任 FuseSource(现为Red Hat) 的顾问,为零售,运输,银行/金融等不同行业的大型和小型公司提供SOA和集成项目支持.我的专长是使用该 ...

  5. Spring集成–强大的拆分器聚合器

    坚固是什么意思? 在本文的上下文中,健壮性是指在不立即返回到调用者的情况下管理流中的异常条件的能力. 在某些处理方案中, n个 m个回答足以做出结论. 通常具有这些趋势的示例处理场景是: 财务,保险和 ...

  6. Mybatis源码之与Spring集成包

    这次讲讲Mybatis与Spring的整合,作为两款优秀的开源框架,被大众广泛使用,自然是需要强强联合的. 使用示例 先看一下怎么使用,首先需要引用这两款框架的jar包: <dependency ...

  7. 消息中间件系列四:RabbitMQ与Spring集成

    一.RabbitMQ与Spring集成  准备工作: 分别新建名为RabbitMQSpringProducer和RabbitMQSpringConsumer的maven web工程 在pom.xml文 ...

  8. Dubbo源码分析(一)Dubbo与Spring集成实例

    前言 Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能.轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自 ...

  9. activiti与spring集成

    摘要: 一.集成配置 <bean id="dataSource" class="org.springframework.jdbc.datasource.Simple ...

最新文章

  1. QIIME 2用户文档. 14机器学习预测样品元数据分类和回归q2-sample-classifier(2018.11)
  2. java数据传递给安卓_Android数据传递的五种方法汇总
  3. [转]sleep和wait有什么区别
  4. eclipse类自动生成注释
  5. Android JNI Attempt to remove non-JNI local reference, dumping thread
  6. SpringBoot : Spring容器生命周期管理:SmartLifecycle
  7. 第一章 架构 1.4 编译 amp; 1.5总结
  8. Linux之lastb命令
  9. Severstal: Steel Defect Detection竞赛
  10. 自定义下拉回弹View-掌握View冲突处理
  11. xshell 6顶部工具栏找不到
  12. 计算机绘图入门,[2018年最新整理]AutoCAD计算机绘图入门.ppt
  13. WebAssembly之wasm2c工具编译使用
  14. 【ArnoldC4D】1.ArnoldToC4D_基本概念及原理(sampling_and_camera_sampl)
  15. 异质化社群量化研究4丨RATE OF CHANGE WITH BANDS
  16. aspose-words基本操作
  17. Adobe illustrator 为什么对不齐?AI无法对齐操作?如图我画了一个形状想用园和那条直线对其但是我鼠标拖动智能参考线都已经对齐了 但是松开后又错开了
  18. AdobeFlash playe不是最新版本解决办法
  19. NEO4J搭建京东手机类小型知识图谱
  20. 新工作,新环境,新征程

热门文章

  1. tomcat(7)日志记录器
  2. 关闭json引用的方式
  3. javafx 遮罩_JavaFX技巧31:遮罩/剪切/ Alpha通道
  4. oauth2和jwt_OAuth2,JWT,Open-ID Connect和其他令人困惑的事物
  5. couchbase_具有Rx-Java的Couchbase Java SDK
  6. 用于混合Spock 1.x和JUnit 5测试的Maven项目设置
  7. API网关和AWS Lambda进行身份验证
  8. OpenLiberty:注入时出错,适用于TomEE和Wildfly
  9. eclipse中junit_在Eclipse中有效使用JUnit
  10. java三件套_Java开发人员应该知道的三件事