spring源码分析之spring-messaging模块详解

0 概述

spring-messaging模块为集成messaging api和消息协议提供支持。

其代码结构为:

其中base定义了消息Message(MessageHeader和body)、消息处理MessageHandler、发送消息MessageChannel。

1. base模块

其结构如下所示:

其中:

message由两部分组成,

MessageHandler是一个处理消息的约定,spring messaging提供了丰富的消息处理方式。

MessageChannel表现为pipes-and-filters架构的管道。

2. 转换器converter模块

    对消息转换提供支持。其结构如下:

从上图可以看出,有消息到string、json、byte数组之间的相互转换。

3. 核心core模块

核心模块提供消息的模板方法,其结构如下:

4. 处理handler模块

 

 大致架构如下:

 

其中,

HandlerMethod封装了一个bean的方法相关信息(getMethod()和getBean()方法),提供了访问方法参数的便利工具。HandlerMethod可以在bean factory中使用createWithResolvedBean获取bean实例时获取该实例。

MessageCondition是一个将conditions映射到message的约定。

HandlerMethodArgumentResolver 是一个解析方法参数到Context中指定Message的参数值的策略接口。

HandlerMethodReturnValueHandler是一个处理从触发一个Message的method Handling返回值的策略接口。

另外,也提供了部分注解:

@interface Header:Annotation which indicates that a method parameter should be bound to a message header.

@interface Headers:Annotation which indicates that a method parameter should be bound to the headers of a message. The annotated parameter must be   assignable to {@link java.util.Map} with String keys and Object values.

@interface MessageExceptionHandler: Annotation for handling exceptions thrown from message-handling methods within a  specific handler class.

@interface MessageMapping:Annotation for mapping a {@link Message} onto message-handling methods by matching to the message destination.

@interface Payload:Annotation that binds a method parameter to the payload of a message. The payload may be passed through a {@link   MessageConverter} to convert it from serialized form with specific MIME type to an Object matching the target method parameter.

@interface SendTo:Annotation that indicates a method's return value should be converted to a {@link Message} and sent to the specified destination.

5.Simp模块

包含诸如STOMP协议的简单消息协议的通用支持。

STOMP,Streaming Text Orientated Message Protocol,是流文本定向消息协议,是一种为MOM(Message Oriented Middleware,面向消息的中间件)设计的简单文本协议。它提供了一个可互操作的连接格式,允许STOMP客户端与任意STOMP消息代理(Broker)进行交互,类似于OpenWire(一种二进制协议)。由于其设计简单,很容易开发客户端,因此在多种语言和多种平台上得到广泛应用。其中最流行的STOMP消息代理是Apache ActiveMQ。
详细协议内容中文版本参见。另stomp架构如下:

                       (spring 官方图片)

6. support模块

提供了Message的实现,及创建消息的MessageBuilder和获取消息头的MessageHeaderAccessor,还有各种不同的MessageChannel实现和channel interceptor支持。

7. tcp模块

   一方面提供了通过TcpOperations建立tcp connection、通过TcpConnectionHandler处理消息和通过TcpConnectionf发送消息的抽象及实现;另一方面包含了对基于Reactor的tcp 消息支持。

8 小结

 Spring Framework 4 包含了一个新的spring-messaging 模块,该模块从使用了 Spring Integration 工程的核心概念如 MessageMessageChannel,MessageHandler和别的一些可以作为消息架构的基础组件。这个模块同样也提供了一些映射消息到方法的注解,类似于基于编程模型的spring mvc注解。

Spring Boot Messaging: Messaging APIs for Enterprise and Integration Solutions by Felipe Gutierrez English | 5 Jun. 2017 | ISBN: 1484212258 | 196 Pages | PDF | 6.36 MB Build messaging applications using the power of Spring Boot; use Spring application events over the Web; use WebSocket, SockJS, and STOMP messaging with Spring MVC; and use Spring JMS, Redis Pub/Sub and Spring AMQP for reliable messaging solutions. This book covers all the Spring Messaging APIs using Spring Boot. Written by a Pivotal engineer, Spring Boot Messaging is an authoritative guide to the many messaging APIs and how to use these for creating enterprise and integration solutions. You will learn and integrate these messaging APIs with more complex enterprise and cloud applications: for example, you will see how to use Spring Cloud Stream for creating message-driven and cloud native microservices. In addition, you’ll discover the new Spring Integration DSL and use it with Spring Cloud Stream to build integration solutions using every enterprise integration pattern. Finally, you’ll see Spring Reactor and Spring Cloud to take your application to the next level. After reading this book, you will come away with a case study application walk-through and will be able to use it as a template for building your own Spring messaging applications or messaging features within your enterprise or cloud application. What You'll Learn Use the main Spring messaging APIs with Spring Framework 5 Build messaging applications over the Web Use WebSocket, SockJS, and STOMP messaging Integrate Spring JMS and Spring AMQP into your applications Work with Spring Cloud Stream and microservices Who This Book Is For Enterprise Java developers who have at least some previous experience with the Spring Framework and/or the Spring platform.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值