Rhino ServiceBus Saga

ayendeI’m a huge fan of Rhino Mocks and have written about it on this blog. Today I read that Ayende has an implementation for a Service Bus. Like nServicebus but “free”, even Udi Dahan talks about it. Now I take a look at the long-running stateful workflow called Saga.

Starting the Saga

The Saga is defined with a generic interface with the type of the state. Implementing the interface adds the properties Id, State and IsCompleted.

public class GroupingSaga : ISaga<GroupSagaState> {
    public GroupSagaState State { get; set; }
    public Guid Id { get; set; }
    public bool IsCompleted { get; set; }
}

One message will be used to start the Saga. When this message implements the ISagaMessage interface the CorrelationId of the message is used to set the Saga Id. Is the message without the interface then the CorrelationId/Saga Id should be returned to the sender in order to continue the same Saga. I’m using the ISagaMessage implementation in my sample.

public class GroupingSaga : InitiatedBy<ProcessFileCommand> {
    public void Consume(ProcessFileCommand message) {
        // logic for starting the Saga
    }
}

After the Saga is started the State (actual the complete Saga) is saved with the SagaPersister. Since I use StructureMap as IoC I make sure the same persister is used else the Saga can never be retrieved. This is done in the ConfigureContainer of the Bootstrapper by defining the persister as a Singleton.

protected override void ConfigureContainer() {
    base.ConfigureContainer();
    Container.Configure(x => {
        x.For(typeof(ISagaPersister<>))
         .Singleton() // use the same persister every time
         .Use(typeof(InMemorySagaPersister<>));
    });
}

Next messages and Saga completion

Now that the Saga has started it can process the next messages. In my sample messages must be collected until a certain time of silence (no messages).

To send a message to the Saga it must contain the Id of the Saga. The ISagaMessage interface takes care of that. On the Saga I must define the Orchestrates interface so that it knows it should handle the message when the Saga has not completed (else the message is discarded).

public class GroupingSaga : Orchestrates<CheckReadyCommand> {
    public void Consume(CheckReadyCommand message) {
        // check LastAction to complete the Saga
    }
}

A Saga is completed when the IsCompleted property is set to True. When the Saga completes it can no longer process messages and the (default) persister will remove it from the storage.

Conclusion

Saving the Saga in memory is good for testing, but insufficient for production, make sure to use something like the RavenDb persister in the References.
Also the Saga implementation of Ayende works good, just like the servicebus in general. What’s missing is the tooling and out-of-the-box production solutions, but you can make and share that yourself. Welcome to the world of open source.

References

rhino-esb-raven a RavenDB Saga persister on github
Learn how to use Rhino Service Bus on Hibernating Rhinos
RhinoServiceBusSaga in my github repositories
BSD-3 license of Rhino ServiceBus

About erictummers

My work as a recruited developer changes almost every month. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s