A demo project i am frantically working on has a requirement for some sort of ESB like message control. My first instinct was to go with NServiceBus but something, for some reason drew me to
MassTransit.
MassTransit is an Open source .Net project run by
Chris Patterson and
Dru Sellars which aims to be a "lean service bus implementation for building loosely coupled applications using the .NET framework.". Cool cause that's what I need.
It sit on top of MSMQ and has some heavy hooks into the castle project for DI etc.
I am writing this while i wait for my VS2008 SP1 install to finish... what the hell is it that thing?? oh the entity framework! well at least its worth the wait ;P
Anyway the documentation is a bit slight and i am a documentation nerd. I like to get a synopsis of what we are trying to achieve and some sample project, also documented, to help me get up to speed, so here is my very basic version of just getting started.
You can just download the binaries if you are feeling very confident or alternatively you can get the source which includes some sample projects.
Running the MassTransit samples:
NB: you will need to install/set up MSMQ (or whatever queue they support in the future), for details on how to do this, see below.
1) Download the source code to an appropriate folder using the following svn command (this is read only)
svn checkout http://masstransit.googlecode.com/svn/trunk/ masstransit-read-only
2) For Mass Transit you will need to create a private MSMQ queue of the name "test_servicebus" and leave the default setting of NOT transactional.
3) Open up the WebRequestReply.sln found in :
..\masstransit-read-only\Samples\WebRequestReply\WebRequestReply.sln (or 2008 if you have it)
4) Build the solution, ensuring it builds (I had to delete a couple of version files for both 2005 and 2008)
5) Set the monorail project to the start up project.
6) F5 that mutha.
This will show a splash screen that links to the asynch screen. Type in some stuff in the request textbox and hit the button and the response will come up.
Things to look for in the code:
The monorail project is the initiating code here and basically is the stuff you will be looking at for how to use this framework. If you are not familiar with castle, monorail and asynch calls then your head may pop.
Firstly look at the DashboardApplication.cs file. This is the gloabal.asax code behind file that is called on app start up. This file starts up the Container which loads the facilities/components using windsor IoC. The container inherits from the defaultMasstransitContainer which has Windsor Castle hooks. Check out the config to see the injection of the Uri and queue type for the bus.
In the DashboardApplication you should also see the line
container.Resolve<IServiceBus>().Subscribe<RequestMessage>(HandleRequestMessage);
and its related delegate
private static void HandleRequestMessage(IMessageContext<RequestMessage> ctx)
{
ResponseMessage response = new ResponseMessage(ctx.Message.CorrelationId, "Request: " + ctx.Message.Text);
container.Resolve<IServiceBus>().Publish(response);
}
this is saying that this delegate wants to handle the receipt of the RequestMessage, "when the RequestMessage is published i want to process it with this command".
alrighty... next is the DemoController.
This is the controller for the asych page that does the work. If you are not familiar with monorail or the ASP.net MVC project this may be a bit confusing, but this is like the code behind page for a dynamic web page. Some of the important things to note are:
Consumes<responsemessage>.For<guid>
This says that this class is interested in (consumes or subscribes to) ResponseMessages. Now i need to look more into the
.For<guid> aspect so i wont comment much yet ( i assume it related to the request message id?)
The Synch method is empty but you will notice there is a BeginAsynch method with the same signature and an EndSynch method with no signatures. These methods and there contents are the asynch approach used by Monorail. Don't be too concerned, classic Asp.Net has had this little known feature for years albeit a slightly different syntax and wrapped around the pre-render aspect of the page life cycle (i may cover this in another post).
The asynch method is called from the the view on clicking the submit button (read up on MVC if this appears odd) where the bus is called and a Requestmessage sent to the bus. Some of the semantics here are very much monorail so the main point is:
a Request Message was sent to the Bus to be processed in an asynch fashion.
If you have debugging on with a break point in the DashboardApplication.HandleRequestMessage method you will see the bus has routed the request here. The method simply creates a basic response and publishes that response. Because the the DemoController is a subscriber to the ResponseMessage type it receives that message via its consume method, assigns it to a local variable and calls the request to be completed, which inturn calls the EndAsynch method.
the key points are the
Consumes<responsemessage>.For<guid>
on the controller with its consume methods and the asynch calls to the bus. each framework will do this slightly differently
container.Resolve<IServiceBus>().Subscribe<RequestMessage>(HandleRequestMessage);
the subscribing to the request message
container.Resolve<IServiceBus>().Publish(response);
the publishing of the response message
If you want to see the inner working then the 3 projects under the mass transit folder contain all the juicy stuff. typically these would just be refence dll's but the whole idea of sample is to see how the code works :)
Hopefully next up I may do a classic Asp.Net example to see if i actually get the concepts correctly (reading and writing are not the same things!) and can use the asynch methods from there.
--------------------------------------
Setting up MSMQ
NB On XP You must have an edition that supports MSMQ (ie not home)
Go in to Control panel > add or remove programmes
Select the add/remove windows components
Select Message Queuing if it is not already selected and add it.
Now if you go to “Control Panel” > “Administrative Tools” > “Computer Management” under the “Services and Applications” branch, when expanded, you should see a “Message Queuing” branch.
If so you have MSMQ set up.
You now need to set up queues for an application to uses
From : http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.ebd.eai.help.src/setting_up_msmq.htm
Setting Up Microsoft Message Queuing (MSMQ)
To Set up MSMQ on your Server:
1. Go to Control Panel -> Add/Remove Programs -> Add/Remove Windows Components.
2. Select Message Queuing Services Component from the Windows Component Wizard.
3. Follow the directions provided by the Queuing Services Wizard.
4. After completing the Queuing Services Wizard, go to Control Panel -> Administrative Tools -> Computer Management -> Services and Applications -> Message Queuing.
To Create a Public Queue:
1. Right click the Public Queues Directory.
2. Select New.
3. Select Public Queue.
4. In the Queue Name Dialogue Box, enter the Queue Name, where the format is [machine_name]\[queue_name].
To Create a Private Queue:
1. Right click the Private Queues Directory.
2. Select New.
3. Select Private Queue.
4. In the Queue Name Dialogue Box, enter the Queue Name, where the format is [machine_name]\private$\[queue_name].
For Mass Transit you will need to create a private queue of the name “test_servicebus” and leave the default setting of NOT transactional.