简体   繁体   中英

What is the major difference between SimpMessageHeaderAccessor vs StompHeaderAccessor Spring + java

What is the major difference between SimpMessageHeaderAccessor vs StompHeaderAccessor Web Socket Spring?

Please explain elaborately if you know about this?

This has a simple answer: SimpMessageHeaderAccessor is the parent of StompHeaderAccessor .

That is, StompHeaderAccessor is used exclusively for STOMP headers while SimpMessageHeaderAccessor is used for all messaging protocol headers, including STOMP.


STOMP is one of the simpler types of messaging protocol - it's text based, making it a little similar to how HTTP works and allows for communication using multiple languages and platforms. But it cannot be used for queuing and topics.

Further reading:

  1. http://jmesnil.net/stomp-websocket/doc/
  2. https://blogs.vmware.com/vfabric/2013/02/choosing-your-messaging-protocol-amqp-mqtt-or-stomp.html

Its important to understand the STOMP protocol, its design and format to better understand the API and the classes related.

STOMP´s interoperable wire format allows clients to communicate with number of message brokers. Since its language agnostic, clients and brokers developed in different languages can send and receive messages to each other.

STOMP complient message brokers are here .

STOMP is text-based and does not use binary protocols. It supports a range of core enterprise messaging features, such as authentication, messaging models like P2P & publish and subscribe, message acknowledgement, transactions, message headers & properties, etc.,

So while you use Spring Messaging framework to build client and server side Messaging platforms and applications, the package org.springframework.messaging.simp has generic support for Simple message protocols, STOMP is ONE of them.

As a base class of StompHeaderAccessor , can use SimpMessageHeaderAccessor for generic message header properties and use StompHeaderAccessor for STOMP specific properties like ContentLength , createForHeartbeat() , isHeartbeat() etc., and STOMP messaging API specific methods like updateStompCommandAsClientMessage() , updateStompCommandAsServerMessage() , wrap(Message<?> message) .

Good examples on using StompHeaderAccessor can be found here . A good working example of using both SimpMessageHeaderAccessor and StompHeaderAccessor is available in this blog .

I have never worked with Web Socket Spring but I will try to help as best I can.

SimpMessageHeaderAccessor: A base class for working with message headers in simple messaging protocols that support basic messaging patterns. Provides uniform access to specific values common across protocols such as a destination, message type (eg publish, subscribe, etc), session id, and others. Use one of the static factory methods in this class, then call getters and setters, and at the end if necessary call MessageHeaderAccessor.toMap() to obtain the updated headers.

StompHeaderAccessor: A MessageHeaderAccessor to use when creating a Message from a decoded STOMP frame, or when encoding a Message to a STOMP frame. When created from STOMP frame content, the actual STOMP headers are stored in the native header sub-map managed by the parent class NativeMessageHeaderAccessor while the parent class SimpMessageHeaderAccessor manages common processing headers some of which are based on STOMP headers (eg destination, content-type, etc).

An instance of this class can also be created by wrapping an existing Message. That message may have been created with the more generic SimpMessageHeaderAccessor in which case STOMP headers are created from common processing headers. In this case, it is also necessary to invoke either updateStompCommandAsClientMessage() or updateStompCommandAsServerMessage() if sending a message and depending on whether a message is sent to a client or the message broker.

What I can make out of all this is that HeaderAccessor of any type it seems is used to create a Message from a decoded message frame, furthermore, it is used to encode a message to a protocol frame, STOMP or otherwise. They do not appear to be that different from HTTP headers in that you can add parameters to them in the following way.

SimpMessageHeaderAccessor accessor = 
SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
    accessor.setHeader("foo", "bar");
    accessor.setNativeHeader("fooNative", "barNative");
    accessor.setLeaveMutable(true);

    MessageHeaders headers = accessor.getMessageHeaders();

More information on WebSockets can be found here: From Zero to Hero with Spring WebSocket or Deep dive into Spring WebSockets

I hope that you were helped by my explanation and have a good day.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM