问题描述

在从Storage Account 队列中获取数据(Queue),在门户中,明显看见有数据,但是通过消费端代码去获取的时候,就是无法获取到有效数据的情况。获取消息的代码如下:

【Azure 存储服务】Azure Storage Account Queue中因数据格式无法处理而在一个小时内不在可见的问题_sed

 

问题解答

经过对 receiveMessages 方法定义的查询,第二个参数,第三个参数的两个时间表示的意思为 消息在Queue中不可见的时间,和消息被处理的超时时间。如上段代码所示,当获取到消息后,如果10秒没有处理完消息,消息就会超时。但是因为第二个参数设置的是3600秒(1小时),所以它会有一个小时在Queue中不可见。 

虽然问题中消息不能再10秒中处理完是因为数据格式的问题,获取的数据格式为JSON,但是下游系统需要处理的是Based64格式。但是第二个参数3600秒的设定是不合理。

最建议的参数值为:第二(visibilityTimeout),第三(timeout ) 参数的时间一样。

 

参考资料

receiveMessages : ​​https://learn.microsoft.com/zh-cn/java/api/com.azure.storage.queue.QueueClient?view=azure-java-stable#com-azure-storage-queue-queueclient-receivemessages(java-lang-integer-java-time-duration-java-time-duration-com-azure-core-util-context)​

public PagedIterable receiveMessages(Integer maxMessages, Duration visibilityTimeout, Duration timeout, Context context)

Retrieves up to the maximum number of messages from the queue and hides them from other operations for the timeout period.

Parameters:

maxMessages - Optional. Maximum number of messages to get, if there are less messages exist in the queue than requested all the messages will be returned. If left empty only 1 message will be retrieved, the allowed range is 1 to 32 messages.

visibilityTimeout - Optional. The timeout period for how long the message is invisible in the queue. If left empty the received messages will be invisible for 30 seconds. The timeout must be between 1 second and 7 days.

timeout - An optional timeout applied to the operation. If a response is not returned before the timeout concludes a RuntimeException will be thrown.

context - Additional context that is passed through the Http pipeline during the service call.