public virtual ChannelMessage Build(BasicDeliverEventArgs message) { if (message == null) throw new ArgumentNullException("message"); try { var result = this.Translate(message); this.AppendHeaders(result, message.BasicProperties); return result; } catch (SerializationException e) { Log.Error("Unable to deserialize message '{0}'.".FormatWith(message.MessageId()), e); throw new PoisonMessageException(e.Message, e); } catch (DeadLetterException) { throw; } catch (Exception e) { Log.Error("General deserialize error for message '{0}'.".FormatWith(message.MessageId()), e); throw new PoisonMessageException(e.Message, e); } }
protected virtual void TryReceive(BasicDeliverEventArgs message, Action<IDeliveryContext> callback) { var messageId = message.MessageId(); try { Log.Verbose("Translating wire-specific message into channel message."); this.CurrentMessage = this.adapter.Build(message); Log.Info("Routing message '{0}' received through group '{1}' to configured receiver callback.", messageId, this.configuration.GroupName); callback(this); } catch (ChannelConnectionException) { Log.Warn("The channel has become unavailable, aborting current transaction."); this.CurrentTransaction.TryDispose(); throw; } catch (PoisonMessageException e) { Log.Warn("Wire message {0} could not be deserialized; forwarding to poison message exchange.", messageId); this.ForwardToPoisonMessageExchange(message, e); } catch (DeadLetterException) { Log.Info("Wire message {0} has expired on the wire; forwarding to dead-letter exchange.", messageId); this.ForwardTo(message, this.configuration.DeadLetterExchange); } catch (Exception e) { this.RetryMessage(message, e); } }
protected virtual void RetryMessage(BasicDeliverEventArgs message, Exception exception) { var nextAttempt = this.AppendException(message, exception) + 1; Log.Debug("Message '{0}' has been attempted {1} times.", message.MessageId(), nextAttempt); if (nextAttempt > this.configuration.MaxAttempts) { Log.Error("Unable to process message '{0}'".FormatWith(message.MessageId()), exception); this.ForwardToPoisonMessageExchange(message, null); } else { Log.Info("Unhandled exception for message '{0}'; retrying.".FormatWith(message.MessageId()), exception); this.ForwardTo(message, this.configuration.InputQueue.ToPublicationAddress()); } }
protected virtual void Send(BasicDeliverEventArgs message, PublicationAddress recipient) { if (recipient == null) return; if (recipient == this.configuration.DeadLetterExchange) this.adapter.AppendRetryAddress(message); this.EnsureTransaction().Register(() => this.Try(() => { Log.Info("Dispatching wire message '{0}' to messaging infrastructure for recipient '{1}'.", message.MessageId(), recipient); this.channel.BasicPublish(recipient, message.BasicProperties, message.Body); })); }
protected virtual void ForwardTo(BasicDeliverEventArgs message, PublicationAddress address) { Log.Debug("Forwarding message '{0}' to recipient '{1}'.", message.MessageId(), address); this.EnsureTransaction(); this.Send(message, address); this.CurrentTransaction.Commit(); }
protected virtual void ForwardToPoisonMessageExchange(BasicDeliverEventArgs message, Exception exception) { Log.Info("Message '{0}' is a poison message.", message.MessageId()); this.AppendException(message, exception); message.SetAttemptCount(0); this.adapter.AppendRetryAddress(message); this.ForwardTo(message, this.configuration.PoisonMessageExchange); }
protected virtual void RetryMessage(BasicDeliverEventArgs message, Exception exception) { var nextAttempt = message.GetAttemptCount() + 1; message.SetAttemptCount(nextAttempt); // 0-based to 1-based value Log.Debug("Message '{0}' has been attempted {1} times.", message.MessageId(), nextAttempt); if (nextAttempt > this.configuration.MaxAttempts) this.ForwardToPoisonMessageExchange(message, exception); else this.ForwardTo(message, this.configuration.InputQueue.ToPublicationAddress()); }