protected override async Task ConsumerReceived(object sender, BasicDeliverEventArgs eventArgs) { string[] result = eventArgs.RoutingKey.Split('.'); var eventName = result.Length > 1 ? result[0] : eventArgs.RoutingKey; try { try { var props = eventArgs.BasicProperties; var replyProps = ConsumerChannel.CreateBasicProperties(); replyProps.CorrelationId = props.CorrelationId; var response = await ProcessEventRpc(eventArgs.RoutingKey, eventName, eventArgs.Body).ConfigureAwait(false); var ms = new MemoryStream(); Serializer.Serialize <IIntegrationEventReply>(ms, response); var body = ms.ToArray(); ConsumerChannel.BasicPublish(ExchangeDeclareParameters.ExchangeName, (string)response.RoutingKey, replyProps, body); } catch (Exception ex) { Logger.LogError("CreateConsumerChannel RPC Received: " + ex.Message + " - " + ex.StackTrace); } } catch (Exception ex) { Logger.LogWarning("ConsumerReceived: " + eventName + " - " + ex.Message + " - " + ex.StackTrace); } // Even on exception we take the message off the queue. // in a REAL WORLD app this should be handled with a Dead Letter Exchange (DLX). // For more information see: https://www.rabbitmq.com/dlx.html ConsumerChannel.BasicAck(eventArgs.DeliveryTag, multiple: false); }
/// <summary> /// Acknowledge the message arrival and processing, so that RabbitMq can remove it from the queue. /// </summary> /// <param name="msg">The message instance</param> public void Ack(RabbitWorkMessage msg) { if (ConsumerChannel.IsOpen) { ConsumerChannel.BasicAck(msg.DeliveryTag, false); } }
protected override async Task ConsumerReceived(object sender, BasicDeliverEventArgs eventArgs) { string[] result = eventArgs.RoutingKey.Split('.'); var eventName = result.Length > 1 ? result[0] : eventArgs.RoutingKey; try { //if (!eventArgs.BasicProperties.CorrelationId.Equals(_correlationId)) return; if (!_callbackMapper.TryRemove(eventArgs.BasicProperties.CorrelationId, out TaskCompletionSource <dynamic> tcs)) { return; } await ProcessEventReply(eventArgs.RoutingKey, eventName, eventArgs.Body, tcs).ConfigureAwait(false); } catch (Exception ex) { Logger.LogWarning("ConsumerReceivedReply: " + eventName + " - " + ex.Message + " - " + ex.StackTrace); } // Even on exception we take the message off the queue. // in a REAL WORLD app this should be handled with a Dead Letter Exchange (DLX). // For more information see: https://www.rabbitmq.com/dlx.html ConsumerChannel.BasicAck(eventArgs.DeliveryTag, multiple: false); }
private void Connect() { var logString = $"hostname: {_consumerSettings.Host}, username: {_consumerSettings.Username}, password: {_consumerSettings.Password}, exchangeName: {_consumerSettings.Exchange}, " + $"queueName: {_consumerSettings.Queue}, isDurable: {_consumerSettings.IsDurable}, isAutodelete: {_consumerSettings.AutoDelete}, routingKey: {_consumerSettings.RoutingKey}"; _logger.LogInformation("Connect: Connecting for {0}", logString); _consumer = new EventingBasicConsumer(ConsumerChannel); _consumer.Received += (model, ea) => { try { _logger.LogInformation("Consume: Calling handler for {0}", logString); IBasicProperties props = ea.BasicProperties; // get body byte[] body = ea.Body; var json = Encoding.UTF8.GetString(body); var msg = JsonConvert.DeserializeObject <QMessage>(json, _consumerSerializationSettings); msg.Properties = new QProperties() { AppId = props.AppId, ClusterId = props.ClusterId, ContentEncoding = props.ContentEncoding, ContentType = props.ContentType, CorrelationId = props.CorrelationId, DeliveryMode = props.DeliveryMode, Expiration = props.Expiration, MessageId = props.MessageId, Priority = props.Priority, ReplyTo = props.ReplyTo, Type = props.Type, UserId = props.UserId }; if (MessageRecieved != null) { MessageRecieved(model, msg); } //callBack(model, msg); // return byte array ConsumerChannel.BasicAck(ea.DeliveryTag, false); _logger.LogInformation("Consume: Acknowledged for {0}", logString); } catch (Exception ex) { _logger.LogError("Consume: Error {0}, Stack: {1}", ex.Message, ex.StackTrace); } }; ConsumerChannel.BasicConsume(queue: _consumerSettings.Queue, autoAck: false, consumer: _consumer); _logger.LogInformation("Consume: Connected for {0}", logString); }
/// <summary> /// /// </summary> /// <returns></returns> public QMessage Dequeue <T>(bool ack = false) { QMessage msg = null; BasicGetResult result; try { //IBasicProperties basicProperties = ConsumerChannel.CreateBasicProperties(); //basicProperties.Persistent = true; lock (_lockSubscriber) { bool noAck = false; // get message from queue result = ConsumerChannel.BasicGet(_consumerSettings.Queue, noAck); if (result == null) { // No message available at this time. } else { IBasicProperties props = result.BasicProperties; // get body byte[] body = result.Body; var json = Encoding.UTF8.GetString(body); json = json.Replace("\"$type\":\"Infrastructure.Queueing.Model.QMessage, Infrastructure.Queueing\",", ""); json = json.Replace("\"$type\":\"Infra.Quotes.Brige.Models.BridgeQuote, Infra.Quotes.Bridge\",", ""); msg = JsonConvert.DeserializeObject <QMessage>(json, _consumerSerializationSettings); msg.DeliveryTag = result.DeliveryTag; msg.Properties = new QProperties() { AppId = props.AppId, ClusterId = props.ClusterId, ContentEncoding = props.ContentEncoding, ContentType = props.ContentType, CorrelationId = props.CorrelationId, DeliveryMode = props.DeliveryMode, Expiration = props.Expiration, MessageId = props.MessageId, Priority = props.Priority, ReplyTo = props.ReplyTo, Type = props.Type, UserId = props.UserId }; if (ack) { ConsumerChannel.BasicAck(result.DeliveryTag, false); } } } } catch (OperationInterruptedException ex) { _logger.LogCritical($"Dequeue Error {ex.Message},Inner Exception:{ex.InnerException}, Stack: {ex.StackTrace}"); throw; } return(msg); }