spring integration 入门

本文深入探讨了Spring Integration框架的三大核心组件:通道、通道拦截器和端点。详细介绍了各种通道类型,如PublishSubscribeChannel、QueueChannel等,以及它们在消息传递中的作用。同时,解析了通道拦截器提供的发送与接收切面功能,和多种端点如ChannelAdapter、Gateway、ServiceActivator等在消息处理流程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考地址:https://blog.youkuaiyun.com/w_x_z_/article/details/53316618

              https://blog.youkuaiyun.com/qq_27808011/article/details/80108622

 

spring integration 分为三部分: 通道,通道拦截器,端点;

通道有:PublishSubscribeChannel(广播 允许消息接收者轮询获得消息),QueueChannel(队列 允许消息接收者轮询获得消息),PriorityChannel(优先 按照优先级将数据存储到队列),RendezvousChannel(约定:在接收方receive()之前,阻塞该通道),.DirectChannel(默认 与RendezvousChannel的区别没搞明白),ExecutorChannel(多线程 没看懂)

通道拦截器: channel.addInterceptor(someInterceptor);提供发送与接受的前 后 成功后六个切面

端点: 是三者中处理消息的组件

(1)Channel Adapter(是一种连接外部系统或传输协议的端点,分为inbound与outbound两种 是单向的),

(2)Gateway(类似于Adapter,但是提供了双向的请求/返回集成方式,也分为入站'inbound'和出站'outbound'),

(3) Service Activator: Service Activator 可调用Spring的Bean来处理消息,并将处理后的结果输出到指定的消息通道

(4) Router: 路由(Router) 可根据消息体内容(Payload Type Router)、消息头的值(Header Value Router) 以及定义好的接收表(Recipient List Router) 作为条件,来决定消息传递到的通道

(5) Filter: 过滤器(Filter) 类似于路由(Router),不同的是过滤器不决定消息路由到哪里,而是决定消息是否可以传递给消息通道

(6) Splitter: 拆分器(Splitter)将消息拆分为几个部分单独处理,拆分器处理的返回值是一个集合或者数组

(7) Aggregator: 聚合器(Aggregator)与拆分器相反,它接收一个java.util.List作为参数,将多个消息合并为一个消息

(8) Enricher: 当我们从外部获得消息后,需要增加额外的消息到已有的消息中,这时就需要使用消息增强器(Enricher)。消息增强器主要有消息体 
增强器(Payload Enricher)和消息头增强器(Header Enricher)两种

(9) Transformer: 转换器(Transformer)是对获得的消息进行一定的转换处理(如数据格式转换)

(10) Bridge: 使用连接桥(Bridge)可以简单的将两个消息通道连接起来

Spring Integration是一个基于Spring框架的集成框架,它提供了一种轻量级的、可扩展的、易于使用的方式,用于在应用程序中集成异构系统和技术。 下面是Spring Integration入门的步骤: 1. 添加依赖:在Maven或Gradle项目中添加Spring Integration的依赖。 2. 配置Spring Integration:在Spring配置文件中配置Spring Integration的组件,如消息通道、消息转换器、消息适配器等。 3. 创建消息处理流程:将各种消息处理器(包括过滤器、路由器、转换器等)按照一定顺序组合成消息处理流程。 4. 启动Spring Integration:通过启动Spring容器来启动Spring Integration。 以下是一个简单的Spring Integration示例: 1. 添加依赖: ``` <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-core</artifactId> <version>5.1.3.RELEASE</version> </dependency> ``` 2. 配置Spring Integration: ``` <bean id="inputChannel" class="org.springframework.integration.channel.DirectChannel"/> <bean id="outputChannel" class="org.springframework.integration.channel.DirectChannel"/> <bean id="messageConverter" class="org.springframework.integration.json.ObjectToJsonTransformer"/> <bean id="messageHandler" class="org.springframework.integration.handler.LoggingHandler"/> <int:channel id="inputChannel"/> <int:channel id="outputChannel"/> <int:transformer input-channel="inputChannel" output-channel="outputChannel" ref="messageConverter"/> <int:logging-channel-adapter id="logger" log-full-message="true" level="INFO" channel="outputChannel"/> ``` 3. 创建消息处理流程: ``` <int:gateway id="gateway" service-interface="com.example.MyGateway" default-request-channel="inputChannel"/> <int:chain input-channel="inputChannel" output-channel="outputChannel"> <int:filter expression="payload != 'ignore'"/> <int:router expression="payload"/> <int:transformer expression="'Hello, ' + payload"/> <int:service-activator ref="messageHandler"/> </int:chain> ``` 4. 启动Spring Integration: ``` ApplicationContext context = new ClassPathXmlApplicationContext("spring-integration.xml"); MyGateway gateway = context.getBean(MyGateway.class); String result = gateway.process("World"); System.out.println(result); ``` 这是一个简单的Spring Integration示例,它演示了如何创建一个消息处理流程,并启动Spring Integration来处理消息。如果你想深入了解Spring Integration,可以参考官方文档或者相关书籍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值