RabbitMQ-- Selectively receive messages from the queue

I am new to RabbitMQ and wondered about a good approach to this problem that I am considering. I want to create a service that subscribes to a queue, and only pulls messages that meet certain criteria; for example, if the message contains a specific subject header.

I am still learning about RabbitMQ and have been looking for tips on how to approach this. My questions include: how can a consumer get only certain messages out of a queue? How can a producer set a subject title in a message (if that’s even the right term?)

+11
source share
2 answers

RabbitMQ is perfect for this situation. You have several options to do what you want. I suggest reading the documentation to better understand. I suggest you use a topic or direct exchange. The topic is more flexible. This happens as follows.

The manufacturer code connects to Broker RabbitMQ and creates Exchange with a specific name.

Producer publishes for sharing. Each published message will be published using a routing key.

The consumer connects to the RabbitMQ broker.

Consumer creates a queue

The consumer binds the queue to the exchange, the same exchange defined in the producer. Binding also includes routing keys for each message required for that particular consumer.

Suppose you posted a journal post. The routing key can be something like "log.info", "log.warn", "log.error". Each message published by the manufacturer will have a corresponding routing key. Then you will have a consumer who sends and sends all error messages by email, and the other writes all error messages to a file. Thus, the mail client will determine the binding of its queue to the exchange using the routing key "log.error". Thus, although the exchange receives all messages, the queue defined for the mail sender will only contain error messages. Filelogger will detect a new separate queue associated with the same exchange and configure a different routing key. You can make three separate bindings for three different routing keys or simply use the "log. *" Template to request all messages from the exchange, starting with the log.

This is a simple example that shows how you can achieve what you want.

look here for code examples, in particular, tutorial number 5.

+21
source
1. To Retrive Message from RabbitMQ we need to first connect with RabbitMQ server public WebClient GetRabbitMqConnection(string userName, string password) { var client = new WebClient(); client.Credentials = new NetworkCredential(userName, password); return client; } 2. Now retrieve message from RabbitMQ using below code. public string GetRabbitMQMessages(string domainName, string port, string queueName, string virtualHost, WebClient client, string methodType) { string messageResult = string.Empty; string strUri = "http://" + domainName + ":" + port + "/api/queues/" + virtualHost + "/"; var data = client.DownloadString(strUri + queueName + "/"); var queueInfo = JsonConvert.DeserializeObject<QueueInfo>(data); if (queueInfo == null || queueInfo.messages == 0) return string.Empty; if (methodType == "POST") { string postbody = " {\"ackmode\":\"ack_requeue_true\",\"count\": \"$totalMessageCount\",\"name\":\"${DomainName}\", \"requeue\":\"false\",\"encoding\":\"auto\",\"vhost\" : \"${QueueName}\"}"; postbody = postbody.Replace("$totalMessageCount", queueInfo.messages.ToString()).Replace("${DomainName}", domainName).Replace("${QueueName}", queueName); messageResult = client.UploadString(strUri + queueName + "/get", "POST", postbody); } return messageResult; } I think this will help you to implement RabbitMQ. 
0
source

All Articles