介绍Spring Integration

在本文中,我们介绍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;

 @Test
 public 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 clean install site”。 假设您已正确配置了一些代码覆盖率工具(在我的情况下为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集成–第一场– Hello World
  4. Spring集成–第2节–更多世界

继续进行与网关的Spring集成

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值