public static class EasyNetQBusFactory
{
/// <summary>
/// The lock.
/// </summary>
private static readonly object Lock = new object();
/// <summary>
/// The current bus
/// </summary>
private static IBus currentBus = null;
/// <summary>
/// Gets the instance of <see cref="IBus"/>.
/// </summary>
/// <returns>The instance of <see cref="IBus"/></returns>
public static IBus GetInstance()
{
if (currentBus == null)
{
lock (Lock)
{
if (currentBus == null)
{
var connectionString = ConfigurationManager.AppSettings["RabbitMQ.ConnectionString"];
var encryptionKey = ConfigurationManager.AppSettings["RabbitMQ.EncryptionKey"];
var logger = LogManager.GetLogger(typeof(IAdvancedBus));
currentBus = RabbitHutch.CreateBus(
connectionString,
service => service
.Register<IEasyNetQLogger>(_ => new Log4NetLogger(logger))
.Register<ISerializer>(_ => new EncryptionEnabledJsonSerializer(encryptionKey, new TypeNameSerializer())));
currentBus.Advanced.Connected += (o, args) =>
{
LogManager.GetLogger(typeof(EasyNetQBusFactory)).Debug("Connected.");
};
currentBus.Advanced.Disconnected += (o, args) =>
{
LogManager.GetLogger(typeof(EasyNetQBusFactory)).Error("Disconnected.");
};
currentBus.Advanced.Blocked += (o, args) =>
{
LogManager.GetLogger(typeof(EasyNetQBusFactory)).Fatal("Blocked.");
};
currentBus.Advanced.Unblocked += (o, args) =>
{
LogManager.GetLogger(typeof(EasyNetQBusFactory)).Info("Unblocked.");
};
currentBus.Advanced.MessageReturned += (o, args) =>
{
LogManager.GetLogger(typeof(EasyNetQBusFactory)).FatalFormat("MessageReturned. [{0}]", args.MessageBody);
};
}
}
}
return currentBus;
}
/// <summary>
/// Kills the instance of <see cref="IBus"/>.
/// </summary>
public static void KillInstance()
{
if (currentBus != null)
{
currentBus.Dispose();
currentBus = null;
}
}
}