Using EasyNetQ with RabbitMQ to publish and receive messages
Asked Answered
P

2

8

I'm new to messaging, and currently investigating using RabbitMQ as part of our system architecture to provide messaging between different services. I've got a basic RabbitMQ example working and it can transmit a basic text message over the bus. It looks like EasyNetQ could simply some of the complexity of using RabbitMQ, though I'm having a little trouble getting it working.

Instead of just a string, I'd like to send a more advanced 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'm trying to send this by publishing it to the queue, and then have the subscriber pick it up off 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 connect, and the publisher reports that the message was published:

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 logs the following:

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

It looks like the subscriber is either not connecting to a queue (or the correct queue), or there is something else I need to do to actually receive the message?

Prestidigitation answered 28/2, 2012 at 16:44 Comment(0)
C
19

Sorry Mun, I'm the main author of EasyNetQ, I've only just seen this. Your problem is that you are disposing of the bus as soon as you've done the subscription, so you immediately stop listening as soon as you've subscribed. Create the bus when your app starts up, dispose 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 address Simon's point. He is right, EasyNetQ is intentionally designed to only provide a subset of RabbitMQ's capabilities in order to simplify the API. We currently provide two patterns: pub/sub and request/response. Expect to see topic based routing soon.

Charlottetown answered 24/4, 2012 at 8:8 Comment(1)
Thanks Mike. Ended up creating a custom wrapper to serve the needs of our project, but will keep an eye on EasyNetQ for any updates.Prestidigitation
C
2

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

Be careful about the order as an Exchange will not hold a copy of a message sent to it; merely pass it to the queues bound to it (or other exchanges in later versions) so if you send a message to an exchange and the create a receiver - which creates a temp queue and binds this queue to the exchange - its possible the message has already been processed - RabbitMQ is very fast (just thinking out loud)

EasyNetQ is a look piece of work butthe not dealing with ACK messages might be an issue for some type of app. RabbitMQ, unlike others, supports more models than pub/sub so using EasyNetQ will limit you - which might be an issue depends on your app etc.

Thanks

Simon

Curzon answered 28/2, 2012 at 19:51 Comment(1)
Yep, the management add on is very useful and definitely essential when doing anything with RabbitMQ.Prestidigitation

© 2022 - 2024 — McMap. All rights reserved.