Spring集成:轻量级集成方法

当今的应用程序希望能够访问企业环境中的所有业务,而无需考虑与绝望的系统无缝集成的应用程序技术。 可以通过使用中间件技术对各种系统进行布线来实现这种集成。

集成平台使应用程序可以相互共享信息的环境,从而使体系结构具有高度的互操作性。 Spring Integration提供了一个中介框架,以使用路由和中介构建轻量级集成解决方案,而与协议无关。 Spring Integration是轻量级的路由和中介框架,不需要笨重的ESB容器即可部署。

Spring Integration使用Message对象来通信,路由和传递数据,这没什么,但是Java对象上的包装器由有效负载和标头组成。 有效载荷包含的数据可以是任何类型,例如文件,字符串,流等,而标头包含有关消息的通用信息,例如ID,时间戳等。

消息通过通道与生产者进行通信,该生产者将源与目标分离,并将消息发布到任何协议,例如JMS,HTTP,Ldap,文件等。生产者将消息发送到通道,而使用者则从通道接收消息

spring-integration_simple

简单集成应用

下例显示了生产者如何将雇员对象发送到渠道,发布者如何从渠道接收消息。

Spring Dependency Maven配置

为了开始简单的集成示例,我们只需要将核心spring集成和spring上下文依赖项添加到maven pom.xml中。 此外,我们还需要Junit和spring测试才能进行单元测试

<properties>
 <spring.framework.version>3.2.3.RELEASE</spring.framework.version>
 <spring.integration.version>2.2.4.RELEASE</spring.integration.version>
 <junit.version>4.11</junit.version>
 </properties>
 <dependencies>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>${spring.framework.version}</version>
 </dependency>
 <dependency>
 <groupId>org.springframework.integration</groupId>
 <artifactId>spring-integration-core</artifactId>
 <version>${spring.integration.version}</version>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>${junit.version}</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-test</artifactId>
 <version>${spring.framework.version}</version>
 <scope>test</scope>
 </dependency>
 </dependencies>

弹簧配置
我们必须在Spring配置中配置通道和网关以发送和接收消息

SpringIntegTest-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd">
 <!--
 sendRequest
 receiveRequest
 -->
 <annotation-config/>
 <context:component-scan base-package="org.springsample.integration"/>
 <gateway id="request" service-interface="org.springsample.integration.SentRequest"/>
 <channel id="sendRequest"/>
 <outbound-channel-adapter channel="sendRequest" ref="receiveResponse" method="processMessage" />
</beans:beans>

在这里,我们创建了请求网关以将消息发送到通道,并创建出站适配器以从sendRequest通道接收消息。 Spring Integration的网关是发送消息的入口点,该消息使用Java接口指定。 Spring Integration在运行时自动定义代理实现

请求和接收

下面我们创建SentRequest和ReceiveResponse类,以发送和接收消息,如下所示

SentRequest.java

package org.springsample.integration;
import org.springframework.integration.annotation.Gateway;
public interface SentRequest {
 @Gateway(requestChannel="sendRequest")
 public void process(Employee emp);

}

@Gateway批注将指示发送消息的入口点

package org.springsample.integration;
import org.springframework.integration.Message;
import org.springframework.integration.annotation.MessageEndpoint;
@MessageEndpoint
public class ReceiveResponse {
public void processMessage(Message<Employee> message) {
 Employee employee = message.getPayload();
 System.out.println("Message Received \n Name :"+employee.getName()+"/n Phone : "+employee.getPhone()+"/n Address :"+employee.getAddress());

 }
}

@MessageEndpoint将指示它将通过适配器从通道接收消息。

以下是雇员POJO,但并非不行

package org.springsample.integration;
public class Employee {
 private String name;
 private String title;
 private String address;
 private String phone;

public Employee(String name, String phone, String address) {this.name=name;this.phone=phone; this.address=address;
//……..Getter amd Setter
 }
}

测试中

我们可以使用spring测试框架进行测试,如下所示

package org.springbyexample.integration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SpringIntegTest {
 @Autowired
 private SentRequest request = null;
 @Test
 public void testIntegration() {
 Employee emp = new Employee("John", "12345678", "Sunny Street Mac RG1");
 request.process(emp);
 }
}

确保在org / springbyexample / integration中保留spring配置文件名称SpringIntegTest-context,并且应该在类路径中
在运行SpringIntegTest时,它将显示控制台消息,如下所示

收到消息
名字:John / n电话:12345678 / n地址:Sunny Street Mac RG1

摘要

Spring Integration是开放源代码的简单集成,可增强松散耦合并使应用程序集成变得容易和简单。 它将以可配置的方式在通道和网关之间集成,路由和中介消息。 本文有助于了解Spring Integration,并将帮助您开发一个简单的集成应用程序。

资源资源

参考: Spring集成:我们的JCG合作伙伴 Nitin Kumar在Tech My Talk博客上提出的一种轻量级集成方法

翻译自: https://www.javacodegeeks.com/2013/09/spring-integration-a-lightweight-integration-approach.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值