Browse Source

add MQ exceptions message to log.

master
yangxiaodong 7 years ago
parent
commit
31b0c8c7d0
4 changed files with 28 additions and 7 deletions
  1. +10
    -2
      src/DotNetCore.CAP.Kafka/KafkaConsumerClient.cs
  2. +9
    -2
      src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClient.cs
  3. +3
    -2
      src/DotNetCore.CAP/IConsumerClient.cs
  4. +6
    -1
      src/DotNetCore.CAP/IConsumerHandler.Default.cs

+ 10
- 2
src/DotNetCore.CAP.Kafka/KafkaConsumerClient.cs View File

@@ -12,7 +12,9 @@ namespace DotNetCore.CAP.Kafka
private readonly KafkaOptions _kafkaOptions;
private Consumer<Null, string> _consumerClient;

public event EventHandler<MessageContext> MessageReceieved;
public event EventHandler<MessageContext> OnMessageReceieved;

public event EventHandler<string> OnError;

public IDeserializer<string> StringDeserializer { get; set; }

@@ -67,6 +69,7 @@ namespace DotNetCore.CAP.Kafka
_consumerClient = new Consumer<Null, string>(config, null, StringDeserializer);

_consumerClient.OnMessage += ConsumerClient_OnMessage;
_consumerClient.OnError += ConsumerClient_OnError;
}

private void ConsumerClient_OnMessage(object sender, Message<Null, string> e)
@@ -77,7 +80,12 @@ namespace DotNetCore.CAP.Kafka
Name = e.Topic,
Content = e.Value
};
MessageReceieved?.Invoke(sender, message);
OnMessageReceieved?.Invoke(sender, message);
}

private void ConsumerClient_OnError(object sender, Error e)
{
OnError?.Invoke(sender, e.Reason);
}

#endregion private methods


+ 9
- 2
src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClient.cs View File

@@ -18,7 +18,8 @@ namespace DotNetCore.CAP.RabbitMQ
private IModel _channel;
private ulong _deliveryTag;

public event EventHandler<MessageContext> MessageReceieved;
public event EventHandler<MessageContext> OnMessageReceieved;
public event EventHandler<string> OnError;

public RabbitMQConsumerClient(string queueName, RabbitMQOptions options)
{
@@ -53,6 +54,7 @@ namespace DotNetCore.CAP.RabbitMQ
{
var consumer = new EventingBasicConsumer(_channel);
consumer.Received += OnConsumerReceived;
consumer.Shutdown += OnConsumerShutdown;
_channel.BasicConsume(_queueName, false, consumer);
while (true)
{
@@ -90,7 +92,12 @@ namespace DotNetCore.CAP.RabbitMQ
Name = e.RoutingKey,
Content = Encoding.UTF8.GetString(e.Body)
};
MessageReceieved?.Invoke(sender, message);
OnMessageReceieved?.Invoke(sender, message);
}

private void OnConsumerShutdown(object sender, ShutdownEventArgs e)
{
OnError?.Invoke(sender, e.Cause?.ToString());
}
}
}

+ 3
- 2
src/DotNetCore.CAP/IConsumerClient.cs View File

@@ -1,6 +1,5 @@
using System;
using System.Threading;
using DotNetCore.CAP.Infrastructure;

namespace DotNetCore.CAP
{
@@ -17,6 +16,8 @@ namespace DotNetCore.CAP

void Commit();

event EventHandler<MessageContext> MessageReceieved;
event EventHandler<MessageContext> OnMessageReceieved;

event EventHandler<string> OnError;
}
}

+ 6
- 1
src/DotNetCore.CAP/IConsumerHandler.Default.cs View File

@@ -95,7 +95,7 @@ namespace DotNetCore.CAP

private void RegisterMessageProcessor(IConsumerClient client)
{
client.MessageReceieved += (sender, message) =>
client.OnMessageReceieved += (sender, message) =>
{
_logger.EnqueuingReceivedMessage(message.Name, message.Content);

@@ -106,6 +106,11 @@ namespace DotNetCore.CAP
}
Pulse();
};

client.OnError += (sender, reason) =>
{
_logger.LogError(reason);
};
}

private CapReceivedMessage StoreMessage(IServiceScope serviceScope, MessageContext messageContext)


Loading…
Cancel
Save