Last week I posted the first in a series on Mass Transit, an open source enterprise messaging system. This series is my public exploration into messaging for our eCommerce application. In this post I'm going to dig in to see an actual working example. I like to see something work, then dig in to figure out how it's all put together. We'll take a look at a simple publish subscribe example to see the messaging in action. In later posts we'll dig in deeper into the guts of MassTransit. That said, let's see some MassTransit in action!
Getting Started
I've downloaded the MassTransit source from the Google Code repository and am going to run one of the samples provided with the source. We'll look at their "PublishSubscribe" sample.
Opening the solution reveals nine projects, a few more than what I would consider a simple sample, however don't let the solution scare you away. It turns out that you can for the most part ignore five of the projects, the mass transit infrastructure projects.
There are four parts to this solution that are relevant and we'll focus on those:
In order to get this sample going you first have to create the MSMQ queues that this sample expects:
You can create these queues in the Message Queuing area of the Services and Applications snap-in in the computer management area on your computer.
Now that we've got the project set up let's fire it up and see what we get. Since we need all three applications running at once, the client, server, and subscription manager we can take advantage of a cool feature in Visual Studio, multi-project startup.
With multi-project startup you start any number of projects in your solution. From the properties dialog for the solution we can choose which projects we want to kick off. (It is worth noting the order in which I have these projects launching.)
When you run this example, the client application will ask you to enter a new password. When you type in a new password you will get a nearly instantaneous response letting you know the password was updated.
The thing to bear in mind here is that at the point you hit the <enter> key submitting your new password, the client publishes a message and then it is done. It appears instantaneous but you'll want to keep in mind that it is asynchronous.
The diagram to the right illustrates what is going on once you hit the <enter> key.
It is worth noting here that the client knows nothing of the server and the server knows nothing of the client. Our example could easily expand to have multiple clients (think for example a windows app or multiple web servers in a farm). Likewise there could be multiple server instance, each consuming different messages. In other words, we are very loosely coupled, which is a good thing.
I hope this introduction to the MassTransit was beneficial. Now that we've seen the example working, in the next post I'll revisit the idea of durability. We'll augment this example and simulate a breakdown in infrastructure and see what durability really means, which is really at the heart of this fantastic open source library.
This blog contains the thoughts and discoveries of Tim Barcz, a technologist with a interests in computer programming technologies.