Reputation: 29300
I'm trying to use RabbitMq with spring WebSocketMessageBroker
, across distributed microservices.
The setup I'm working with is
Within the WebSocketMessageBroker, I'm using the following config, taken from the docs:
@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/push").setAllowedOrigins("*").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableStompBrokerRelay("/queue/", "/topic/", "/app");
registry.setApplicationDestinationPrefixes("/app");
registry.setPathMatcher(new AntPathMatcher("."));
}
}
Given this config, what exchange / queue should MyMicroservice
publish to in order for the message to be proxied through to the Stomp service?
I've tried the following (on the publishing side -- within MyMicroservice)
@Configuration
@SpringBootApplication
@EnableRabbit
public class Config {
public static final String WEB_QUEUE = "/topic/myNotificationTopic";
public static final String WEB_EXCHANGE = "web.exchange";
@Bean
Queue webQueue() {
return new Queue(WEB_QUEUE, false);
}
@Bean
TopicExchange webExchange() {
return new TopicExchange(WEB_EXCHANGE);
}
@Bean
Binding binding(Queue webQueue, TopicExchange webExchange) {
return BindingBuilder.bind(webQueue).to(webExchange).with(WEB_QUEUE);
}
}
@Component
public class ExamplePublisher {
@Autowired
private AmqpTemplate amqpTemplate;
public void sendMessage() {
amqpTemplate.convertAndSend(Config.WEB_QUEUE, "Hello, world");
}
}
However, the message doesn't appear to be proxied over the WebSocket connection.
To be clear, my questions are:
Upvotes: 2
Views: 978
Reputation: 121552
It isn't clear by your explanation why you are going to use Spring AMQP for the STOMP interaction, although it is possible anyway.
I'd suggest to take a look to the StompClient
support in the Spring Messaging, if you are going to send STOMP messages to the target destination directly from Java.
With the Spring AMQP (or just AMQP protocol) you should follow some RabbitMQ STOMP Adapter rules:
Topic Destinations
For simple topic destinations which deliver a copy of each message to all active subscribers, destinations of the form
/topic/<name>
can be used. Topic destinations support all the routing patterns of AMQP topic exchanges.Messages sent to a topic destination that has no active subscribers are simply discarded.
AMQP 0-9-1 Semantics
For
SEND
frames, the message is sent to theamq.topic
exchange with the routing key<name>
.For
SUBSCRIBE
frames, an autodeleted, non-durable queue is created and bound to theamq.topic
exchange with routing key<name>
. A subscription is created against the queue.
Pay attention that you should have subscription
first anyway, otherwise your messages will be lost without subscribers.
Upvotes: 2