0

I have two applications communicating with each other through a RabbitMQ virtual host, using Masstransit as an abstraction layer.

I am trying to create a third application which logs all the published messages to this virtual host, while, not knowing up front what these message types are.

If I knew the messages up front, it would be easy; I would just create some consumers. Unfortunate this is not the case.

I tried the Observers, but they only seem to work if the observed bus is the same as the one which sends the messages. So this wouldn't work cross-application.

From the rabbit's docs I found this would be easy by using rabbit MQ bindings, but, this does not seem to be supported: Publish message using exchange and routing key using MassTransit

I also tried: How to log all Rabbit MQ messages?, but it seems a dead-end as well because I want to log the data in the custom format to a database.

It seems I am missing a trivial thing, but after searching whole day, I haven't found the desired result. Can you get me on the right track?

For bus configuration, in all 3 applications I am using the straight forward configuration pattern:

 _bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
NinjaDeveloper
  • 1,620
  • 3
  • 19
  • 51
Stefan
  • 17,448
  • 11
  • 60
  • 79

1 Answers1

1

If you know the queue names for the consuming services, you could create a new service with a new queue that would explicitly specify a binding to the exchange name of the other service queues. This would deliver a copy of every message delivered to that service to the new service.

The service could then consume the JToken, which would deliver the JSON body of the message to the consumer. By doing this, every message could then be translated/stored however you like.

class LogConsumer : IConsumer<JToken> {...}

Then, create the bindings in your receive endpoint:

cfg.ReceiveEndpoint(host, "log-queue", ep =>
{
    ep.Bind("service1-queue");
    ep.Bind("service2-queue");
    ep.Consumer<LogConsumer>();
}

This should be enough to get you started!

Stefan
  • 17,448
  • 11
  • 60
  • 79
Chris Patterson
  • 28,659
  • 3
  • 47
  • 59
  • Ah, wow, I'll give it a try. I also don't know the queues in advance but maybe I find a way to query them. Btw, I was looking into the `Bind` function before, but couldn't find it in this overload: `cfg.ReceiveEndpoint("logger", e => ...`);`. but I now see that the signature and type of `ep` is different in this overload. – Stefan Jan 02 '18 at 16:39