Spring Integration Framework简介

我们非常了解Spring框架和JMS 。 在本文中,我们将介绍称为Spring Integration企业集成框架Spring Integration是一个开源企业集成框架,可增强Spring单独完成的功能。 Spring Integration构建在Spring的IoC之上,它抽象了消息源和目标,集成了消息,路由并对其进行操作,同时集成了应用程序环境的各种组件。

Spring Integration用于通信的Message对象有效负载标头数据组成。 有效负载包含实际数据,而标头包含其他元数据,例如idtimestamp等。下图说明了Spring Integration通信中涉及的不同组件

因此,让我们创建一个示例Spring Integration应用程序:

使用自定义Java接口作为入站网关

  1. 创建一个新的Maven项目。
  2. 将依赖项添加到pom.xml文件中:
    <properties><spring.version>4.0.0.RELEASE</spring.version><jms.version>1.1-rev-1</jms.version><activemq.version>3.1</activemq.version><spring.integration.version>2.2.5.RELEASE</spring.integration.version><junit.version>4.11</junit.version><spring.test.version>3.2.3.RELEASE</spring.test.version>
    </properties><!-- Spring Integration -->
    <dependency><groupId>org.springframework.integration</groupId><artifactId>spring-integration-core</artifactId><version>${spring.integration.version}</version>
    </dependency>
    <dependency><groupId>org.springframework.integration</groupId><artifactId>spring-integration-jms</artifactId><version>${spring.integration.version}</version>
    </dependency><!-- Include Spring test -->
    <dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.test.version}</version><scope>test</scope>
    </dependency><!-- Include JUnit -->
    <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope>
    </dependency>
  3. 创建学生实体类Student.java  如:
    package com.jcombat.entity;public class Student {String name;public Student(String name) {this.name = name;}public String getName() {return name;}
    }
  4. 现在,让我们在上下文文件中配置spring集成组件(将其命名为Test-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:context="http://www.springframework.org/schema/context"xmlns:jms="http://www.springframework.org/schema/jms" xmlns:p="http://www.springframework.org/schema/p"xmlns:int-jme="http://www.springframework.org/schema/integration"xmlns:int="http://www.springframework.org/schema/integration"xmlns:int-jms="http://www.springframework.org/schema/integration/jms"xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd  http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsdhttp://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsdhttp://www.springframework.org/schema/jms  http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"><!-- Enable annotations --><context:annotation-config/><!-- Component scan to find all Spring components --><context:component-scan base-package="org.jcombat.integration"/><bean id="simpleMessageReceiver" class="com.jcombat.integration.DemoMessageReceiver" /><!-- Define a request channel to communicate --><int:channel id="request" /><int:outbound-channel-adapter channel="request" ref="simpleMessageReceiver" method="processMessage" /><int:gateway id="demoGateway" service-interface="com.jcombat.integration.DemoGatewayInterface"default-request-channel="request"></int:gateway></beans>

    消息通道是封装实际数据并使消息生产者与使用者分离的东西。

    网关基本上是消息传递系统的入口/出口点。 因此,如果您有一个消息传递服务器,例如ActiveMQ或TIBCO,则DefaultMessageListener充当入站网关,是我们消息传递系统的入口点。

    当消息到达配置的通道时, Service Activator用于调用本地服务,其方式是该服务不知道正在从消息传递系统中调用该服务。

    适配器从外部消息传递系统(JMS,SFTP等)接收消息,并将其“适配”到消息传递系统(作为Spring Integration Message <T> )。 入站JMS适配器接收传入的外部消息,并将其“调整”为Spring Integration Message <T>类型。 反之亦然:它需要一个Spring Integration Message <T>并将其“调整”为外部系统所需的格式。

    因此,一旦消息通过入站适配器进入,它就会通过Channels从一个组件流向另一个组件。 最终,将消息写到某个地方可能是适当的。 我们可以使用出站适配器编写消息。

  5. 现在,让我们创建在上下文中指定的网关接口,如下所示:
    package com.jcombat.integration;import com.jcombat.entity.Student;public interface DemoGatewayInterface {public void process(Student student);
    }
  6. 创建一个接收器类,该类将在被适配器路由后最终从通道接收传入消息。
    package com.jcombat.integration;import org.springframework.integration.Message;import com.jcombat.entity.Student;public class DemoMessageReceiver {public void processMessage(Message<Student> message) {Student student = message.getPayload();System.out.println("Message Received - Student Name - " + student.getName());}
    }
  7. 就是这个。 最后,我们需要一个客户端来调用网关接口方法。 让我们使用Spring Test Framework来做到这一点:
    package com.jcombat.integration;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;import com.jcombat.entity.Student;
    import com.jcombat.integration.DemoGatewayInterface;@RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    public class Test {@Autowiredprivate DemoGatewayInterface request;@org.junit.Testpublic void testIntegration() {Student emp = new Student("Abhishek");request.process(emp);}
    }
  8. 将您的应用程序添加到服务器并启动服务器。
  9. 导航到我们在上文第7点中创建的Test.java文件,并将其作为JUnit测试运行。 以下是我们在IDE控制台中看到的日志:

    短跑
  10. 使用Spring DefaultMessageListener作为入站网关

    1. 创建一个新的Maven项目。
    2. 确保ActiveMQ服务器已启动并正在运行。
    3. 首先是将条目输入pom.xml文件。 因此,这与前面的情况相同。
    4. 让我们编写上下文文件( jmsContext.xml ),并将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:context="http://www.springframework.org/schema/context"xmlns:jms="http://www.springframework.org/schema/jms" xmlns:p="http://www.springframework.org/schema/p"xmlns:int-jme="http://www.springframework.org/schema/integration"xmlns:int="http://www.springframework.org/schema/integration"xmlns:int-jms="http://www.springframework.org/schema/integration/jms"xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd  http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsdhttp://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsdhttp://www.springframework.org/schema/jms  http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"><bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://localhost:61616" /></bean><bean id="messageListenerContainer"class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destinationName" value="testQueue" /><property name="maxConcurrentConsumers" value="1" /><property name="concurrentConsumers" value="1" /><property name="receiveTimeout" value="5000" /><property name="recoveryInterval" value="60000" /><property name="autoStartup" value="true" /></bean><!-- Define a channel to communicate out to a JMS Destination --><int:channel id="inbound" /><int:channel id="outbound" /><bean id="simpleMessageListener" class="com.jcombat.listener.SimpleMessageListener" /><int-jms:message-driven-channel-adapterid="jmsIn" container="messageListenerContainer" channel="inbound"acknowledge="auto" /><int:service-activator input-channel="inbound"output-channel="outbound" ref="simpleMessageListener" method="onMessage" /><int-jms:outbound-channel-adapter id="jmsOut"channel="outbound" connection-factory="connectionFactory"destination-name="sampleQueue" /></beans>
    5. 让我们在服务器启动时加载上下文。 为此,请在web.xml文件中输入适当的内容,如下所示:
      <web-app xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/jmsContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
      </web-app>
    6. 创建侦听器类,该类将最终接收消息,如下所示:
      package com.jcombat.listener;public class SimpleMessageListener {public String onMessage(String message) {System.out.println(message);return message;}
      }
    7. 是时候运行我们的应用程序了。 因此,这应该作为:
      1. 发送自定义消息到testQueue

        sprInt1
      2. 侦听器被调用,它的onMessage方法被执行,将进入的消息记录为IDE控制台:

        sprInt2
      3. 消息通过出站通道并通过出站适配器传递到目标sampleQueue ,如下所示:

        sprInt3
    8. 下载源代码

      您可以在此处下载源代码。

翻译自: https://www.javacodegeeks.com/2015/11/introduction-to-spring-integration-framework.html

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

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

相关文章

网络营销广告投放策略

网络营销广告投放策略 网络营销第一桶金&#xff1a;10年微博热火&#xff0c;粉丝1毛一个&#xff0c;我看到了这个机会。开发了注册微博账户的软件可以卖粉丝了怎么推广呢微博账户头像上加广告&#xff0c;去关注活人&#xff0c;被关住的人&#xff0c;就能看到广告&#xf…

空间皮肤代码_OpenCV实现皮肤表面粗糙度3D显示

点击上方蓝字关注我们微信公众号&#xff1a;OpenCV学堂关注获取更多计算机视觉与深度学习知识问题分析与思路这个是最近有人问我的一个问题&#xff0c;想把一个拍好的皮肤图像&#xff0c;转换为3D粗糙度表面显示&#xff0c;既然是粗糙度表面显示&#xff0c;我想到的就是把…

windows修改时间服务器,在Windows中设置时间服务器 2012 R2

大家都知道, 时的服务是任何网络中最重要的组成部分, 任何系统, 在所有计算机上同步时钟是我们可以做的事情工作&#xff0c;也都成功同步的内部系统最少. 通常情况下&#xff0c;我们有一个网络时间服务器以从外部时钟获得的时间和内部提供.什么NTP服务器的默认地图, 在这种情…

shell脚本遍历分库分表数据

vim shell.sh 十库百表 for n in {1..10}doif [ $n ! 10 ]thenn0"${n}"fifor i in {00..99}domysql -h host${n} -P port -uusername -ppwd -D database -e SQL >> lognamedonedone 转载于:https://www.cnblogs.com/Jaxlinda/p/7079391.html

q版地图制作软件_Flash动画的图形元件实例-Q版人物侧面行走

对于刚入门者而言&#xff0c;学会了基本图形的绘制之后&#xff0c;如何应用软件的各种动画补间功能&#xff0c;制作出具有表现力的动画&#xff0c;就需要更进一阶的知识技能了&#xff1b;那么&#xff0c;设计制作一个卡通人物的行走效果&#xff0c;如何从没有头绪的任务…

绝地服务器维护7月5日,绝地求生正式服7月5日停机更新维护内容公告

我们将在北京时间7月5日(星期四) 上午10点 开始正式服的停机维护。- 维护开始时间&#xff1a;7月5日(星期四) 上午10点 (预计3小时)玩家们大家好&#xff0c;伴随PGI2018的临近&#xff0c;我们将向大家呈现以PGI为主题、全新风格的PUBG。我们将向玩家们提供丰富的活动和奖品&…

javafx2_JavaFX 2 GameTutorial第1部分

javafx2介绍 我相信大多数软件开发人员可能会在年轻人&#xff08;年轻人&#xff09;生活的某一时刻被迫创建游戏来帮助他们学习编程语言&#xff08;我知道我曾经做过&#xff09;。 以前&#xff0c;我的第一台计算机实际上是Franklin Ace 1000 &#xff0c;后来是Apple [] …

webstorm最新破解方法

方法来自 Rover12421 大神。 1.从官网下载WebStorm2016.1安装。 2.下载 破解补丁 并解压&#xff0c;记住路径 3.编辑WebStorm安装目录下 bin 文件夹中的 WebStorm.exe.vmoptions 与 WebStorm64.exe.vmoptions 文件&#xff0c; 在头部加上 -javaagent:D:\Program Files (x86)\…

python作者 google面试_如果Google面试让你用python写一个树的遍历程序

前几天忽然对python很感兴趣&#xff0c;学了几天也感觉它非常的简洁实用。打破了我这么长时间对java C# C 和vb的审美疲劳&#xff0c;让我眼前一亮。“就像读英文一样简单”这句话评价python说的很合理。我对python的好感很大部分是因为听说google很多程序用python&#xff0…

加载页面就触发ajax,AJAX post方法,有时会在页面加载时触发,有时不会

我对AJAX有一个奇怪的问题&#xff0c;我在页面加载时使用AJAX POST方法返回对象地图。我正在调试该过程&#xff0c;有时会调用该方法&#xff0c;并且Java Servlet有时会运行。我正确地包含了JS导入&#xff0c;其他jQuery调用正常工作。我试过不同的探险家。我使用的是GET方…

在Java 8中使用不带静态导入的Mockito

如何通过在基于Java 8的项目中删除静态导入来简化Mockito的使用。 基本原理 Mockito API基于&#xff08;BDD&#xff09;Mockito类中聚集的静态方法&#xff08;大部分&#xff09;&#xff0c;然后进行非常流畅的链接方法调用。 可以使用模拟/间谍/给定/然后/验证静态方法启…

python object has no attribute_如何修复python中的“AttributeError:type object has no attribute”?...

您的代码引发此异常&#xff1a;AttributeError: type object Meeting has no attribute datetime在这一行&#xff1a;meeting_start Meeting.datetime.start_time.hourPython告诉您&#xff0c;Meeting类没有名为datetime的属性。这是真的&#xff1a;Meeting类是一个制造me…

ajax 页面无刷新,Ajax的页面无刷新实现详解(附代码)

这次给大家带来Ajax的页面无刷新实现详解(附代码)&#xff0c;Ajax页面无刷新实现的注意事项有哪些&#xff0c;下面就是实战案例&#xff0c;一起来看一下。ajax (ajax开发)AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML)&#xff0c;是指一种创建交互式网页…

运营管理最新版史蒂文森_运营增长人都在看的硬核案例拆解是怎么做的?

你会拆案例吗&#xff1f;大部分运营增长人听到这个问题都会愣一下&#xff0c;心想这有什么会拆不会拆的&#xff1f;看一下活动规则&#xff0c;把流程走一遍&#xff0c;不就可以了&#xff1f;当马上要做活动但又没思路缺灵感时&#xff0c;我们通常会试着先去关注一下相关…

pc网站和移动网站在同一服务器吗,机动都市阿尔法PC服和移动服互通吗

机动都市阿尔法PC服已经开启了&#xff0c;很多小伙伴想知道这个PC服和移动服有什么区别&#xff0c;互通情况怎么样&#xff0c;下面就是机动都市阿尔法PC服和移动服互通的具体内容&#xff0c;一起来看看吧。PC服和移动服互通吗国服PC版开启时&#xff0c;将额外增设一个独立…

ssh结合使用

springxml配置 <?xml version"1.0" encoding"UTF-8"?> <web-app xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xmlns"http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation"http://xmlns.jcp.org/xml/ns/…

teamcity_TeamCity构建依赖项

teamcity介绍 构建依赖关系的主题既非琐碎的&#xff0c;也非次要的。 各种构建工具从不同的角度处理此主题&#xff0c;从而提供了各种解决方案&#xff0c;每种解决方案都有其优点和缺点。 熟悉发行版和快照依赖关系的Maven和Gradle用户可能不了解TeamCity快照依赖关系&…

OC Swift 走马灯效果

我们常见走马灯样式的功能&#xff0c;下面整理一下 Object-C 与 Swift 的实现代码 OC UILabel *label3 [[UILabel alloc] initWithFrame:CGRectMake(10,200, self.view.bounds.size.width, 100)]; label3.backgroundColor [UIColor redColor]; label3.text "走马灯 走马…

sql怎么修改服务器角色,创建、删除或修改角色 (Management Studio)

创建、删除或修改角色 (Management Studio)06/13/2017本文内容Reporting Services 提供了定义对报表服务器的访问级别的预定义角色。 需要访问报表服务器的每个用户或组都通过说明可以执行的任务的角色来进行访问。 这些角色是对作为整体的报表服务器进行定义的。 不能对报表服…

python中产生随机数模块_Python中random模块生成随机数详解

Python中的random模块用于生成随机数。下面介绍一下random模块中最常用的几个函数。random.randomrandom.random()用于生成一个0到1的随机符点数: 0 < n < 1.0random.uniformrandom.uniform的函数原型为&#xff1a;random.uniform(a, b)&#xff0c;用于生成一个指定范围…