Tuesday, May 19, 2009

MassTransit Publishers

[Intro]

So we feel we have something that the world needs to know about, we have messages to publish. This is what kicks off the events that make up the Pub/Sub system. The IT division have told you they are sick of modifying the HR application to call a growing number of web service to let those services know about  new or updated employee information. You decide this may be a good candidate for some Pub/Sub love. We will start with New Employees, firstly we would need to create a suitable message to publish, say "NewEmployeeNotificationMessage". This has all the relevant info in the message. As part of the creation process all we need to do is create a message of the given type and publish it.

var message = CreateNewEmployeeMessage();
_serviceBus.Publish(message);


That is it. Well.... its not, but as far as the publishing code goes that all there is too it, there is a little bit of infrastructure set up that goes on at start up, but to publish a message is really that simple.



There are times where you may want to know of a response if a subscriber sends one, this can be done by setting a response address in a delegate as part of the publish eg:



_bus.Publish(message, x=> x.SetResponseAddress(_bus.Endpoint.Uri));


If a response is expected then the service publishing the message should also be a consumer of the response message type, see the consumers post



The bus is a MassTransit.IServiceBus that is injected in to the service. We will cover setting up the bus later on in the series.



*this may be a bit over the top example. If you a re building enterprise wide service and integrating system perhaps MT is a little too light weight, judge for yourself. Personally I am angling at using for intra component messaging.

2 comments:

Lee Campbell said...

Being pedantic about naming again, but would "EmployeeCreatedMessage" or "EmployeeCreatedNotificationMessage" be better than "NewEmployeeNotificationMessage".

I imagine some naming conventions could go a long way here:
"Request.." prefix for publishing request eg RequestCreateCustomerMessage

Maybe like the above, suffix with "Notification" where it is not so much a command request but a completion

Unknown said...

Jeez mate you know these post go out at 1 am! the naming is bad so people doesnt steal my code, i cant show all my cards! ;)