Azure服务总线丢弃消息?
我正在尝试在Azure中构建一个简单的WebAPI REST服务,后端有一个服务总线队列工作器。我可以从Web API向工作人员发送一条消息。然而,我试图发送更多的信息,只是为了看看一切是如何运作的。因此,我创建了一个简单的控制器,如下所示:Azure服务总线丢弃消息?,azure,azureservicebus,Azure,Azureservicebus,我正在尝试在Azure中构建一个简单的WebAPI REST服务,后端有一个服务总线队列工作器。我可以从Web API向工作人员发送一条消息。然而,我试图发送更多的信息,只是为了看看一切是如何运作的。因此,我创建了一个简单的控制器,如下所示: for (int i = 0; i < 100; i++) { var msg = new BrokeredMessage("Ping"); BioConnector.QueueConnector.OrdersQueueClient.
for (int i = 0; i < 100; i++)
{
var msg = new BrokeredMessage("Ping");
BioConnector.QueueConnector.OrdersQueueClient.Send(msg);
}
for(int i=0;i<100;i++)
{
var msg=新代理消息(“Ping”);
BioConnector.QueueConnector.OrdersQueueClient.Send(msg);
}
当我呼叫控制器时,我只收到工作人员接收到的大约1/2的消息。其余的似乎都被删除了。我在使用发布的示例代码获取大约一半的消息时遇到了问题,所以我编写了自己的测试代码。我已经尝试了超过100条队列消息,并且始终具有100%的发送/接收奇偶校验。也许您对代码也有类似的问题
<appSettings>
<add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://blahblah.servicebus.windows.net/;SharedSecretIssuer=owner;SharedSecretValue=pDk0b....=" />
</appSettings>
class Program
{
static void Main(string[] args)
{
string connectionString = ConfigurationSettings.AppSettings["Microsoft.ServiceBus.ConnectionString"];
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
QueueDescription queueDesc = new QueueDescription("TestQueue");
if (!namespaceManager.QueueExists(queueDesc.Path))
{
namespaceManager.CreateQueue(queueDesc);
}
QueueClient topicClient = QueueClient.CreateFromConnectionString(connectionString, queueDesc.Path);
int sentMsgCount = 0;
int recdMsgCount = 0;
for (int i = 0; i < 100; i++)
{
BrokeredMessage msg = new BrokeredMessage("Test message " + i);
topicClient.Send(msg);
sentMsgCount++;
Console.WriteLine("Sent Message: " + msg);
}
QueueClient subClient = QueueClient.CreateFromConnectionString(connectionString, queueDesc.Path);
bool moreMessages = true;
while (moreMessages)
{
BrokeredMessage recdMsg = subClient.Receive(TimeSpan.FromSeconds(3));
if (recdMsg != null)
{
Console.WriteLine("Received Message: " + recdMsg);
recdMsgCount++;
recdMsg.Complete();
}
else
{
moreMessages = false;
}
}
Console.WriteLine("# of sent msgs: " + sentMsgCount + ", # of rec'd msgs: " + recdMsgCount);
Console.Read();
}
}
类程序
{
静态void Main(字符串[]参数)
{
string connectionString=ConfigurationSettings.AppSettings[“Microsoft.ServiceBus.connectionString”];
var namespaceManager=namespaceManager.CreateFromConnectionString(connectionString);
QueueDescription queueDesc=新的QueueDescription(“TestQueue”);
如果(!namespaceManager.QueueExists(queueDesc.Path))
{
namespaceManager.CreateQueue(queueDesc);
}
QueueClient topicClient=QueueClient.CreateFromConnectionString(connectionString,queueDesc.Path);
int sentMsgCount=0;
int recdMsgCount=0;
对于(int i=0;i<100;i++)
{
BrokeredMessage msg=新BrokeredMessage(“测试消息”+i);
topicClient.Send(msg);
sentMsgCount++;
Console.WriteLine(“已发送消息:+msg”);
}
QueueClient子客户端=QueueClient.CreateFromConnectionString(connectionString,queueDesc.Path);
bool-moreMessages=true;
while(更多信息)
{
BrokeredMessage recdMsg=子客户端接收(TimeSpan.FromSeconds(3));
if(recdMsg!=null)
{
Console.WriteLine(“收到的消息:+recdMsg”);
recdMsgCount++;
recdMsg.Complete();
}
其他的
{
moreMessages=false;
}
}
控制台写入线(“#已发送MSG:+sentMsgCount+”,#已接收MSG:+recdMsgCount”);
Console.Read();
}
}
这是一个奇怪的问题。通过一次随机的“尝试”,我最终改变了队列的字符串名称,然后一切又开始工作了。除了队列的名称之外,我什么也没做更改——根本没有更改任何配置参数
Azure上的特定队列似乎有问题 Azure服务总线提供持久的消息传递,因此您不会丢失任何消息。需要进一步调查的一些项目: 1) 是否存在从该队列中提取消息的工作人员角色的另一个实例 2) 您是否使用peek lock作为接收模式,因为这将是保证至少一次交付的唯一方法。接收和删除模式没有保证 3) 消息是否由于消息过期或超过最大传递次数而进入死信队列,即它们已收到但未完成多次 4) 如果上述任何一项都不适用,则提出支持通知单,Azure产品团队可以调查症状,因为正如我所提到的,这是一个持久的消息传递系统,因此不会“丢失”任何消息。我的项目中有,并使用
QueueClient.Send()
发送消息,面临相同的消息丢失问题
我彻底解决问题的解决方案是:
在发送端,我不得不这样做
在接收端,我是这样提取消息体的:
using (var stream = brokeredMessage.GetBody<Stream>())
{
using (var streamReader = new StreamReader(stream, Encoding.UTF8))
{
var msg = streamReader.ReadToEnd();
// Convert the JSON message to an object
// var obj = JsonConvert.DeserializeObject<ObjectType>(msg);
}
}
使用(var stream=brokeredMessage.GetBody())
{
使用(var streamReader=newstreamreader(stream,Encoding.UTF8))
{
var msg=streamReader.ReadToEnd();
//将JSON消息转换为对象
//var obj=JsonConvert.DeserializeObject(msg);
}
}
以上所有内容都不适合我。在使用服务总线主题/订阅输出时,如果只写入多条消息中的一条消息,则会进入主题。我正在使用服务总线主题触发器,但已禁用,否则该1条消息也将丢失。令人沮丧的是,当事情紧急时,还存在其他问题,请参见:是。天哪,我在这件事上浪费了好几天时间——我一直在丢失消息(或者至少SDK一直在默默地删除消息),直到我更改了队列名称,使队列中没有连字符。现在我得到了所有消息,特别是当队列中已经有消息并且应用程序启动时。
using (var stream = brokeredMessage.GetBody<Stream>())
{
using (var streamReader = new StreamReader(stream, Encoding.UTF8))
{
var msg = streamReader.ReadToEnd();
// Convert the JSON message to an object
// var obj = JsonConvert.DeserializeObject<ObjectType>(msg);
}
}