|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace Foundatio.Messaging2 { |
| 5 | + public interface IEnvelope { |
| 6 | + // trace parent id used for distributed tracing |
| 7 | + string TraceParentId { get; } |
| 8 | + // message type |
| 9 | + string MessageType { get; } |
| 10 | + // message body |
| 11 | + object GetMessage(); |
| 12 | + // number of attempts to deliver the message |
| 13 | + int Attempts { get; } |
| 14 | + // when the message was originally sent |
| 15 | + DateTime SentAtUtc { get; } |
| 16 | + // when the message should expire |
| 17 | + DateTime? ExpiresAtUtc { get; } |
| 18 | + // when the message should be delivered when using delayed delivery |
| 19 | + DateTime? DeliverAtUtc { get; } |
| 20 | + // additional message data to store with the message |
| 21 | + IReadOnlyDictionary<string, string> Properties { get; } |
| 22 | + } |
| 23 | + |
| 24 | + public class Envelope : IEnvelope { |
| 25 | + private Lazy<object> _message; |
| 26 | + |
| 27 | + public Envelope(Func<object> getMessageFunc, string messageType, string coorelationId, DateTime? expiresAtUtc, DateTime? deliverAtUtc, IReadOnlyDictionary<string, string> properties) { |
| 28 | + _message = new Lazy<object>(getMessageFunc); |
| 29 | + MessageType = messageType; |
| 30 | + TraceParentId = coorelationId; |
| 31 | + ExpiresAtUtc = expiresAtUtc; |
| 32 | + DeliverAtUtc = deliverAtUtc; |
| 33 | + Properties = properties; |
| 34 | + } |
| 35 | + |
| 36 | + public Message(Func<object> getMessageFunc, MessagePublishOptions options) { |
| 37 | + _message = new Lazy<object>(getMessageFunc); |
| 38 | + TraceParentId = options.CorrelationId; |
| 39 | + MessageType = options.MessageType; |
| 40 | + ExpiresAtUtc = options.ExpiresAtUtc; |
| 41 | + DeliverAtUtc = options.DeliverAtUtc; |
| 42 | + Properties = options.Properties; |
| 43 | + } |
| 44 | + |
| 45 | + public string TraceParentId { get; private set; } |
| 46 | + public string MessageType { get; private set; } |
| 47 | + public int Attempts { get; private set; } |
| 48 | + public DateTime SentAtUtc { get; private set; } |
| 49 | + public DateTime? ExpiresAtUtc { get; private set; } |
| 50 | + public DateTime? DeliverAtUtc { get; private set; } |
| 51 | + public IReadOnlyDictionary<string, string> Properties { get; private set; } |
| 52 | + |
| 53 | + public object GetMessage() { |
| 54 | + return _message.Value; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public interface IEnvelope<out T> : IEnvelope where T: class { |
| 59 | + T Message { get; } |
| 60 | + } |
| 61 | + |
| 62 | + public class Envelope<T> : IEnvelope<T> where T: class { |
| 63 | + private readonly IEnvelope _envolope; |
| 64 | + |
| 65 | + public Envelope(IEnvelope message) { |
| 66 | + _envolope = message; |
| 67 | + } |
| 68 | + |
| 69 | + public T Message => (T)GetMessage(); |
| 70 | + |
| 71 | + public string TraceParentId => _envolope.TraceParentId; |
| 72 | + public string MessageType => _envolope.MessageType; |
| 73 | + public int Attempts => _envolope.Attempts; |
| 74 | + public DateTime SentAtUtc => _envolope.SentAtUtc; |
| 75 | + public DateTime? ExpiresAtUtc => _envolope.ExpiresAtUtc; |
| 76 | + public DateTime? DeliverAtUtc => _envolope.DeliverAtUtc; |
| 77 | + public IReadOnlyDictionary<string, string> Properties => _envolope.Properties; |
| 78 | + public object GetMessage() => _envolope.GetMessage(); |
| 79 | + } |
| 80 | +} |
0 commit comments