Trying out NServiceBus part 3: pushing an event

In previous posts I’ve shown how I’ve setup testing environment for trying out NServiceBus. After dealing with some problems applications started to work correctly, at least to some extent. The only problem left is that event is being published, but it does not always reach the handler. Why is that?

I’ve setup my solution to start both Mailer and nServiceBusFun projects when I hit F5 (you can do it by right clicking on solution and selecting “Set StartUp projects…”). And sometimes when I do that message is being sent from publisher and is being delivered to handler correctly, but sometimes it’s not. The reason for this is we have here a race condition. Simply – if Mailer fails to register before nServiceBusFun publishes the event, it is not being delivered, which is pretty natural – NServiceBus cannot hold every possible event published and push it to handlers in future after they register. So I modified my publisher slightly, to sent event message after user presses any key on keyboard.

This way I can simply wait until Mailer registers successfully and then post event to service bus. And it works great, message is being sent, handler receives it and displays appropriate message.

As you can see I’ve made it so I can send up to 5 messages (just for testing reasons), each after key press. But let’s see what happens if Mailer application crushes while processing an event. After publishing an event see that it’s handled by subscriber correctly. Than close subscriber application and send few more events from publisher. Nothing bad happens which is OK – publisher is no affected in any way by subscriber failure which is definitely what we want. But such failure cannot cause messages being lost, those events might indicate that something important has happened and in response to that some actions need to be taken. It could be very problematic if such messages were never handled. So lets restart Mailer application by right clicking on Mailer project and starting new instance from Debug menu option. What you can see is that all the messages that had been published are immediately being delivered once subscriber finishes initialization. This is great feature that takes of our minds handling subscriber failures and need to store those messages manually. NServiceBus internally uses RavenDB to effectively store and send such messages.

Let’s stop for now, this is just a tip of an iceberg that NServiceBus is and I hope to learn a lot more about it and hopefully use it in production one day. And I will of course try to share all I will learn about it in here.


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s