diff --git a/src/Helsenorge.Messaging/MessagingServer.cs b/src/Helsenorge.Messaging/MessagingServer.cs index 02f6820c..1c179d40 100644 --- a/src/Helsenorge.Messaging/MessagingServer.cs +++ b/src/Helsenorge.Messaging/MessagingServer.cs @@ -194,6 +194,7 @@ public async Task StartAsync() if (!await HasCommonAncestorAsync(AmqpCore.Settings.MyHerIds.ToArray())) throw new MessagingException( "There must be a common set of ancestor queues when receiving from multiple HER-Ids."); + if (!string.IsNullOrWhiteSpace(Settings.AmqpSettings.Synchronous.StaticReplyQueue)) { var queueNames = new QueueNames @@ -595,12 +596,18 @@ async Task IMessagingNotification.NotifyUnhandledExceptionAsync(IAmqpMessage mes private async Task HasCommonAncestorAsync(int[] herIds) { - if (herIds.Count() <= 1) + if (herIds.Length <= 1) + { return true; + } var communicationPartyDetailsList = new List(); foreach (var herId in herIds) - communicationPartyDetailsList.Add(await AmqpCore.AddressRegistry.FindCommunicationPartyDetailsAsync(herId)); + { + communicationPartyDetailsList.Add( + await AmqpCore.AddressRegistry.FindCommunicationPartyDetailsAsync(herId) + ); + } var queueNames = GetCommonAncestor(communicationPartyDetailsList); @@ -638,7 +645,10 @@ private async Task GetCommonAncestorAsync(IEnumerable herIds) { var communicationPartyDetailsList = new List(); foreach (var herId in herIds) + { communicationPartyDetailsList.Add(await AmqpCore.AddressRegistry.FindCommunicationPartyDetailsAsync(herId)); + } + return GetCommonAncestor(communicationPartyDetailsList); } diff --git a/src/Helsenorge.Messaging/MessagingSettings.cs b/src/Helsenorge.Messaging/MessagingSettings.cs index be42f278..1522c129 100644 --- a/src/Helsenorge.Messaging/MessagingSettings.cs +++ b/src/Helsenorge.Messaging/MessagingSettings.cs @@ -131,7 +131,6 @@ public AmqpSettings(MessagingSettings settings) /// Gets or set the Type of Message Broker we are using /// public MessageBrokerDialect MessageBrokerDialect { get; set; } = MessageBrokerDialect.RabbitMQ; - /// /// Gets or sets the connection string /// @@ -188,7 +187,6 @@ public AmqpSettings(MessagingSettings settings) /// If true logs every the MessageListener has finished a read call to the broker. /// public bool LogReadTime { get; set; } = false; - /// /// The HER-ids we are representing. /// @@ -242,8 +240,14 @@ public class SynchronousSettings /// The timout for synchronous calls /// public TimeSpan CallTimeout { get; set; } = TimeSpan.FromSeconds(15); - + /// + /// Specify the reply queue you are listening on if you have implemented + /// public string StaticReplyQueue { get; set; } + /// + /// Enable if you want to skip validation of the . Default false. + /// + public bool SkipReplyQueueMappingValidation { get; set; } = false; internal SynchronousSettings() { } @@ -253,6 +257,11 @@ internal void Validate() if (ReadTimeout == TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(ReadTimeout)); if (CallTimeout == TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(CallTimeout)); + if (SkipReplyQueueMappingValidation) + { + return; + } + if (FindReplyQueueForMe() == null) { throw new InvalidOperationException("Could not determine reply to queue for this server"); diff --git a/test/Helsenorge.Messaging.Tests/Amqp/Senders/SynchronousSendTests.cs b/test/Helsenorge.Messaging.Tests/Amqp/Senders/SynchronousSendTests.cs index 958895c0..a11190e9 100644 --- a/test/Helsenorge.Messaging.Tests/Amqp/Senders/SynchronousSendTests.cs +++ b/test/Helsenorge.Messaging.Tests/Amqp/Senders/SynchronousSendTests.cs @@ -72,7 +72,36 @@ public void Send_Synchronous_OK() // message should be gone from our sync reply Assert.AreEqual(0, MockFactory.Helsenorge.SynchronousReply.Messages.Count); } - + + [TestMethod] + public void Send_Synchronous_SingleSyncreplyQueueEnabled_True_OK() + { + Settings.AmqpSettings.Synchronous.SkipReplyQueueMappingValidation = true; + var message = CreateMessage(); + + // post a reply on the syncreply queue before posting the actual message + var mockMessage = CreateMockMessage(message); + mockMessage.To = MockFactory.Helsenorge.SynchronousReply.Name; + mockMessage.ReplyTo = MockFactory.OtherParty.Synchronous.Name; + mockMessage.Queue = MockFactory.Helsenorge.SynchronousReply.Messages; + MockFactory.Helsenorge.SynchronousReply.Messages.Add(mockMessage); + + var response = RunAndHandleException(Client.SendAndWaitAsync(message)); + + var logProcessedMessage = MockLoggerProvider.Entries.Where(l => l.LogLevel == LogLevel.Information + && l.Message.Contains($"Removing processed message {mockMessage.MessageId} from Herid {MockFactory.OtherHerId} from queue {MockFactory.Helsenorge.SynchronousReply.Name}. Correlation = {message.MessageId}")); + Assert.AreEqual(1, logProcessedMessage.Count()); + + var logEntry = MockLoggerProvider.Entries.Where(l => l.LogLevel == LogLevel.Information + && l.Message.StartsWith("ResponseTime") + && l.Message.EndsWith(" ms")); + Assert.AreEqual(1, logEntry.Count()); + // make sure the content is what we expect + Assert.AreEqual(GenericResponse.ToString(), response.ToString()); + // message should be gone from our sync reply + Assert.AreEqual(0, MockFactory.Helsenorge.SynchronousReply.Messages.Count); + } + [TestMethod] [ExpectedException(typeof(MessagingException))] public void Send_Synchronous_ErrorQueue()