Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ - Send message to a particular consumer in a queue

This is the scenario - There are multiple app servers. Browser can connect via websocket to any app server.

The app servers (consumers) are all listening on a particular queue. As soon as a web socket connection is received, the particular app server binds the queue with a routing key {userId} to a direct exchange.

I want a message sent to the direct exchange with the routing key {userId} to be received by only the particular app server where the binding has occured.

Is a direct exchange the right exchange to use in this case? Or should some other type of exchange be used?

I'm using spring-amqp to create dynamic bindings when a websocket comes in

// create the RabbitMq queue and bind to it
String routingKey = MessageConstants.getRoutingKeyForUserRecommendationQueue(user);
Binding userRecommendationBinding = BindingBuilder.bind(userRecommendationsQueue).
    to(directExchange).with(routingKey);
amqpAdmin.declareBinding(userRecommendationBinding);
like image 783
nsdiv Avatar asked Dec 24 '22 08:12

nsdiv


1 Answers

Send message to a particular consumer in a queue

this is not possible. any consumer connected to a queue has a chance of consuming any given message in the queue

I want a message sent to the direct exchange with the routing key {userId} to be received by only the particular app server where the binding has occured.

you can do this by creating exclusive / autoDelete queues for your consumer, with a binding that directs all messages for that consumer to that queue.

Is a direct exchange the right exchange to use in this case?

either a direct exchange or a topic exchange is fine. direct exchange is slightly easier to understand, but topic exchange is more flexible

like image 185
Derick Bailey Avatar answered Feb 23 '23 17:02

Derick Bailey