1
0
mirror of synced 2024-11-21 17:03:50 +00:00

Added concept for ILogger

Moved some code about
This commit is contained in:
Mark van Renswoude 2016-11-21 20:54:29 +01:00
parent 646ba22e63
commit e6a6c89e29
10 changed files with 164 additions and 70 deletions

View File

@ -9,11 +9,7 @@ namespace Tapeti.Connection
{
public class TapetiWorker
{
public string HostName { get; set; }
public int Port { get; set; }
public string VirtualHost { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public TapetiConnectionParams ConnectionParams { get; set; }
public string PublishExchange { get; set; }
@ -124,11 +120,11 @@ namespace Tapeti.Connection
var connectionFactory = new ConnectionFactory
{
HostName = HostName,
Port = Port,
VirtualHost = VirtualHost,
UserName = Username,
Password = Password,
HostName = ConnectionParams.HostName,
Port = ConnectionParams.Port,
VirtualHost = ConnectionParams.VirtualHost,
UserName = ConnectionParams.Username,
Password = ConnectionParams.Password,
AutomaticRecoveryEnabled = true,
RequestedHeartbeat = 30
};

20
Default/ConsoleLogger.cs Normal file
View File

@ -0,0 +1,20 @@
namespace Tapeti.Default
{
public class ConsoleLogger : ILogger
{
public void Connect(TapetiConnectionParams connectionParams)
{
throw new System.NotImplementedException();
}
public void ConnectFailed(TapetiConnectionParams connectionParams)
{
throw new System.NotImplementedException();
}
public void ConnectSuccess(TapetiConnectionParams connectionParams)
{
throw new System.NotImplementedException();
}
}
}

View File

@ -14,12 +14,29 @@ namespace Tapeti.Default
private readonly Lazy<DefaultControllerFactory> controllerFactory;
private readonly Lazy<DefaultRoutingKeyStrategy> routingKeyStrategy = new Lazy<DefaultRoutingKeyStrategy>();
private readonly Lazy<DefaultMessageSerializer> messageSerializer = new Lazy<DefaultMessageSerializer>();
private readonly Lazy<ILogger> logger;
public DefaultDependencyResolver(Func<IPublisher> publisherFactory)
{
controllerFactory = new Lazy<DefaultControllerFactory>(() => new DefaultControllerFactory(publisherFactory));
logger = new Lazy<ILogger>(() =>
{
// http://stackoverflow.com/questions/6408588/how-to-tell-if-there-is-a-console
try
{
// ReSharper disable once UnusedVariable
var dummy = Console.WindowHeight;
return new ConsoleLogger();
}
catch
{
return new DevNullLogger();
}
});
}
@ -34,6 +51,9 @@ namespace Tapeti.Default
if (typeof(T) == typeof(IMessageSerializer))
return (T)(messageSerializer.Value as IMessageSerializer);
if (typeof(T) == typeof(ILogger))
return (T)logger.Value;
return default(T);
}

17
Default/DevNullLogger.cs Normal file
View File

@ -0,0 +1,17 @@
namespace Tapeti.Default
{
public class DevNullLogger : ILogger
{
public void Connect(TapetiConnectionParams connectionParams)
{
}
public void ConnectFailed(TapetiConnectionParams connectionParams)
{
}
public void ConnectSuccess(TapetiConnectionParams connectionParams)
{
}
}
}

11
ILogger.cs Normal file
View File

@ -0,0 +1,11 @@
namespace Tapeti
{
// This interface is deliberately specific and typed to allow for structured logging (e.g. Serilog)
// instead of only string-based logging without control over the output.
public interface ILogger
{
void Connect(TapetiConnectionParams connectionParams);
void ConnectFailed(TapetiConnectionParams connectionParams);
void ConnectSuccess(TapetiConnectionParams connectionParams);
}
}

View File

@ -55,6 +55,11 @@
<Compile Include="Connection\TapetiPublisher.cs" />
<Compile Include="Connection\TapetiSubscriber.cs" />
<Compile Include="Connection\TapetiWorker.cs" />
<Compile Include="Default\ConsoleLogger.cs" />
<Compile Include="Default\DevNullLogger.cs" />
<Compile Include="ILogger.cs" />
<Compile Include="TapetiConnectionExtensions.cs" />
<Compile Include="TapetiConnectionParams.cs" />
<Compile Include="TapetiTypes.cs" />
<Compile Include="Tasks\SingleThreadTaskQueue.cs" />
<Compile Include="Default\DefaultControllerFactory.cs" />

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Tapeti.Annotations;
@ -12,11 +11,8 @@ namespace Tapeti
{
public class TapetiConnection : IDisposable
{
public string HostName { get; set; } = "localhost";
public int Port { get; set; } = 5672;
public string VirtualHost { get; set; } = "/";
public string Username { get; set; } = "guest";
public string Password { get; set; } = "guest";
public TapetiConnectionParams Params { get; set; }
public string PublishExchange { get; set; } = "";
public string SubscribeExchange { get; set; } = "";
@ -29,10 +25,21 @@ namespace Tapeti
private IDependencyResolver dependencyResolver;
private List<IQueueRegistration> registrations;
private TapetiWorker worker;
private readonly Lazy<List<IQueueRegistration>> registrations = new Lazy<List<IQueueRegistration>>();
private readonly Lazy<TapetiWorker> worker;
public TapetiConnection()
{
worker = new Lazy<TapetiWorker>(() => new TapetiWorker(
DependencyResolver.Resolve<IMessageSerializer>(),
DependencyResolver.Resolve<IRoutingKeyStrategy>())
{
ConnectionParams = Params ?? new TapetiConnectionParams(),
PublishExchange = PublishExchange
});
}
public TapetiConnection WithDependencyResolver(IDependencyResolver resolver)
{
@ -52,7 +59,7 @@ namespace Tapeti
if (!string.IsNullOrEmpty(queueAttribute.Name))
throw new ArgumentException("Dynamic queue attributes must not have a Name");
GetRegistrations().Add(new ControllerDynamicQueueRegistration(
registrations.Value.Add(new ControllerDynamicQueueRegistration(
DependencyResolver.Resolve<IControllerFactory>,
DependencyResolver.Resolve<IRoutingKeyStrategy>,
type, SubscribeExchange));
@ -62,7 +69,7 @@ namespace Tapeti
if (string.IsNullOrEmpty(queueAttribute.Name))
throw new ArgumentException("Non-dynamic queue attribute must have a Name");
GetRegistrations().Add(new ControllerQueueRegistration(
registrations.Value.Add(new ControllerQueueRegistration(
DependencyResolver.Resolve<IControllerFactory>,
type, SubscribeExchange, queueAttribute.Name));
}
@ -72,28 +79,13 @@ namespace Tapeti
}
public TapetiConnection RegisterAllControllers(Assembly assembly)
{
foreach (var type in assembly.GetTypes().Where(t => t.IsDefined(typeof(QueueAttribute))))
RegisterController(type);
return this;
}
public TapetiConnection RegisterAllControllers()
{
return RegisterAllControllers(Assembly.GetCallingAssembly());
}
public async Task<ISubscriber> Subscribe()
{
if (registrations == null || registrations.Count == 0)
if (!registrations.IsValueCreated || registrations.Value.Count == 0)
throw new ArgumentException("No controllers registered");
var subscriber = new TapetiSubscriber(GetWorker());
await subscriber.BindQueues(registrations);
var subscriber = new TapetiSubscriber(worker.Value);
await subscriber.BindQueues(registrations.Value);
return subscriber;
}
@ -101,17 +93,14 @@ namespace Tapeti
public IPublisher GetPublisher()
{
return new TapetiPublisher(GetWorker());
return new TapetiPublisher(worker.Value);
}
public async Task Close()
{
if (worker != null)
{
await worker.Close();
worker = null;
}
if (worker.IsValueCreated)
await worker.Value.Close();
}
@ -119,27 +108,5 @@ namespace Tapeti
{
Close().Wait();
}
protected List<IQueueRegistration> GetRegistrations()
{
return registrations ?? (registrations = new List<IQueueRegistration>());
}
protected TapetiWorker GetWorker()
{
return worker ?? (worker = new TapetiWorker(
DependencyResolver.Resolve<IMessageSerializer>(),
DependencyResolver.Resolve<IRoutingKeyStrategy>())
{
HostName = HostName,
Port = Port,
VirtualHost = VirtualHost,
Username = Username,
Password = Password,
PublishExchange = PublishExchange
});
}
}
}

View File

@ -0,0 +1,23 @@
using System.Linq;
using System.Reflection;
using Tapeti.Annotations;
namespace Tapeti
{
public static class TapetiConnectionExtensions
{
public static TapetiConnection RegisterAllControllers(this TapetiConnection connection, Assembly assembly)
{
foreach (var type in assembly.GetTypes().Where(t => t.IsDefined(typeof(QueueAttribute))))
connection.RegisterController(type);
return connection;
}
public static TapetiConnection RegisterAllControllers(this TapetiConnection connection)
{
return RegisterAllControllers(connection, Assembly.GetCallingAssembly());
}
}
}

35
TapetiConnectionParams.cs Normal file
View File

@ -0,0 +1,35 @@
using System;
namespace Tapeti
{
public class TapetiConnectionParams
{
public string HostName { get; set; } = "localhost";
public int Port { get; set; } = 5672;
public string VirtualHost { get; set; } = "/";
public string Username { get; set; } = "guest";
public string Password { get; set; } = "guest";
public TapetiConnectionParams()
{
}
public TapetiConnectionParams(Uri uri)
{
HostName = uri.Host;
VirtualHost = string.IsNullOrEmpty(uri.AbsolutePath) ? "/" : uri.AbsolutePath;
if (!uri.IsDefaultPort)
Port = uri.Port;
var userInfo = uri.UserInfo.Split(':');
if (userInfo.Length > 0)
{
Username = userInfo[0];
if (userInfo.Length > 1)
Password = userInfo[1];
}
}
}
}

View File

@ -31,7 +31,7 @@ namespace Test
while(true)
publisher.Publish(new MarcoMessage()).Wait();
Console.ReadLine();
//Console.ReadLine();
}
}
}