Archive for July, 2009
Configuring, Testing and Probing NServiceBus with IronPython
I have recently been working with NServiceBus quite a bit and whilst I was working on a WPF IronPython Console - User Control / Window, it occurred to me that it would be a useful tool in testing and monitoring behaviour of my services whilst running under NServiceBus. Let’s have a look at the simplest of examples by configuring up an IBus and publishing messages using Udi’s PubSub sample.
The first change to Udi’s sample code comes in the publisher where the main method’s body no longer contains any configuration code whatsoever.
using System; using Common.Logging; using NServiceBus; using Messages; using System.Collections.Generic; using System.ComponentModel; using Org.TechA.Wpf.Controls; using NServiceBus.Unicast.Transport.Msmq.Config; using NServiceBus.Unicast.Transport.Msmq; using NServiceBus.ObjectBuilder; //register the extensions for configuration //so IronPython can see the extension methods [assembly: Microsoft.Scripting.Runtime.ExtensionType( typeof(Configure), typeof(ConfigureSpringBuilder))] [assembly: Microsoft.Scripting.Runtime.ExtensionType( typeof(Configure), typeof(ConfigureMsmqSubscriptionStorage))] [assembly: Microsoft.Scripting.Runtime.ExtensionType( typeof(Configure), typeof(ConfigureXmlSerializer))] [assembly: Microsoft.Scripting.Runtime.ExtensionType( typeof(Configure), typeof(ConfigureMsmqTransport))] [assembly: Microsoft.Scripting.Runtime.ExtensionType( typeof(Configure), typeof(ConfigureUnicastBus))] [assembly: Microsoft.Scripting.Runtime.ExtensionType( typeof(Configure), typeof(ConfigureUnicastBus))] namespace Server { class Program { static IBus bus = null; [STAThread] static void Main() { LogManager.GetLogger(“hello”).Debug(“Started.”); var ir_window = new ConsoleWindow(); ir_window.ShowDialog(); } } }
What’s been added here is the decoration of the assembly with a set of ExtensionType attributes from the DLR to allow us to program against the NServiceBus fluent interfaces extension methods from IronPython. The other thing to note in the code above is that the configuration code from the PubSub sample is removed and what now remains is to create and run an instance of the IronPython Application Shell, effectively embedding IronPython into my NServiceBus publisher. Once this is done we are all set to start scripting our services and NServiceBus. If I run this code above I expect to see the the IronPython Application Scripting Window pop up ready for action.
Configure the Bus and Send a message
Here’s a look at a sample script typed at the console window (see above) to configure the bus and start sending some messages.
#import the relevant libraries from System import * from NServiceBus import * import clr #add a reference to the messages #library from the pubsub sample #and import it clr.AddReference(“Messages”) from Messages import * #configure the IBus #NOTE: type this line as a WHOLE in the #IronPython Console Window bus = Configure.With() .SpringBuilder() .MsmqSubscriptionStorage() .XmlSerializer().MsmqTransport() .IsTransactional(True).PurgeOnStartup(False) .UnicastBus() .ImpersonateSender(False) .CreateBus() .Start() #create some messages msg1 = EventMessage() msg1.EventId = Guid.NewGuid() msg1.Duration = TimeSpan(99999) msg1.Time = DateTime.Now msg2 = EventMessage() msg2.EventId = Guid.NewGuid() msg2.Duration = TimeSpan(99999) msg2.Time = DateTime.Now.AddYears(-1) #prove that the messages have values print msg1.EventId.ToString() print msg2.EventId.ToString() #put the messages in an array msgs = Array[EventMessage]((msg1, msg2)) #publish the messages bus.Publish[EventMessage](msgs)
Running the Code
After running this script above we can expect our subscribers to start receiving the messages and we can check to see if indeed the Guid and DateTime property values in the messages match up once being received by the subscribers.
As you can see, our EventId values and other properties all match those created using a REPL experience with IronPython embedded as our publisher. The Dynamic languages turn out to be useful in other places when it comes to NServiceBus but we will leave that for another post soon.
Also, I would like to point out that you could leave the configuration code in the C# code that starts up the publisher and push the IBus instance as a variable into the scope of the IronPython runtime and then execute a similar (smaller) script against it. The IronPython Application Shell has a method that allows you to add variables into the scope ad-hoc from the statically typed side of the publisher. Here’s how to achieve that:
[STAThread] static void Main() { LogManager.GetLogger(“hello”).Debug(“Started.”); bus = NServiceBus.Configure.With() .SpringBuilder() .MsmqSubscriptionStorage() .XmlSerializer() .MsmqTransport() .IsTransactional(true) .PurgeOnStartup(false) .UnicastBus() .ImpersonateSender(false) .CreateBus() .Start(); var ir_window = new ConsoleWindow(); var vars = new Dictionary<string, object>(); vars.Add(“bus”, bus); ir_window.ctlIronPythonConsole.OnVariablesAdded(vars); ir_window.Title = “NServiceBus IronPython Publisher”; ir_window.ShowDialog(); }
You can find the full code here and if your interested in downloading The IronPython Application Shell, it can be found here.
Below is the link to the full sample including the the NServiceBus PubSub sample, however you should remember to setup the queues that are required (by the NServiceBus samples themselves) by running the VbScript that comes with the download of NServiceBus itself.
NServiceBus-Pub/Sub Example with IronPython Application Shell
No commentsFirefox annoyance – rare but true
I have been a Firefox fan for a couple of versions now but I cant
believe this really annoying great feature in the latest version (3.5) of Firefox. If I hover over any part of a page, it displays an empty tooltip.
It’s hardly a feature that gets in your way but it’s damn annoying. Even if I hover over the vertical scrollbar I can see the damn thing. Apparently there has been some history of tooltip strangeness with Firefox in the past but I haven’t really delved into it too deeply.
Anyone heard of this problem before or experiencing it with the current version?
5 commentsNServiceBus has made messaging an easier choice.
I am finding it harder and harder to find scenarios where I would consider anything other than NServiceBus to build messaging systems behind the corporate firewall. Certainly punching a hole through it might open up the possibility for other technologies, but in most cases it’s to act as a bridge to what will eventually hit some NServiceBus infrastructure.
Deployment is a snap and in a project based environment where small systems are regularly custom built to order for heavy load batch processing, NServiceBus will make rolling out a highly scalable solution a very quick and reasonably simple process. Also, NServiceBus configuration is far simpler these days and to be honest, compared to some frameworks it’s downright simple to understand.
The kind of processing numbers we are achieving in current proof of concepts for new projects are impressive and the deployment story is going to let us ring out every last drop of blood from hardware otherwise considered as redundant and useless. Not a web server in sight!
One thing on MY the Network Administrators wish list is a zero deployment transactional queue (transport). Andreas created a file based ITransport implementation but unfortunately no support for transactions, however it does show just how extensible NServiceBus is.
No comments







