About Features

This section is home to feature articles by regular and guest authors. Topics and opinions vary - some articles include discussions!

Home Features Open Source Integration with nServiceBus
Open Source Integration with nServiceBus
Features - Feature Articles
Written by Erik Westermann   
Sunday, 01 March 2009 15:54

Commercially available products, like Microsoft’s BizTalk Server, are a key part of many organizations’ SOA-style applications and application integration solutions. Freely available, open source solutions have been gaining acceptance in the broader marketplace. The acceptance of open source solutions is based on not only the maturity of the solutions, but also based on the accessibility and maturity of the underlying user groups that use, develop, and promote open source solutions. nServiceBus is a freely available, open source distributed, scalable, communications framework that’s based on the publish-subscribe model.

nSerivceBus, available since early 2007, is evolving under the guidance of it’s architect and author Udi Dahan. It’s latest release, version 1.9, primarily focuses on improving the underlying configuration model making it easier for developers that are not only new to nServiceBus, but also new to messaging-based solutions.


nServiceBus works by acting as a communications layer between publishers – entities that send information, and subscribers – entities that receive and process information. The information that passes between a publisher and subscriber is often referred to as a message – a message is, in Microsoft-based technologies, a type or class written in C# (or any other .NET-programming language).

nServiceBus provides the programming model, configuration model,  and binding to the underlying infrastructure to reliably handle messages and map message types to subscribers. The programming model in version 1.9 is considerably easier to use and understand as compared to previous versions.

One of the samples that comes out-of-the-box, called PubSub, is designed to demonstrate how easy it is to get nServiceBus working. The sample also demonstrates how to create message types and associate a message type with an endpoint.  The following walks you through some of the sample’s major features – you don’t have to have nServiceBus or the samples installed to follow-along.


nSerivceBus does not limit the definition of messages in any way – you describe your own messages in whatever way makes sense in your situation. nServiceBus only requires that you derive your message type(s) from the marker interface nServiceBus.IMessage (a marker interface - also referred to as an abscract type - is an interface that does not define any members). nServiceBus can operate on a concrete type (an instance of a class), and can operate on abstract types (an instance of class derived from an abstract type). This approach makes it possible for nServiceBus to operate on specific messages,  or messages that are based on a certain type.

The PubSub sample includes two messages, IEvent and EventMessage. For simplicity, the code that follows demonstrates how to publish the EventMessage type:

IEvent eventMessage;
eventMessage = new EventMessage();
// bus type defined elsewhere . . .
bus.Publish(eventMessage);

The code appears to be simple becasue the underlying infrastructure that nServiceBus provides handles the details of ensuring that the message has a subscriber and handles the details of sending the message to the subscriber.

Subscribers handle messages as follows:

// Handle messages of type IEvent
public class EventMessageHandler :
IMessageHandler<IEvent>
{
public void Handle(IEvent message)
{ . . . }
}
// Handle EventMessage
public class EventMessageHandler :
IMessageHandler<EventMessage>
{
public void Handle(EventMessage message)
{ . . . }
}

IMessageHandler in the preceding code is defined by nServiceBus. The nServiceBus infrastructure calls the Handle method when it delivers an EventMessage type message to the subscriber. nServiceBus is multi-threaded so that you can set it up to take advantage of available resources. The preceding method gets called whenever a message from any thread is available, thereby further simplifying the programming model. Subscribers still need to ensure that their code is thread-safe whenever they write to any shared resources.

Messages in the sample get transported between Publishers and Subscribers using MSMQ. I’m not going to explain the complete details about configuration here since it will divert the focus of this article. I will say that configuration is easy to understand if you take some of the code you see at face value. If you are not familiar with acronyms like IoC and DI, you may have to simply accept some of the code you read in the sample without trying to understand it, focusing instead on working with nServiceBus. Here is how the Publisher binds to the underlying bus:

 var bus = NServiceBus.Configure.With()
    .SpringBuilder()
    .MsmqSubscriptionStorage()
    .XmlSerializer()
    .MsmqTransport()
        .IsTransactional(true)
        .PurgeOnStartup(false)
    .UnicastBus()
        .ImpersonateSender(false)
    .CreateBus()
    .Start();

The code uses a new C# language feature that was introduced in version 3.0  - implicitly typed variables (shown on the first line – var bus . . .). Reading the rest of the code, which is really just one line, reveals that the publisher serializes messages using an XmlSerializer, and sends messages using MSMQ. nServiceBus runs the publisher asynchronously on its own thread so that once the above line executes, it immediately returns control to your code giving you the freedom to run other aspects of your application. 

Although nServiceBus does not currently include documentation it does have an active user community - Udi Dahan actively participates in the disucssions. The community’s discussions include enhancements, features, use cases, and day-to-day operation of nServiceBus.

More information about nSeriviceBus is at these sites:

This is the first of what will be a series of articles covering nSerivceBus – stay tuned!
 

Newsflash

"The number of computer science majors enrolled at U.S. universities increased for the first time in six years..."

Full article here.

RSS Feed

Sponsor

ArtOfBabel.com is supported by Erik Westermann's wWorkflow.net - BizTalk, SOA, ESB consulting & services.

Banner Ads Available

Contact advertising [at [ ArtOfBabel }dot{ com

Search