MiniBus

I’ve been hacking away recently on a little MSMQ side project I call MiniBus that allows me to reliably integrate between small applications and services, etc. with a dose of NServiceBus-style goodness. This kind of begs the question though, why not just use NServiceBus? Well, for a start, getting a business to pay for it can be a difficult conversation, and if you’re going to use it at all then really it should be used as part of an overall system architecture that can justify it such as the new mobile eCommerce platform we’ve recently been building. Whilst it is clearly capable of handling simple application integration scenarios too, that isn’t what it was really designed for and can be overkill. It’s also quite complex and requires a good understanding of distributed systems architecture, something not all developers are willing to learn about. As much as I love NServiceBus (and hey, it must be good ’cause its managed to convince some people that I have a clue about architecture, but don’t tell my boss!) sometimes the project at hand calls for a simpler approach.

I often find myself working on small integration projects when we bring new clients on board. Some of the older apps that we have that already do similar work are written in such a way that there exists a direct temporal coupling between us and our clients, or even to various other parts of our own system. It makes for a rather brittle architecture. My desire to improve this situation had me looking around for something that, in NServiceBus terms, would essentially give me a bus.Send but not bus.Publish. All I needed was the ability to break that coupling and let me integrate apps reliably by offloading work, when appropriate, to other services but…I also wanted some of the nice things that NServiceBus has like automatic retries, JSON serialization, transaction support, and error queues for failed messages. Oh, and I also wanted something small and lean and I think I’ve achieved that goal so I’m happy with the end result.

MiniBus is not an ESB and it doesn’t do pub-sub, but it does have some bus-like properties. In a nutshell, it offers the following features:

  • Send to one or more queues (local and/or remote)
  • Load balancing via round-robin dispatching
  • Read messages synchronously or asynchronously
  • Choice of XML or JSON serialization
  • Enlist in ambient transactions
  • Configurable automatic retries
  • Move to error queue on failure
  • Automatically create local queues
  • Install MSMQ if not detected
  • Simple logging support
  • Ability to return error messages back to the read queue

Whilst the source code and full documentation can be found on GitHub here’s a quick overview of using MiniBus. Essentially you create a bus instance via the BusBuilder class and then use it to either send messages:


// create a bus for sending messages
IBus bus = new BusBuilder()
    .WithLogging(new FileLogger())
    .InstallMsmqIfNeeded()
    .DefineErrorQueue("MiniBus.errors")
    .DefineWriteQueue("MiniBus.messages1")
    .DefineWriteQueue("MiniBus.messages2@remotepc")
    .DefineWriteQueue("MiniBus.messages3")
    .CreateLocalQueuesAutomatically()
    .EnlistInAmbientTransactions()
    .JsonSerialization()
    .CreateBus();

// create your message type
var bob = new Person { p.Name = "Bob", Age = 22 };

// send it
bus.Send(bob);

or receive them:


// create a bus for reading messages
IBus bus = new BusBuilder()
    .WithLogging(new FileLogger())
    .DefineErrorQueue("MiniBus.errors")
    .DefineReadQueue("MiniBus.messages1")
    .JsonSerialization()
    .NumberOfRetries(3)
    .CreateBus();

// register a message handler
bus.RegisterHandler<Person>(new PersonHandler());

// process messages on the read queue
bus.Receive<Person>();

Receiving messages can be done synchronously as shown in the example or asynchronously. If, for example, you want to process messages only at specific times then the Receive method is the better choice. If you want to process messages as and when they arrive then call the ReceiveAsync method as soon as you’ve created the bus and configured your handler. Either way, the handler specific code runs in the context of a transaction and should an exception occur MiniBus will retry if you’ve configured the bus to do so. If after the maximum number of retries the message still can’t be successfully processed, MiniBus will move it to an error queue. When you’re ready to try again, the ReturnAllErrorMessages method will move failed messages off the error queue and back to the read queue. On the subject of handlers, you simply create one or more classes that implement the IHandleMessage<T> interface and then register them with the bus.

MiniBus also has a rudimentary “load balancing” option via the AutoDistributeOnSend method. Ordinarily, having defined multiple write queues, sending a message would mean that a copy is sent to all of those queues which is fine if you need different handlers for the same message type. On the other hand, if you want to spread the load by having two or more different endpoints deal with messages in the same way setting the AutoDistributeOnSend option will cause MiniBus to adopt a round-robin style balancing approach whereby the first call to Send will send the message to the first queue, the second call to Send will send the message to the second queue, and so on before going back to the first. This means that no single endpoint is tasked with having to do all the work. In other words, you gain some parallelism.

In order to keep it small and simple, MiniBus has no dependencies on any other library. Logging is up to the consuming application. An interface is provided (ILogMessages) that you can use to wrap your logging library of choice. If you don’t like specifying queue names in code, it’s up to you to read them out of your config file and set the various bus properties. Finally, If you need a hosting process I recommend TopShelf for Windows Services. Right now, MiniBus leaves all of this up to you though that might change in future iterations.

There’s probably a lot more that could be done with MiniBus but you have to draw the line somewhere. There’s no point in attempting to make another ESB when there’s already plenty of great options out there like NServiceBus, and Rebus, another cool library. With MiniBus I had a simple use case in mind and I’ve more or less built what I needed but I think it could be useful to others too and for that reason I think it’s worth sharing so I’ve decided to put it “out there” by making it available on both GitHub and Nuget. It’s small, simple to use and reliable too so feel free to give it a go, and if you find a use for it then great! If you do happen to find any issues though, be sure to let me know or better yet, submit a patch! :)

Updates

I’ll keep this page updated with changes as and when they happen

24/11/2013

MiniBus 0.1.3 released

  • ReturnErrorMessages renamed to ReturnAllErrorMessages
  • Added ReturnErrorMessage method to move specific message from error queue
  • Added Fail fast option
  • The fail fast option is useful for scenarios where it’s important that messages are processed in order. As soon as an error is detected no more messages are processed and the failing message stays on the read queue.