Skip to content

Instantly share code, notes, and snippets.

@abdullin
abdullin / SampleQuarantine.cs
Created June 15, 2011 16:06
Quarantine sample that reports recurring failures to email (sample for Lokad.CQRS v2.0)
// This quarantine sample uses in-memory quarantine to detect repetitive processing failures
// It records each failure into the streaming container (which could be file/Azure)
// When failure threshold is breached, we send an email to the hardcoded address.
// failure message contains all information about failures and message contents
public sealed class MailQuarantine : IEnvelopeQuarantine
{
readonly SmtpHandlerCore _core;
readonly IStreamingContainer _container;
readonly MemoryQuarantine _quarantine = new MemoryQuarantine();
@abdullin
abdullin / IEnvelopeQuarantine.cs
Created June 16, 2011 05:57
Documented version of IEnvelopeQuarantine
/// <summary>
/// Implements quarantine logic for the specific message handler. Default implementation
/// is <see cref="MemoryQuarantine"/>
/// </summary>
public interface IEnvelopeQuarantine
{
/// <summary>
/// Tries to quarantine the specified envelope. Implementation can decide whether we need to give another
/// try to process the envelope (by returning <em>False</em>) or if quarantine should accept the envelope
/// completely. Then processor will discard the queue from it's incoming queue and leave it up to the
@abdullin
abdullin / RegisterPoisoningQuarantine.cs
Created June 16, 2011 06:07
An example of registering poison quarantine.
x.Quarantine(c => {
var logRoot = c.Resolve<IStreamingRoot>();
var registry = c.Resolve<QueueWriterRegistry>();
IQueueWriterFactory factory;
if (!registry.TryGet("azure-account-name", out factory))
throw new IOE("Didn't find the specified Azure account. Did you register it in the queue registry?")
// registration is right now, if you don't have it already
// builder.Advanced.RegisterQueueWriterFactory(c => new AzureQueueWriterFactory(account, c.Resolve<IEnvelopeStreamer>()));
return new SampleQuarantine(logging, factory);
@abdullin
abdullin / RoutingDispatcher.cs
Created June 20, 2011 16:31
Sample of message dispatcher that routes and records messages (for Lokad.CQRS v2.0)
// this dispatchers routes all incoming messages between 2 queues (commands/events)
// and also records all messages into a tape storage.
// fore registration sample see https://gist.github.com/1035950
public sealed class RoutingDispatcher : ISingleThreadMessageDispatcher
{
readonly IDictionary<string, IQueueWriter> _routes = new Dictionary<string, IQueueWriter>();
readonly QueueWriterRegistry _factories;
readonly string _endpoint;
readonly ITapeWriter _writer;
readonly IEnvelopeStreamer _streamer;
@abdullin
abdullin / RoutingDispatcherRegistration.cs
Created June 20, 2011 16:33
Sample of registering routing dispatcher with custom quarantine (Lokad.CQRS v2.0)
m.AddAzureProcess(config, IdFor.Publish, p =>
{
p.DispatcherIs(
(c, a, x) =>
{
// provided by the bus
var registry = c.Resolve<QueueWriterRegistry>();
var streamer = c.Resolve<IEnvelopeStreamer>();
// not provided by bus in v2.0
var tapeWriter = c.Resolve<ITapeWriter>();
@abdullin
abdullin / RedirectToWhen.cs
Created June 26, 2011 08:59
Static helper class for invoking handling methods on AR and AR state.
// snippet is for http://abdullin.com/journal/2011/6/26/event-sourcing-a-la-lokad.html
// simple helper, that looks up and calls the proper overload of
// When(SpecificEventType event). Reflection information is cached statically
// once per type.
public static class RedirectToWhen
{
static class Cache<T>
{
public static readonly IDictionary<Type, MethodInfo> Dict = typeof(T)
@abdullin
abdullin / TimerService.cs
Created June 28, 2011 08:43
Sample of a timer service for powering up sagas (for Lokad.CQRS v2.0)
// the gist is discussed in: http://groups.google.com/group/lokad/browse_thread/thread/4d39dc299037ea09
// This is a sample of a timer service for powering up sagas (for Lokad.CQRS v2.0)
// register it in CqrsHostBuilder like this:
// hostBuilder.Advanced.ConfigureContainer(cb => cb.RegisterType<TimerService>().As<IEngineProcess>());
public sealed class TimerService : IEngineProcess
{
readonly IMessageSender _sender;
public TimerService(IMessageSender sender)
{
@abdullin
abdullin / WebRole.cs
Created July 6, 2011 05:17
Sample of running AppEngine inside the Web Role (for Lokad.CQRS v2.0)
// Sample of running AppEngine inside the Web Role, essentially merging
// 1 worker role and 1 web role (for Lokad.CQRS v2.0)
// For explanation see: http://abdullin.com/journal/2011/7/6/lokadcqrs-can-make-windows-azure-cheaper-for-you.html
public class WebRole : RoleEntryPoint
{
CqrsEngineHost _host;
readonly CancellationTokenSource _source = new CancellationTokenSource();
public override bool OnStart()
{
@abdullin
abdullin / Program.cs
Created October 6, 2011 18:27
Wiring Lokad-CodeDSL in a MightyMoose style.
// This is a hacky sample (that works for me) of alternative way to use Lokad-codeDSL
// or any similar way of generating message contracts on-the-fly. Original approach was
// with using T4 template, that would rebuild cs files from DSL representation, whenever
// we hit Ctrl-S.
// This approach works almost exactly like this (Ctrl-S to rebuild), but does not require VS
// to run or does not require unloading VS to change the underlying generator code.
// in fact it is extremely boring. Lolcats from MightyMoose could be used to improve the situation, though.
// any takers? :)
@abdullin
abdullin / TestMessageSerialization.cs
Created October 12, 2011 18:51
Sample of using Specifications to test serializers
/// <summary>
/// This class scans all available specifications for messages used
/// then performs round-trip via specified serializer,
/// and then does the structural comparison of resulting values
/// </summary>
[TestFixture]
public sealed class TestMessageSerialization
{
static Group[] ListMessages()
{