Using EasyNetQ with RabbitMQ to Publish and Receive Messages

I am new to messaging and am currently learning using RabbitMQ as part of our system architecture for messaging between different services. I have a basic RabbitMQ example , and it can send the main text message on the bus. It seems like EasyNetQ may just have some difficulty using RabbitMQ, although I find it a little difficult to get it working.

Instead of a simple line, I would like to send a more extended message, represented by the following class:

public class Message { public string Text { get; set; } public int RandomNumber { get; set; } public DateTime Date { get; set; } } 

I try to send this by posting it to the queue and then starting the subscriber from the queue. My code is as follows:

Publisher

 using (var bus = RabbitHutch.CreateBus("host=localhost")) { var message = new Message() { Text = "Hello World", RandomNumber = new Random().Next(1,100), Date = DateTime.Now }; bus.Publish<Message>(message); } 

Receiver

 using (var bus = RabbitHutch.CreateBus("host=localhost")) { bus.Subscribe<Message>("test", m => Console.WriteLine(string.Format("Text: {0}, RandomNumber: {1}, Date: {2}", m.Text, m.RandomNumber, m.Date))); } 

Both sides seem to be connecting, and the publisher reports that the message was posted:

 DEBUG: Trying to connect INFO: Connected to RabbitMQ. Broker: 'localhost', VHost: '/' DEBUG: Published UserQuery+Message:query_lzzfst, CorrelationId ec81fc89-4d60-4a8b-8ba2-7a6d0818d2ed 

The subscriber registers the following:

 DEBUG: Trying to connect INFO: Connected to RabbitMQ. Broker: 'localhost', VHost: '/' 

It seems that the subscriber either does not connect to the queue (or the correct queue), or is there something else I need to do to really get the message?

+6
source share
3 answers

Sorry, Moon, I’m the main author of EasyNetQ, I just saw this. Your problem is that you take away the bus as soon as you sign up, and immediately stop listening as soon as you sign up. Create a bus when your application starts, dispose of it when it shuts down. Why you should do this is explained in the docs here: https://github.com/mikehadlow/EasyNetQ/wiki/Connecting-to-RabbitMQ

To set Simon's point. He's right, EasyNetQ is only intended to provide a subset of the capabilities of RabbitMQ to simplify the API. We currently provide two templates: pub / sub and request / response. Expect a speedy transition to a thematic framework.

+17
source

RabbitMQ has a management add-in (http://www.rabbitmq.com/management.html), which is necessary when working with a rabbit: it will show you the exchanges and queues and the client that are connected. Thus, you should be able to see if the receiver is connected to the queue.

Be careful with the order, as Exchange will not keep a copy of the message sent to it; just pass it to the queues attached to it (or other exchanges in later versions), therefore, if you send a message to the exchange and create a receiver that creates a temporary queue and associates this queue with the exchange, the message may have already been processed - RabbitMQ is very fast (just out loud)

EasyNetQ is a piece of work, but without dealing with ACK messages, it can be a problem for some applications. RabbitMQ, unlike others, supports more models than pub / sub, so using EasyNetQ will limit you - this can be a problem, it depends on your application, etc.

thanks

Simon

+2
source

I also started playing with rabbitMQ and easynetQ, they are fantastic products. Mike Hadlow is right about making the bus a single. I created a sample project to reflect this here

The MXRabbitBus object in my code is also for use in a web application.

-3
source

Source: https://habr.com/ru/post/909563/


All Articles