vantheshark / Burrow.NET

Burrow.NET is a simple library created based on some EasyNetQ ideas, it's a thin wrapper of RabbitMQ.Client for .NET. Basically, if you just need to put your message or subscribe messages from RabbitMQ server, you found the right place. With Burrow.NET, you can easily customize almost everything start with exchange and queue name, changing the way to serialize your object, inject custom error handling strategies, etc.
https://github.com/vanthoainguyen/Burrow.NET/wiki/Get-started-with-Burrow.NET
63 stars 21 forks source link

Publish / Subscribe implementation #18

Open hmihail opened 9 years ago

hmihail commented 9 years ago

Hi,

I am pretty new with this, so please excuse me if my question is stupid.

I want to implement a publish / subscribe pattern with a class let's call it Order. I want to be able to have 2 subscriptions, based on the Order state, and also to be able to subscribe with more than one instance, and get all the messages, not round-robin them.

I've created an exchange, fanout, and bind two queues to it, one for new Orders and one for finished Orders. No routing key to the bindings.

Is this approach good, because I can't get it to work. Any ideas, maybe an example?

Thanks!

vantheshark commented 9 years ago

Fanout exchange doesn't seem to be a right choice for your case because whatever you publish to the exchange will be routed to those 2 queues. I would suggest you use Direct exchange and use different binding for different queue.

Direct exchange -> routing key = Order -> OrderQueue Direct exchange -> routing key = FinishedOrder -> FinishedOrderQueue.

These 2 messages can be different or from the same class. You can then subscribe to those 2 queues and process messages differently. I think you just need to implement a IRouteFinder and can use it with Burrow.NET.

http://thoai-nguyen.blogspot.com.au/2012/05/rabbitmq-exchange-queue-name-convention.html

Cheers

hmihail commented 9 years ago

Thanks! I will try that. But will this setup allow me to listen to FinishedOrdersQueue with more than one subscriber?

vantheshark commented 9 years ago

What do you really mean by "more than one subscriber"

If you mean multiple threads, Burrow.NET supports that by default, you can specify the number of threads when subscribe to the queue.

If you mean multiple instances of your code on different servers to subscribe to the same queue in order to share the load, then also YES because RabbitMQ allows you to subscribe that way.

Cheers

hmihail commented 9 years ago

What I want is to subscribe to the FinishedOrdersQueue with 2 applications, and both to receive each message. I just implemented your suggestion and it works but only one instance of the app gets the message.

vantheshark commented 9 years ago

Let's say you publish message type "FinishedOrdersQueue" to the exchange and eventually route to that queue. If you subscribe from that queue, your app will receive messages of that type. If there are multiple instances subscribe from that queue, they will receive messages by Round Robin fashion and I'm 100% sure about that. Could you try to puplish messages faster than they are being processed? Or simply stop the instance that is receiving messages to see what happen to the other instance? Also make sure there is not any conectivitiy problem!

hmihail commented 9 years ago

Yes, they are balanced between the subscribers, but I don't want that. I want all of the subscribers to get all the messages. Maybe the direct type of the exchange is not the best?

vantheshark commented 9 years ago

Ah ok, you want all subscribers to have the same amount of messages. Then I guess you can create another queue, bind to the same exchange with the same routing key, each instance then subscribe to different queue.

Try to use ConstantRouteFinder for those subscribers to subscribe to the expected queue. I think you can create the 2nd queue manually and bind it manually