public void OnMessageProcessingFailedDoesNotUpdateMessageVisibilityIfMovingMessageToDlq()
        {
            var message = this.CreateNewMessage();
            var messageBody = message.GetBody();
            var queueNames = new VersionedQueueNames(messageBody.GetType());
            var dlqUrl = "http://queue.com/dlq/";

            // GetOrCreateQueue
            // PublishMessage (To DLQ) -> Test that a new MessageId is assigned!
            // Delete Message
            this.SqsClient
                .Setup(x => x.GetOrCreateQueueUrl(queueNames.Dlq))
                .Returns(dlqUrl)
                .Verifiable();

            this.SqsClient
                .Setup(x => x.PublishMessage(dlqUrl, Convert.ToBase64String(message.ToBytes())))
                .Returns(new SendMessageResponse())
                .Verifiable();

            this.SqsClient
                .Setup(x => x.DeleteMessage(messageBody.QueueUrl, messageBody.ReceiptHandle))
                .Verifiable();

            this.MessageProcessor.OnMessageProcessingFailed(message, null, true);
            
            this.SqsClient.VerifyAll();
            
            // Verify that 'UpdateMessageVisibility' is NOT called.
            this.SqsClient.Verify(x => x.ChangeMessageVisibility(messageBody.QueueUrl, messageBody.ReceiptHandle, It.IsAny<decimal>()), Times.Never());
        }
        protected override void Init()
        {
            if (messageWorkers == null)
            {
                var workerBuilder   = new List <IMessageHandlerBackgroundWorker>();
                var queuesToMonitor = new Dictionary <string, Type>();

                foreach (var handler in this.MessageHandlerRegister.RegisteredHandlers)
                {
                    // TODO: Should produce the same results as the loop below, but use custom config constructs.
                    var messageType         = handler.Key;
                    var handlerRegistration = handler.Value;
                    var queueNames          = new VersionedQueueNames(messageType); //// new QueueNames(messageType);

                    if (OnlyEnablePriortyQueuesForTypes == null ||
                        OnlyEnablePriortyQueuesForTypes.Any(x => x == messageType))
                    {
                        // Create a priority queue, and associated message handlers
                        if (handlerRegistration.Configuration.NoOfThreads > 0)
                        {
                            // Called for each required background message handler.
                            handlerRegistration.Configuration.NoOfThreads.Times(i =>
                                                                                workerBuilder.Add(
                                                                                    this.BackgroundWorkerFactory.CreateMessageHandlerWorker(
                                                                                        handlerRegistration,
                                                                                        queueNames.Priority,
                                                                                        WorkerErrorHandler)));
                        }

                        queuesToMonitor.Add(queueNames.Priority, messageType);
                    }

                    if (handlerRegistration.Configuration.NoOfThreads == 0)
                    {
                        threadPoolHandlers.Add(messageType, handlerRegistration.MessageHandlerFactory.CreateMessageHandler());
                        threadPoolHandlerStats.Add(messageType, new ThreadPoolMessageHandlerStats(messageType.Name));
                    }
                    else
                    {
                        handlerRegistration.Configuration.NoOfThreads.Times(i =>
                                                                            workerBuilder.Add(
                                                                                this.BackgroundWorkerFactory.CreateMessageHandlerWorker(
                                                                                    handlerRegistration,
                                                                                    queueNames.In,
                                                                                    WorkerErrorHandler)));
                    }

                    queuesToMonitor.Add(queueNames.In, messageType);
                }

                messageWorkers = workerBuilder.ToArray();

                // Create the background worker thread(s) to monitor message queue(s)
                queueWorkers = this.BackgroundWorkerFactory.CreateQueueHandlerWorkers(
                    queuesToMonitor,
                    this.MessageHandlerRegister.RegisteredHandlers,
                    QueueWorkerErrorHandler).ToArray();

                queueWorkerIndexMap = new Dictionary <string, int[]>();
                for (var i = 0; i < messageWorkers.Length; i++)
                {
                    var worker = messageWorkers[i];

                    int[] workerIds;
                    if (!queueWorkerIndexMap.TryGetValue(worker.QueueName, out workerIds))
                    {
                        queueWorkerIndexMap[worker.QueueName] = new[] { i };
                    }
                    else
                    {
                        workerIds = new List <int>(workerIds)
                        {
                            i
                        }.ToArray();
                        queueWorkerIndexMap[worker.QueueName] = workerIds;
                    }
                }
            }
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("Client.exe.config"));
            // var Log4Net = log4net.LogManager.GetLogger(typeof(Program));

            // For some reason, in a console app you need to set the LogFactory manually!
            ServiceStack.Logging.LogManager.LogFactory = new ServiceStack.Logging.Log4Net.Log4NetFactory("Client.exe.config");
            var Log = LogManager.GetLogger(typeof(Program));
            /*
            Log.Info("Info");
            Log.Debug("Debug");
            Log.Warn("Warn");
            Log.Error("Error");
            Log.Fatal("Fatal");
            */

            Log.Info("===== Starting Client =====");
            IMessageQueueClient c;
            AwsSqsMessageQueueClient sqsMessageQueueClient;

            // var svc = new InMemoryTransientMessageService();
            // var svc = new RedisMqServer(new PooledRedisClientManager(new[] { "localhost:6379" }));
            var svc = MessageServerFactory.CreateMessageService();

            // Register the handlers before creating any client objects.
            using (var messageQueueClient = svc.MessageFactory.CreateMessageQueueClient())
            {
                Log.Info("===== Message Queue Client Started =====");
                Log.Info("Type 'EXIT' to close.");
                // svc.Start();

                while (true)
                {
                    // The MqClient should result in a message in the out q, as the messages are not configured to be one-way
                    messageQueueClient.Publish(new Hello {Text = "This comes from the client"});

                    // This message should return a response.
                    messageQueueClient.Publish(new Hello3 {Text = "This comes from the client #3"});

                    Log.Info("Waiting for message response.");
                    var responseQueueName = new VersionedQueueNames(typeof (Hello3Response)).In; // QueueNames<Hello3Response>.In;
                    var response = messageQueueClient.Get(responseQueueName, TimeSpan.FromSeconds(20));
                    if (response != null)
                    {
                        var obj = response.ToMessage<Hello3Response>().GetBody();
                        Log.InfoFormat("Message response received: {0}", obj.ResponseText);
                        sqsMessageQueueClient = messageQueueClient as AwsSqsMessageQueueClient;
                        if (sqsMessageQueueClient != null)
                        {
                            sqsMessageQueueClient.DeleteMessageFromQueue(responseQueueName, obj.ReceiptHandle);                            
                        }

                        Log.InfoFormat("Deleted response message from queue: {0}", obj.QueueName);
                    }
                    else
                    {
                        Log.Info("No message response received.");
                    }

                    // ================== REQ/REPLY MQ ===============
                    var uniqueCallbackQ = "mq:c1" + ":" + Guid.NewGuid().ToString("N");
                    messageQueueClient.Publish(new Message<Hello4>(new Hello4 {Text = "This comes from the client #4"})
                        {
                            ReplyTo = uniqueCallbackQ
                        });

                    Log.Info("Waiting for message response.");
                    var uniqueResponse = messageQueueClient.Get(uniqueCallbackQ, TimeSpan.FromSeconds(20));
                    if (uniqueResponse != null)
                    {
                        var obj = uniqueResponse.ToMessage<Hello4Response>().GetBody();
                        Log.InfoFormat("Message response received: {0}", obj.ResponseText);
                        sqsMessageQueueClient = messageQueueClient as AwsSqsMessageQueueClient;
                        if (sqsMessageQueueClient != null)
                        {
                            sqsMessageQueueClient.DeleteMessageFromQueue(uniqueCallbackQ, obj.ReceiptHandle);
                        }

                        Log.InfoFormat("Deleted response message from queue: {0}", obj.QueueName);
                    }
                    else
                    {
                        Log.Info("No message response received.");
                    }

                    // Optionally, delete the mq.
                    sqsMessageQueueClient = messageQueueClient as AwsSqsMessageQueueClient;
                    if (sqsMessageQueueClient != null)
                    {
                        sqsMessageQueueClient.DeleteQueue(uniqueCallbackQ);
                    }
                    // ===============================================

                    // TODO: TEST - messageQueueClient.WaitForNotifyOnAny()

                    var text = Console.ReadLine() ?? string.Empty;
                    if (text.ToUpperInvariant() == "EXIT")
                    {
                        break;
                    }
                }
            }

            Log.Info("Stopping Client.");            
        }