From ed421361ac071fa8b9ee1e704c64e24f233dbcf1 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Tue, 19 Mar 2019 11:47:52 +0100 Subject: [PATCH] Fixed #15: Attribute for mandatory messages --- Tapeti.Annotations/DurableQueueAttribute.cs | 3 +++ Tapeti.Annotations/DynamicQueueAttribute.cs | 11 +++++------ Tapeti.Annotations/MandatoryAttribute.cs | 15 +++++++++++++++ Tapeti.Annotations/MessageControllerAttribute.cs | 5 +++++ Tapeti.Annotations/RequestAttribute.cs | 8 ++++++++ Tapeti/Connection/TapetiPublisher.cs | 10 +++++++++- 6 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 Tapeti.Annotations/MandatoryAttribute.cs diff --git a/Tapeti.Annotations/DurableQueueAttribute.cs b/Tapeti.Annotations/DurableQueueAttribute.cs index bf4cac7..d6fe89d 100644 --- a/Tapeti.Annotations/DurableQueueAttribute.cs +++ b/Tapeti.Annotations/DurableQueueAttribute.cs @@ -2,6 +2,7 @@ namespace Tapeti.Annotations { + /// /// /// Binds to an existing durable queue to receive messages. Can be used /// on an entire MessageController class or on individual methods. @@ -17,6 +18,8 @@ namespace Tapeti.Annotations public string Name { get; set; } + /// + /// The name of the durable queue public DurableQueueAttribute(string name) { Name = name; diff --git a/Tapeti.Annotations/DynamicQueueAttribute.cs b/Tapeti.Annotations/DynamicQueueAttribute.cs index 5fe9525..f7de921 100644 --- a/Tapeti.Annotations/DynamicQueueAttribute.cs +++ b/Tapeti.Annotations/DynamicQueueAttribute.cs @@ -2,6 +2,7 @@ namespace Tapeti.Annotations { + /// /// /// Creates a non-durable auto-delete queue to receive messages. Can be used /// on an entire MessageController class or on individual methods. @@ -12,12 +13,10 @@ namespace Tapeti.Annotations public string Prefix { get; set; } - /// - /// If prefix is specified, Tapeti will compose the queue name using the - /// prefix and a unique ID. If not specified, an empty queue name will be passed - /// to RabbitMQ thus letting it create a unique queue name. - /// - /// + /// + /// An optional prefix. If specified, Tapeti will compose the queue name using the + /// prefix and a unique ID. If not specified, an empty queue name will be passed + /// to RabbitMQ thus letting it create a unique queue name. public DynamicQueueAttribute(string prefix = null) { Prefix = prefix; diff --git a/Tapeti.Annotations/MandatoryAttribute.cs b/Tapeti.Annotations/MandatoryAttribute.cs new file mode 100644 index 0000000..38fcc87 --- /dev/null +++ b/Tapeti.Annotations/MandatoryAttribute.cs @@ -0,0 +1,15 @@ +using System; + +namespace Tapeti.Annotations +{ + /// + /// + /// Can be attached to a message class to specify that delivery to a queue must be guaranteed. + /// Publish will fail if no queues bind to the routing key. Publisher confirms must be enabled + /// on the connection for this to work (enabled by default). + /// + [AttributeUsage(AttributeTargets.Class)] + public class MandatoryAttribute : Attribute + { + } +} diff --git a/Tapeti.Annotations/MessageControllerAttribute.cs b/Tapeti.Annotations/MessageControllerAttribute.cs index 1f419f2..f4a4723 100644 --- a/Tapeti.Annotations/MessageControllerAttribute.cs +++ b/Tapeti.Annotations/MessageControllerAttribute.cs @@ -2,6 +2,11 @@ namespace Tapeti.Annotations { + /// + /// + /// Attaching this attribute to a class includes it in the auto-discovery of message controllers + /// when using the RegisterAllControllers method. It is not required when manually registering a controller. + /// [AttributeUsage(AttributeTargets.Class)] public class MessageControllerAttribute : Attribute { diff --git a/Tapeti.Annotations/RequestAttribute.cs b/Tapeti.Annotations/RequestAttribute.cs index d9fc44e..2f14097 100644 --- a/Tapeti.Annotations/RequestAttribute.cs +++ b/Tapeti.Annotations/RequestAttribute.cs @@ -2,6 +2,14 @@ namespace Tapeti.Annotations { + /// + /// + /// Can be attached to a message class to specify that the receiver of the message must + /// provide a response message of the type specified in the Response attribute. This response + /// must be sent by either returning it from the message handler method or using + /// YieldWithResponse when using Tapeti Flow. These methods will respond directly + /// to the queue specified in the reply-to header automatically added by Tapeti. + /// [AttributeUsage(AttributeTargets.Class)] public class RequestAttribute : Attribute { diff --git a/Tapeti/Connection/TapetiPublisher.cs b/Tapeti/Connection/TapetiPublisher.cs index d3afeb6..8887b85 100644 --- a/Tapeti/Connection/TapetiPublisher.cs +++ b/Tapeti/Connection/TapetiPublisher.cs @@ -1,6 +1,8 @@ using System; +using System.Reflection; using System.Threading.Tasks; using RabbitMQ.Client; +using Tapeti.Annotations; namespace Tapeti.Connection { @@ -17,7 +19,7 @@ namespace Tapeti.Connection public Task Publish(object message) { - return workerFactory().Publish(message, null, false); + return workerFactory().Publish(message, null, IsMandatory(message)); } @@ -31,5 +33,11 @@ namespace Tapeti.Connection { return workerFactory().PublishDirect(message, queueName, properties, mandatory); } + + + private static bool IsMandatory(object message) + { + return message.GetType().GetCustomAttribute() != null; + } } }