RabbitMQ - Publish to direct exchange with RoutingKey

2,018 views
Skip to first unread message

Robert Slaney

unread,
Sep 10, 2017, 10:50:34 PM9/10/17
to masstransit-discuss
Hi all,

I have an application that needs to publish an update notification to 1 or more interested parties.  The information contains sensitive data that needs to be encrypted per party so each party get's its own message.

I would like to utilise a single Direct exchange to allow me to publish a message using a routing key so that I can configure which party each message get sent to.


Each destination party may not be on the same VHost so I will be setting up shoveling.

.. bare with me as I describe my setup and findings ...

I have set up IPipe<PublishContext<T>> facade to add a routing Key to the RabbitMqSendContext *cough cough* via reflection in the Send(PublishContext<T>) method.

I created a Direct exchange with the appropriate name ( full name of contract type ) and bound 2 shovels with a different route key value each.

When I first sent the message MT created tried to create a fanout exchange with the full name of the contract, it threw an exception because the Rabbit exchange was already created with a direct exchange type.

I deleted the exchange and set it up again using a fanout and re-attached the shovels.

This time MQ successfully published the message to the exchange but each shovel got a copy of the message even though the routing key did not match 1 shovel.

It appears a fanout exchange will ignore the routing key when distributing to bound queues.

I create another direct exchange and then added another shovel between the fanout and direct exchanges, changing the first 2 shovels sources to this new exchange.  Now when I publish a message to the fanout exchange it ends up in the direct exchange, finally moved onto only the correct final queue.


--- Question ---
Is there a way to set the routing key when I publish a message without using reflection?  I can't find any hooks to allow me to get access to the RabbitMQ send context directly.
Can I get MT get create a direct exchange instead of a fanout, or event allow me to intercept the create process ?  If not, is there an API that allows me to create a direct exchange manually when MT creates the original fanout exchange (like an observers) ?


Cheers...

Rob



Robert Slaney

unread,
Jan 2, 2018, 5:32:53 PM1/2/18
to masstransit-discuss
Any thoughts on this ?

Chris Patterson

unread,
Jan 2, 2018, 8:42:26 PM1/2/18
to masstrans...@googlegroups.com
With MT 4.0 you can set it using a convention for each message type.

cfg => cfg.Publish<MyEvent>(x => x.SetRoutingKey(m => m.MessagePropery));

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstransit-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/9129b617-6e63-4bb9-a6c3-53b7de356c3e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Chris Patterson

unread,
Jan 2, 2018, 8:47:21 PM1/2/18
to masstrans...@googlegroups.com
Sorry, I totally got that wrong.

It's:

cfg.Send<MyEvent>(x => x.UseRoutingKeyFormatter(context => context.Message.SomePropertyThatIsYourRoutingKey));

And any message published/sent will use that convention.


To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsubscribe...@googlegroups.com.

To post to this group, send email to masstransit-discuss@googlegroups.com.

Robert Slaney

unread,
Feb 6, 2018, 3:20:06 AM2/6/18
to masstransit-discuss
Thanks for that.  We are currently on v3.5.7, but plan to upgrade to v4.x soon.

but I'll try it in our .NET Core prototype.

Am I right in assuming I still cannot get MT to create a direct exchange in RabbitMQ with v4.x, and that fanout is the only supported topography?

Chris Patterson

unread,
Feb 7, 2018, 9:13:13 PM2/7/18
to masstrans...@googlegroups.com
For publish, I believe you can set the exchange type, as well as for consume (receive endpoints). You can also set routingKey for receive endpoints, there is documentation on the web site showing how to do it.


--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

To post to this group, send email to masstransit-discuss@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages