0

I'm beginning my exploration of NServiceBus, RabbitMQ, MassTransit and EasyNetQ.

One detail I specifically want to understand is how to ensure a custom set of meta-data is always sent with every published message?

For example, regardless of what a message producer publishes, I may want to ensure that the following pieces of information are always sent as part of the message:

  • TimeStamp
  • Workflow Name
  • Priority

It doesn't really matter what the meta-data is, what is important is having the option or ability to forward along such arbitrary information.

I like the idea that the namespace and class name of the message sent by a producer is automatically translated into the "routing key" of the message. However, in order for additional meta-data to be attached to the message, it may be necessary to have some kind of "pre-publish" hook-point that allows the arbitrary message to be converted into the payload of a wrapper object, as such:

public class BaseMessage {
    //a couple fields of meta-data that every message should have.
    public string TimeStamp;
    public int Priority;

    //The payload is the real message that the consumer cares about
    //and can be any format (JSON, XML, etc).
    public string payload;
}

In this case, I don't want the "routing key" to contain either the name or namespace of the BaseMessage. The payload type, before it was serialized, is what that "routing key" should be based on.

Of course all of this also presupposes that the consumer has some convenient way to de-serialize both the BaseMessage and the payload.

So which of the tools I listed supports this kind of need? Please show example syntax where applicable.

Brent Arias
  • 29,277
  • 40
  • 133
  • 234

2 Answers2

0

If you require that the message is formatted in something like XML, that supports a strong schema, you could simply perform a schema validation before accepting the message at the receiver. Then, if you can assure the message is checked before sending, then you can improve performance by removing the receiver validation if it is necessary.

Pekka
  • 3,529
  • 27
  • 45
  • I like what you are saying, but your suggestion assumes that I write the validation code myself. What I'm trying to discover is if one of the tools I listed provides the capability for me instead. And, to be more precise, it is not actually "validation" I'm seeking. I'm looking for an AOP behavior or "decorator" pattern that allows me to manipulate the producer's message before it goes on the wire; I'd like to know if one of the aforementioned tools supports that. – Brent Arias Jan 31 '15 at 01:24
  • The requirement is largely orthogonal to the transmission of messages and the developers may have specifically excluded this to permit the user to layer their own preference. The NServiceBus example provided by @Andreas-Öhlund is an example. The same reference contains the [ValidationMessageMutator](http://docs.particular.net/samples/messagemutators/#validationmessagemutator) you require. – Pekka Jan 31 '15 at 09:50
0

In NServiceBus you would use a message mutator to make sure all messages sent will have the required data

http://docs.particular.net/samples/messagemutators/#transportmessagecompressionmutator

I would also recommend using headers to transmitt this data to avoid "poluting" your business data contracts with infrastructure concerns

Andreas Öhlund
  • 5,263
  • 20
  • 24