Quantcast
Channel: Service Bus forum
Viewing all articles
Browse latest Browse all 1916

Messages not expiring if EnableDeadLetteringOnMessageExpiration=true

$
0
0

Has anyone had issues with message expiration when using the EnableDeadLetteringOnMessageExpiration option in Service Bus for Windows Server?  What I'm seeing is that message expiration works as expected with this option set to false (the messages disappear from the queue and, of course, are not moved to the dead letter queue).  However, once I set the option to true, the messages do not seem to expire at all.  The code I'm using to test this is below.  If I run it as is, no message is ever picked up.  If I uncomment the "Testing only" block, the message is picked up from the queue, which means it did not expire when it should have.  On the other hand, if I leave EnableDeadLetteringOnMessageExpiration set to false, the message is not picked up from either the queue or the dead letter queue (as is expected).

Thank you,

Rick

	    const string queueName = "QueueDeadLetterDemo";
            //Create a NamespaceManager instance (for management operations)
            var namespaceManager = NamespaceManager.Create();


            var queue = new QueueDescription(queueName)
                {
                    //Make sure expired messages go to the dead letter queue
                    EnableDeadLetteringOnMessageExpiration = true,
                    //Set the expiration to 1 second so all messages expire quickly (1 second is the minimum for this)
                    //DefaultMessageTimeToLive = TimeSpan.FromSeconds(1)
                };

            
            if (namespaceManager.QueueExists(queueName))
            {
                namespaceManager.DeleteQueue(queueName);
            }
            queue = namespaceManager.CreateQueue(queue);

            Console.WriteLine("EnableDeadLetterOnExpiration = {0}", queue.EnableDeadLetteringOnMessageExpiration);
            //Console.WriteLine("DefaultMessageTimeToLive = {0}", queue.DefaultMessageTimeToLive);

            //Create a MessagingFactory instance (for sending and receiving messages)
            var messageFactory = MessagingFactory.Create();
            
            //Create a queue client to send and receive messages to and from the queue
            var queueClient = messageFactory.CreateQueueClient(queueName);

            //Create a simple brokered message and send it to the queue
            var sendMessage = new BrokeredMessage("Hello World!");
            //Set an expiration on the message
            sendMessage.TimeToLive = TimeSpan.FromSeconds(1);
            queueClient.Send(sendMessage);
            Console.WriteLine("Message sent: Body = {0}", sendMessage.GetBody<string>());
            Console.WriteLine();
            Console.WriteLine("Waiting to begin receiving...");
            System.Threading.Thread.Sleep(5000);
            Console.WriteLine("Receiving...");

            ////Testing only.  Verify the message did expire
            //var receivedMessage = queueClient.Receive(TimeSpan.FromSeconds(5));
            //if (receivedMessage != null)
            //{
            //    Console.WriteLine("Message received: Body = {0}", receivedMessage.GetBody<string>());
            //    receivedMessage.Complete();
            //}

            //Create a queue client for the dead letter queue
            string deadLetterQueuePath = QueueClient.FormatDeadLetterPath(queueName);
            var deadletterQueueClient = messageFactory.CreateQueueClient(deadLetterQueuePath);

            //Receive the message from the dead letter queue
            BrokeredMessage deadLetterMessage = null;

            while (deadLetterMessage == null)
            {
                deadLetterMessage = deadletterQueueClient.Receive(TimeSpan.FromSeconds(5));
                if (deadLetterMessage != null)
                {
                    Console.WriteLine("Message received: Body = {0}", deadLetterMessage.GetBody<string>());
                    deadLetterMessage.Complete();
                }
                else
                {
                    Console.WriteLine("No message received yet... waiting...");
                    System.Threading.Thread.Sleep(2000);
                    Console.WriteLine("Trying again...");
                }
            }
            //Close the connection to the Service Bus
            messageFactory.Close();

            Console.WriteLine("Press Enter to close.");
            Console.ReadLine();



Viewing all articles
Browse latest Browse all 1916

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>