spring源码分析之spring-messaging模块详解
0 概述
spring-messaging模塊為集成messaging api和消息協(xié)議提供支持。
其代碼結構為:
其中base定義了消息Message(MessageHeader和body)、消息處理MessageHandler、發(fā)送消息MessageChannel。
1. base模塊
其結構如下所示:
其中:
message由兩部分組成,
MessageHandler是一個處理消息的約定,spring messaging提供了豐富的消息處理方式。
MessageChannel表現(xiàn)為pipes-and-filters架構的管道。
2. 轉(zhuǎn)換器converter模塊
? ? 對消息轉(zhuǎn)換提供支持。其結構如下:
從上圖可以看出,有消息到string、json、byte數(shù)組之間的相互轉(zhuǎn)換。
3. 核心core模塊
核心模塊提供消息的模板方法,其結構如下:
4. 處理handler模塊
?
?大致架構如下:
?
其中,
HandlerMethod封裝了一個bean的方法相關信息(getMethod()和getBean()方法),提供了訪問方法參數(shù)的便利工具。HandlerMethod可以在bean factory中使用createWithResolvedBean獲取bean實例時獲取該實例。
MessageCondition是一個將conditions映射到message的約定。
HandlerMethodArgumentResolver 是一個解析方法參數(shù)到Context中指定Message的參數(shù)值的策略接口。
HandlerMethodReturnValueHandler是一個處理從觸發(fā)一個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協(xié)議的簡單消息協(xié)議的通用支持。
STOMP,Streaming Text Orientated Message Protocol,是流文本定向消息協(xié)議,是一種為MOM(Message Oriented Middleware,面向消息的中間件)設計的簡單文本協(xié)議。它提供了一個可互操作的連接格式,允許STOMP客戶端與任意STOMP消息代理(Broker)進行交互,類似于OpenWire(一種二進制協(xié)議)。由于其設計簡單,很容易開發(fā)客戶端,因此在多種語言和多種平臺上得到廣泛應用。其中最流行的STOMP消息代理是Apache ActiveMQ。
詳細協(xié)議內(nèi)容中文版本參見。另stomp架構如下:
? ? ? ? ? ? ? ? ? ? ? ?(spring 官方圖片)
6. support模塊
提供了Message的實現(xiàn),及創(chuàng)建消息的MessageBuilder和獲取消息頭的MessageHeaderAccessor,還有各種不同的MessageChannel實現(xiàn)和channel interceptor支持。
7. tcp模塊
? ?一方面提供了通過TcpOperations建立tcp connection、通過TcpConnectionHandler處理消息和通過TcpConnectionf發(fā)送消息的抽象及實現(xiàn);另一方面包含了對基于Reactor的tcp 消息支持。
8 小結
?Spring Framework 4 包含了一個新的spring-messaging?模塊,該模塊從使用了?Spring Integration?工程的核心概念如?Message,?MessageChannel,MessageHandler和別的一些可以作為消息架構的基礎組件。這個模塊同樣也提供了一些映射消息到方法的注解,類似于基于編程模型的spring mvc注解。
轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/4446796.html
總結
以上是生活随笔為你收集整理的spring源码分析之spring-messaging模块详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: STOMP协议规范--转载
- 下一篇: mysql --The MEMORY S