Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use SimpMessagingTemplate without creating a web socket message broker Spring 4

Can I send a message to a message broker using SimpMessagingTemplate#convertAndSendToUser or SimpMessagingTemplate#convertAndSend methods without settings up a websocket message broker using @EnableWebSocketMessageBroker?

What I'm trying to do is utilise one websocket server to provide messaging for two application server instances(One spring 4 and one Spring 3). I created a one web server with Spring 4, Spring boot plus websocket message broker enabled.

Now I want two application servers to push messages to rabbitmq so it will broadcast them to clients subscribed to it.

First issue I faced is if there is no websockt message broker configuration available, SimpMessagingTemplate will not get autowired to application context. I couldn't get it injected without creating a websocket message board either.

Please help me to find out whether this is possible.

BTW I have a previous question unanswered related to this.

like image 255
Susitha Ravinda Senarath Avatar asked Mar 04 '16 09:03

Susitha Ravinda Senarath


Video Answer


1 Answers

Well, After reading lots of documentation I found the answer myself. The key thing is this architecture is following.

enter image description here

In this architecture spring act as a gateway for communication between the message broker and client. Spring doesn't do anything(Other than when it necessary) but forward the request to the message broker(STOMP messages). The configuration kept on Spring defines couple of important things. One is the exchange and other were routing keys. Spring configuration gives us an abstract layer so we subscribe and push messages to message broker without a fuss.

SimpMessagingTemplate is the abstract layer which we use to communicate with message broker. Spring creates the bean using the given details. Well I couldn't create a instance of SimpMessagingTemplate manually. I have to update Spring 3 application to Spring 4 in order to use websockets.

Since Spring and message broker is decoupled, clustering the application instance doesn't make any effect on message broker. Spring will communicate to message broker only when it need to subscribe to a channel or when it need to publish a message to a channel. So if there is two instances subscribing to same channel it would be two queues binding the one exchange using same routing key. Messages published into a channel will be available to all subscribers(queues) because they all use same routing key. Refer to rabbitmq stop plugin documentation for more elaborative description.

like image 98
Susitha Ravinda Senarath Avatar answered Sep 29 '22 22:09

Susitha Ravinda Senarath