局域网电脑做网站,sem推广代运营,兰州城建设计院网站,电脑做视频的网站在本文中#xff0c;我们介绍Spring Integration 。 如果您以前没有使用过Spring Integration#xff0c;那么可能会帮助您复习Gregor Hohpe的Enterprise Integration Patterns 。 我还将推荐Josh Long 撰写的这篇出色的介绍性文章 。 上下文设置 简而言之#xff0c; 企业… 在本文中我们介绍Spring Integration 。 如果您以前没有使用过Spring Integration那么可能会帮助您复习Gregor Hohpe的Enterprise Integration Patterns 。 我还将推荐Josh Long 撰写的这篇出色的介绍性文章 。 上下文设置 简而言之 企业集成模式就是如何使两个应用程序可能位于不同的技术堆栈不同的机器不同的网络上相互通信以提供单个业务功能。 面临的挑战是如何确保这种通信对业务用户保持透明同时又对应用程序可靠且容易。 消息传递是模式之一。 使用此模式应用程序可以使用可自定义的格式频繁立即可靠和异步地相互通信。 应用程序通过在虚拟管道称为Channels 上发送数据称为Messages 来相互交谈。 这是对该概念的过于简单的介绍但希望足以理解本文的其余部分。 Spring Integration不是任何模式的实现但是它支持这些模式主要是消息传递。 本文的其余部分将动手实践并且是Spring 3系列的扩展。本系列的早期文章包括 Hello World with Spring 3 MVC 使用Spring 3 MVC处理表单 使用Spring 3进行单元测试和记录 使用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 groupIdorg.springframework.integration/groupIdartifactIdspring-integration-core/artifactId version2.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 version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:phttp://www.springframework.org/schema/p xmlns:inthttp://www.springframework.org/schema/integration xsi:schemaLocationhttp://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.xsdint:channel idinputChannel/int:channelint:channel idoutputChannelint:queue capacity10 //int:channelint:service-activator input-channelinputChanneloutput-channeloutputChannel refhelloService methodgreet /bean idhelloServiceclassorg.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);AutowiredQualifier(inputChannel)MessageChannel inputChannel;AutowiredQualifier(outputChannel)PollableChannel outputChannel;Testpublic void test() {inputChannel.send(new GenericMessageString(World));assertEquals(outputChannel.receive().getPayload(), Hello World);logger.debug(Checked basic Hello World with Spring Integration);}} 尽管不是强制性的但我发现使用以下登录设置更容易。 如果您愿意可以随时使用它。 文件/src/main/resources/logback.xml ?xml version1.0 encodingUTF-8?
configurationappender nameCONSOLE classch.qos.logback.core.ConsoleAppenderencoderpattern%d %5p | %t | %-55logger{55} | %m %n/pattern/encoder/appenderlogger nameorg.springframeworklevel valueERROR /!-- level valueINFO / --!-- level valueDEBUG / --/loggerrootlevel valueDEBUG /appender-ref refCONSOLE //root
/configuration 现在只需键入“ mvn -e clean install”或使用m2e插件您就应该能够运行单元测试并确认给定的字符串“ World”HelloWorld服务确实在整个通道安排中确实返回了“ Hello World”。和消息。 同样我强烈建议您执行“ mvn -e clean安装站点”这是可选的但我强烈建议。 假设您已正确配置了一些代码覆盖率工具在我的情况下为cobertura将为您提供一个不错HTML报告其中显示了代码覆盖率。 在这种情况下它将是100。 我已经写了一系列关于代码质量的文章 详细介绍了该主题但总而言之确保我使用和推荐使用的任何编码实践/框架都符合一些基本的代码质量标准对我来说非常重要。 。 能够进行单元测试和测量是我所做的这样一项基本检查。 毋庸置疑一般而言Spring包括Spring集成会通过带有鲜艳色彩的检查。 结论 本文就是这样。 在下一篇文章中我们将看到如何将应用程序代码与我们当前的JUnit测试中具有的Spring Integration特定代码 即inputChannel.send…等 隔离 。 建议进一步阅读... 以下是本系列早期文章的链接 Hello World with Spring 3 MVC 使用Spring 3 MVC处理表单 使用Spring 3进行单元测试和记录 使用Spring 3 MVC处理表单验证 这些是我可以推荐的出色材料 Spring Integration入门 Spring Integration的示例代码 Spring集成–第1节– Hello World Spring集成–第2节–更多世界 继续与网关进行Spring集成 参考在Tech for Enterprise博客上我们的JCG合作伙伴 Partho 介绍了Spring Integration 。 翻译自: https://www.javacodegeeks.com/2012/08/introducing-spring-integration.html