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注解。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM