Tapeti Consumer HandleBasicDeliver schedules a Task asynchronously to handle the messages
This commit is contained in:
parent
d71927915c
commit
9f30c1ec74
@ -17,6 +17,8 @@ namespace Tapeti.Connection
|
||||
private readonly IDependencyResolver dependencyResolver;
|
||||
private readonly IReadOnlyList<IMessageMiddleware> messageMiddleware;
|
||||
private readonly List<IBinding> bindings;
|
||||
|
||||
private readonly ILogger logger;
|
||||
private readonly IExceptionStrategy exceptionStrategy;
|
||||
|
||||
|
||||
@ -28,12 +30,15 @@ namespace Tapeti.Connection
|
||||
this.messageMiddleware = messageMiddleware;
|
||||
this.bindings = bindings.ToList();
|
||||
|
||||
logger = dependencyResolver.Resolve<ILogger>();
|
||||
exceptionStrategy = dependencyResolver.Resolve<IExceptionStrategy>();
|
||||
}
|
||||
|
||||
|
||||
public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey,
|
||||
IBasicProperties properties, byte[] body)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
ExceptionDispatchInfo exception = null;
|
||||
try
|
||||
@ -59,7 +64,7 @@ namespace Tapeti.Connection
|
||||
{
|
||||
if (binding.Accept(context, message))
|
||||
{
|
||||
InvokeUsingBinding(context, binding, message);
|
||||
await InvokeUsingBinding(context, binding, message);
|
||||
|
||||
validMessageType = true;
|
||||
}
|
||||
@ -68,26 +73,30 @@ namespace Tapeti.Connection
|
||||
if (!validMessageType)
|
||||
throw new ArgumentException($"Unsupported message type: {message.GetType().FullName}");
|
||||
|
||||
worker.Respond(deliveryTag, ConsumeResponse.Ack);
|
||||
await worker.Respond(deliveryTag, ConsumeResponse.Ack);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
exception = ExceptionDispatchInfo.Capture(UnwrapException(e));
|
||||
worker.Respond(deliveryTag, exceptionStrategy.HandleException(context, exception.SourceException));
|
||||
await worker.Respond(deliveryTag, exceptionStrategy.HandleException(context, exception.SourceException));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
exception = ExceptionDispatchInfo.Capture(UnwrapException(e));
|
||||
worker.Respond(deliveryTag, exceptionStrategy.HandleException(null, exception.SourceException));
|
||||
await worker.Respond(deliveryTag, exceptionStrategy.HandleException(null, exception.SourceException));
|
||||
}
|
||||
|
||||
exception?.Throw();
|
||||
if (exception != null)
|
||||
{
|
||||
logger.HandlerException(exception.SourceException);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void InvokeUsingBinding(MessageContext context, IBinding binding, object message)
|
||||
private Task InvokeUsingBinding(MessageContext context, IBinding binding, object message)
|
||||
{
|
||||
context.Binding = binding;
|
||||
|
||||
@ -136,9 +145,7 @@ namespace Tapeti.Connection
|
||||
await binding.Invoke(c, message);
|
||||
});
|
||||
|
||||
firstCaller.Call(context)
|
||||
.Wait();
|
||||
|
||||
return firstCaller.Call(context);
|
||||
}
|
||||
|
||||
private static Exception UnwrapException(Exception exception)
|
||||
|
@ -1,4 +1,6 @@
|
||||
namespace Tapeti.Default
|
||||
using System;
|
||||
|
||||
namespace Tapeti.Default
|
||||
{
|
||||
public class ConsoleLogger : ILogger
|
||||
{
|
||||
@ -16,5 +18,10 @@
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public void HandlerException(Exception e)
|
||||
{
|
||||
Console.WriteLine(e.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
namespace Tapeti.Default
|
||||
using System;
|
||||
|
||||
namespace Tapeti.Default
|
||||
{
|
||||
public class DevNullLogger : ILogger
|
||||
{
|
||||
@ -13,5 +15,9 @@
|
||||
public void ConnectSuccess(TapetiConnectionParams connectionParams)
|
||||
{
|
||||
}
|
||||
|
||||
public void HandlerException(Exception e)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
namespace Tapeti
|
||||
using System;
|
||||
|
||||
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.
|
||||
@ -7,5 +9,6 @@
|
||||
void Connect(TapetiConnectionParams connectionParams);
|
||||
void ConnectFailed(TapetiConnectionParams connectionParams);
|
||||
void ConnectSuccess(TapetiConnectionParams connectionParams);
|
||||
void HandlerException(Exception e);
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ namespace Test
|
||||
var container = new Container();
|
||||
container.Register<MarcoEmitter>();
|
||||
container.Register<Visualizer>();
|
||||
container.Register<ILogger, Tapeti.Default.ConsoleLogger>();
|
||||
|
||||
//container.Register<IFlowRepository>(() => new EF(serviceID));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user