servicebus messagecount check returns true 即使它是 0
servicebus messagecount check returns true even though it's 0
我有一个功能可以检查我的服务总线中是否还有一条消息,它会不断检查。
获取ammount的函数是:
public int GetActiveMessageCount()
{
var connectionString = azureConnectionStringWithoutEntityPath;
long messageCount = 0;
try
{
var nameSpaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
messageCount = nameSpaceManager.GetQueue(azureQueueName).MessageCountDetails.ActiveMessageCount;
}
catch
{
throw;
}
return (int)messageCount;
}
支票是:
public bool MessageCountBiggerThenZero()
{
int messageCount = 0;
try
{
messageCount = this.GetActiveMessageCount();
}
catch (Exception e)
{
Debug.WriteLine("An error occured while checking if the messageValue is bigger then zero and not null saying : " + e);
}
return messageCount > 0;
}
当消息为零时,这在开始时完美运行,但在它开始接收消息并传递最后一条消息后,它仍然通过 MessageCountBiggerThenZero 检查。
有人知道这是怎么回事吗? serviceBus return 在最后一条消息之后是否有其他值?
编辑:
更深入地了解检查(因此即使不再有消息,它仍然通过 if 语句):
public object RecieveOneMessageFromServiceBus()
{
var client = QueueClient.CreateFromConnectionString(azureConnectionString);
BrokeredMessage brokermessage = null;
client.PrefetchCount = 1; // get 1 message at a time
if (MessageCountBiggerThenZero())
{
try
{
// try to recieve message
}
catch
{
throw;
}
}
else
{
return null;
}
}
根据您的代码,我假设您没有调用 BrokeredMessage.Complete,这可能会在您处理消息后将消息标记为已处理和删除。我修改了您的代码并添加了日志以打印当前活动消息,如下所示:
public static object RecieveOneMessageFromServiceBus()
{
var client = QueueClient.CreateFromConnectionString(azureConnectionString);
client.PrefetchCount = 1; // get 1 message at a time
if (MessageCountBiggerThenZero())
{
try
{
Console.WriteLine("begin retrieving a message...");
var message = client.Receive();
Console.WriteLine($"received message with MessageId:{message.MessageId}");
//after receive the message for handling
Console.WriteLine($"after received a message in processing, active messages count:{GetActiveMessageCount()}");
//after handled the message
message.Complete();
Console.WriteLine($"invoke message.Complete() to complete the received message");
//after receive the message for handling
Console.WriteLine($"after Complete() the message, active messages count:{GetActiveMessageCount()}");
}
catch
{
throw;
}
}
return null;
}
此外,您可以登录到 Azure 门户并检查服务总线队列概述下的 ACTIVE MESSAGE COUNT,如下所示:
此外,您可以参考Receive messages from the queue without checking the current active messages by yourself, aslo you could refer to this 的官方教程。
好的,我似乎已经修复了它,但我不知道如何以及为什么。我确实按照布鲁斯所说的那样实施了它,但这没有用。也许完整的方法发送速度不够快或者其他事情正在发生,但现在我改变了我的接收方法来接收和删除并添加了一个计时器来获取大约 5 秒的消息,例如:
var client = QueueClient.CreateFromConnectionString(azureConnectionString,ReceiveMode.ReceiveAndDelete);
brokermessage = client.Receive(TimeSpan.FromSeconds(5));
有了这段代码,它似乎可以工作,任何人都可以向我解释为什么会非常需要。
编辑:
有关如何更好地实施此功能的额外说明,请参阅下面的评论
我有一个功能可以检查我的服务总线中是否还有一条消息,它会不断检查。
获取ammount的函数是:
public int GetActiveMessageCount()
{
var connectionString = azureConnectionStringWithoutEntityPath;
long messageCount = 0;
try
{
var nameSpaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
messageCount = nameSpaceManager.GetQueue(azureQueueName).MessageCountDetails.ActiveMessageCount;
}
catch
{
throw;
}
return (int)messageCount;
}
支票是:
public bool MessageCountBiggerThenZero()
{
int messageCount = 0;
try
{
messageCount = this.GetActiveMessageCount();
}
catch (Exception e)
{
Debug.WriteLine("An error occured while checking if the messageValue is bigger then zero and not null saying : " + e);
}
return messageCount > 0;
}
当消息为零时,这在开始时完美运行,但在它开始接收消息并传递最后一条消息后,它仍然通过 MessageCountBiggerThenZero 检查。
有人知道这是怎么回事吗? serviceBus return 在最后一条消息之后是否有其他值?
编辑:
更深入地了解检查(因此即使不再有消息,它仍然通过 if 语句):
public object RecieveOneMessageFromServiceBus()
{
var client = QueueClient.CreateFromConnectionString(azureConnectionString);
BrokeredMessage brokermessage = null;
client.PrefetchCount = 1; // get 1 message at a time
if (MessageCountBiggerThenZero())
{
try
{
// try to recieve message
}
catch
{
throw;
}
}
else
{
return null;
}
}
根据您的代码,我假设您没有调用 BrokeredMessage.Complete,这可能会在您处理消息后将消息标记为已处理和删除。我修改了您的代码并添加了日志以打印当前活动消息,如下所示:
public static object RecieveOneMessageFromServiceBus()
{
var client = QueueClient.CreateFromConnectionString(azureConnectionString);
client.PrefetchCount = 1; // get 1 message at a time
if (MessageCountBiggerThenZero())
{
try
{
Console.WriteLine("begin retrieving a message...");
var message = client.Receive();
Console.WriteLine($"received message with MessageId:{message.MessageId}");
//after receive the message for handling
Console.WriteLine($"after received a message in processing, active messages count:{GetActiveMessageCount()}");
//after handled the message
message.Complete();
Console.WriteLine($"invoke message.Complete() to complete the received message");
//after receive the message for handling
Console.WriteLine($"after Complete() the message, active messages count:{GetActiveMessageCount()}");
}
catch
{
throw;
}
}
return null;
}
此外,您可以登录到 Azure 门户并检查服务总线队列概述下的 ACTIVE MESSAGE COUNT,如下所示:
此外,您可以参考Receive messages from the queue without checking the current active messages by yourself, aslo you could refer to this
好的,我似乎已经修复了它,但我不知道如何以及为什么。我确实按照布鲁斯所说的那样实施了它,但这没有用。也许完整的方法发送速度不够快或者其他事情正在发生,但现在我改变了我的接收方法来接收和删除并添加了一个计时器来获取大约 5 秒的消息,例如:
var client = QueueClient.CreateFromConnectionString(azureConnectionString,ReceiveMode.ReceiveAndDelete);
brokermessage = client.Receive(TimeSpan.FromSeconds(5));
有了这段代码,它似乎可以工作,任何人都可以向我解释为什么会非常需要。
编辑:
有关如何更好地实施此功能的额外说明,请参阅下面的评论