简体   繁体   中英

how can i set messageTTL in an azure queue trigger?

Question:

https://docs.microsoft.com/en-us/rest/api/storageservices/version-2017-07-29 says that "messagettl" can be set to specific values over 7 days, or -1.
How can I post a message to a queue with a custom timeout? (specifically -1)

Background:

We create messages via posting to an IAsynCollector similar to the example at https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue#output---c-script-example

We recently processed a very large backlog of queue messages, each holding the name of an azure blob for processing.

The backlog took longer to process than expected, and some of the blobs were not processed. We believe they timed out as there were no messages poisoned, but queue timeout isn't a logged event.

The "Expiration Time" at the head of the queue was always more than 1 day in the future, however we received some transient errors due to a downstream system being overloaded. I think that those messages went to the back of the queue and timed out.

If you just want to set the queue message expiration time(TTL), you could also bind the queue to CloudQueue . Then use the AddMessageAsync() method to publish message with TTL timespan.

The below is my test code, and if you want to set the TTL to -1 you should set the value to TimeSpan.MaxValue it will act like -1 .

public static class Function1
    {
        public class CustomQueueMessage
        {
            public string PersonName { get; set; }
            public string Title { get; set; }
        }
        [FunctionName("Function1")]
        public static void Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            [Queue("myqueue")]CloudQueue myQueueItems,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            var ms1 = new CustomQueueMessage { PersonName = "You", Title = "None" };
            CloudQueueMessage q1 = new CloudQueueMessage(JsonConvert.SerializeObject(ms1));
            myQueueItems.AddMessageAsync(q1, TimeSpan.FromDays(11),null,null,null);
            CloudQueueMessage q2 = new CloudQueueMessage("test");
            myQueueItems.AddMessageAsync(q2, TimeSpan.MaxValue , null, null,null);
        }
    }

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM