質問

Problem:

I am reading messages from a Azure Storage Queue and then inserting them into a Storage Table using a Worker Role.

I want to read in messages but only process them if there are at least 100 (this is to optimize the Storage Table batch insert which is occurring). If there are less than 100 messages, then I want to cancel the message processing and make them immediately visible on the queue again for the next queue read.

Question:

Is it possible to mark a message which has just been read by CloudQueue.GetMessages(...) as visible without having to wait for the timeout to expire?

Code: (in WorkerRole.cs)

public override void Run()
{
    while (true)
    {
        var messages = queue.GetMessages(100);

        if (messages.Count() >= 100)
        {
            // This will process, insert into a table, and delete from the queue
            ProcessMessages(messages);
        }
        else
        {
            //!!! MARK MESSAGES AS VISIBLE ON THE QUEUE
            System.Threading.Thread.Sleep(1000);
        }
    }
}

Thanks

役に立ちましたか?

解決

You can check the queue's `ApproximateMessageCount' property (details here), which will give you a rough idea how many messages are waiting in the queue.

Also: you can set a message's invisibility timeout to something small (maybe 5-10 seconds?). After that period, the message becomes visible again. You can also modify invisibility timeout to something shorter after you read it.

Just remember that reading from the queue counts as a transaction, as does updating messages (e.g. updating invisibility timeout).

Waiting for 100 messages may be a non-optimal optimization. Oh, and GetMessages()(details here) is limited to 32 messages, so it doesn't make sense to wait for 100. Also: Transactions are really, really cheap (a penny per 100K transactions). I don't necessarily see the value here.

他のヒント

Reset the expire time to 0.0. That will hopefully do the trick.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top