Spring Websocket: How intercept a message sent by SimpMessagingTemplate through any either convertAndSend or convertAndSendToUser methods?

Spring Websocket: How intercept a message sent by SimpMessagingTemplate through any either convertAndSend or convertAndSendToUser methods?



For Spring Websocket


Spring Websocket



I have the following and works fine:


private final SimpMessagingTemplate simpMessagingTemplate;

...

@Scheduled(cron="some expression")
public void sendNotification()
logger.info("sendNotification ...");

simpMessagingTemplate.convertAndSend(
"/topic/something",
new Notification(...));

logger.info("... sendNotification");



The code works fine. All the clients subscribed to that Topic, (I am working with ActiveMQ) are able to see the message(s) sent.


Topic


ActiveMQ



A custom class


@Component
class MessageChannelInterceptorAdapter extends ChannelInterceptorAdapter {



With all the methods @Override was created to interception purposes.


@Override



This MessageChannelInterceptorAdapter class is registered about infrastructure as follows:


MessageChannelInterceptorAdapter


@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

@Autowired
@Qualifier("messageChannelInterceptorAdapter")
private ChannelInterceptorAdapter messageChannelInterceptorAdapter;

...

@Override
public void configureClientInboundChannel(ChannelRegistration registration)
registration.interceptors(messageChannelInterceptorAdapter);
WebSocketMessageBrokerConfigurer.super.configureClientInboundChannel(registration);


@Override
public void configureClientOutboundChannel(ChannelRegistration registration)
registration.interceptors(messageChannelInterceptorAdapter);
WebSocketMessageBrokerConfigurer.super.configureClientOutboundChannel(registration);


...



Even when the app works fine. The messages sent through SimpMessagingTemplate never are intercepted by MessageChannelInterceptorAdapter.


SimpMessagingTemplate


MessageChannelInterceptorAdapter



Note: the MessageChannelInterceptorAdapter class works fine to intercept Stomp events but again not for SimpMessagingTemplate


MessageChannelInterceptorAdapter


Stomp


SimpMessagingTemplate



Thus: How intercept any message sent by SimpMessagingTemplate through either convertAndSend or convertAndSendToUser methods?


SimpMessagingTemplate


convertAndSend


convertAndSendToUser



What is missing? missing configuration about infrastructure or create other class extending a specific Spring class.





Ever make any progress?
– Jeef
Aug 26 at 1:01





@Jeef Yes, about SimpMessagingTemplate, the interceptor only works how is expected only when exists a client listening the Destination. Because it is Topic in my case and there is no consumers, the message is sent but is lost and the interceptor never works.
– Manuel Jordan
Aug 26 at 15:41



SimpMessagingTemplate


Destination


Topic




1 Answer
1



So as far as I can tell there isn't an easy way to do this. I had a situation where I was calling the template directly (like you). When I migrated to use an external STOMP broker I found out suddenly some of my calls were no longer valid:



/topic/sddf/traffic is no longer a valid destination when you use Rabbit


/topic/sddf/traffic



I looked into writing my own version of the class SimpMessagingTemplate which became too difficultly - so instead I had to manually change all the calls.


SimpMessagingTemplate



My example is in Kotlin not Java



My solution was to go with this class:


@Component
@Configurable
class MessagingTemplateWrapper

var template: SimpMessagingTemplate = ApplicationContextHolder.getContext().getBean(SimpMessagingTemplate::class.java)
private val log = LoggerFactory.getLogger(BridgeDataListener::class.java)

fun convertAndSend(dest: String, data: Any)

val rabbitDestination = dest.substring(0,11) + dest.substring(11).replace("/",".")
template.convertAndSend(rabbitDestination, data)




And anywhere where I was using the template I did this:


// var template: SimpMessagingTemplate = ApplicationContextHolder.getContext().getBean(SimpMessagingTemplate::class.java)
var template: MessagingTemplateWrapper = ApplicationContextHolder.getContext().getBean(MessagingTemplateWrapper::class.java)



So I just had a wrapper to do (maybe) what you wanted.



I don't love the solution - but it does work.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế