REQUEST A DEMO

Clock – A Super Simple Carrier Extension

Let’s set aside TwitterAgent for a bit and create the simplest extension to Dovetail Carrier possible, a timepiece, a clock, a chronometer of sorts. The intent of this post is to document in one place all the pieces required to create a Carrier Extension.

image

The Marker

For now we need to mark the assembly with an attribute announcing it as a Carrier extension.

[assembly: CarrierExtension]

You just need to put this code somewhere but it usually goes in your AssemblyInfo.cs file.

The Message

We first need to create the message we’ll be publishing and consuming.

public class ClockMessage
{
	public string Announcement { get; set; }
}

The Publisher

Next up we need to create a service which will publish these messages.

public class ClockMessageService : IMessagePublishingService<ClockMessage>
{
	private readonly MessageBusSettings _messageBusSettings;
	private readonly IEndpointFactory _endpointFactory;
	private readonly ILogger _logger;
	private Timer _timer;

	public ClockMessageService(MessageBusSettings messageBusSettings, 
IEndpointFactory endpointFactory, ILogger logger) { _messageBusSettings = messageBusSettings; _endpointFactory = endpointFactory; _logger = logger; } private void onTimer(object state) { var endpoint = _endpointFactory.GetEndpoint(_messageBusSettings.MessageQueue); var announcement = String.Format("At the tone the time will be {0}.",
DateTime.Now.ToLongTimeString()); endpoint.Send(new ClockMessage { Announcement = announcement }); } public void Start() { _logger.LogInfo("Starting test message service"); _timer = new Timer(onTimer, null,
TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10)); } public void Stop() { _logger.LogInfo("Stopping test message service"); _timer.Dispose(); } }

This service uses a timer to publish a ClockMessage every 10 seconds. Each message’s announcement is an homage to a bygone era when we didn’t have time pieces embedded into everything.

Defining RuleSets

public class ClockRuleRegistry : RulesRegistry
{
	public ClockRuleRegistry()
	{
		DefineRuleSetFor<ClockMessage>("Just log the message", rule =>
		{
			rule.AlwaysDo<LogAction>();
		});

		For<IMessagePublishingService<ClockMessage>>().Use<ClockMessageService>();
	}
}

A RuleRegistry is used to define one or more RuleSets. In this case the lone RuleSet  will always write the Announcement to the logger. The registry will additionally configure the message publishing service.

This is typically where you add functionality related to your business domain. What exactly do you want to do with a Clock Message in your business? 🙂

The Action

I needed the RuleSet to do something. So I made the simplest action I could think of. Log It!

public class LogAction: IAction<ClockMessage>
{
	private readonly ILogger _logger;

	public LogAction(ILogger logger)
	{
		_logger = logger;
	}

	public void Execute(IRuleContext<ClockMessage> context)
	{
		_logger.LogInfo(context.Message.Announcement);
	}
}

The Deployment

All you need to do for this extension to work is to drop it into the Carrier application directory.

image

To test I run Dovetail Carrier from the command line

image

Highlights are mine. You can see the ClockMessageService starting up. Messages are being received in the message queue and then being handled immediately by the “Just log the message” RuleSet.

All Done

I hope this one stop, soup to nuts, customization post for Dovetail Carrier helps you out.