英文:
How do I keep my consumer listening the messages on Azure Sevice Bus using Azure sdk for Golang v0.3.1?
问题
我一直在使用azure-sdk-for-go/sdk/messaging/azservicebus v0.3.1模块将我的消费者与Azure Service Bus连接起来,但是实现的代码只接收固定数量的消息,然后停止应用程序,我希望能让消费者一直监听队列。以下是我的代码:
client, err := azservicebus.NewClientFromConnectionString("连接字符串", nil)
if err != nil {
log.Fatalf("创建Service Bus客户端失败:%s", err.Error())
}
receiver, err := client.NewReceiverForQueue("队列", nil)
if err != nil {
log.Fatalf("创建消费者失败:%s", err.Error())
}
messages, err := receiver.ReceiveMessages(context.TODO(), 10, nil)
if err != nil {
log.Fatalf("接收消息失败:%s", err.Error())
}
for _, message := range messages {
body, err := message.Body()
if err != nil {
log.Fatalf("解析消息正文失败:%s", err.Error())
}
fmt.Println("消息 --->", string(body))
err = receiver.CompleteMessage(context.TODO(), message)
if err != nil {
log.Fatalf("完成消息失败:%s", err.Error())
}
fmt.Printf("接收并完成消息\n")
}
英文:
I've been using the module azure-sdk-for-go/sdk/messaging/azservicebus v0.3.1 to connect my consumer with Azure Service Bus but the code implemented only receive a fixed number of message and then stop the application and I'would like to keep the consumer listening the queue. Follow my code:
client, err := azservicebus.NewClientFromConnectionString("Connection String", nil)
if err != nil {
log.Fatalf("Failed to create Service Bus Client: %s", err.Error())
}
receiver, err := client.NewReceiverForQueue("queue", nil)
if err != nil {
log.Fatalf("Failed to create Consumer: %s", err.Error())
}
messages, err := receiver.ReceiveMessages(context.TODO(), 10, nil)
if err != nil {
log.Fatalf("Failed to receive Messages: %s", err.Error())
}
for _, message := range messages {
body, err := message.Body()
if err != nil {
log.Fatalf("Failed to parse message body: %s", err.Error())
}
fmt.Println("Message --->", string(body))
err = receiver.CompleteMessage(context.TODO(), message)
if err != nil {
log.Fatalf("Failed to complete message: %s", err.Error())
}
fmt.Printf("Received and completed message\n")
}
答案1
得分: 2
你已经掌握了99%的代码 - 如你所指出的,ReceiveMessages
只返回你请求的数量。
所以你只需要将你的逻辑包装在一个循环中,这样就可以持续运行:
for {
messages, err := receiver.ReceiveMessages(context.TODO(), 10, nil)
if err != nil {
log.Fatalf("Failed to receive Messages: %s", err.Error())
}
for _, message := range messages {
body, err := message.Body()
if err != nil {
log.Fatalf("Failed to parse message body: %s", err.Error())
}
fmt.Println("Message --->", string(body))
err = receiver.CompleteMessage(context.TODO(), message)
if err != nil {
log.Fatalf("Failed to complete message: %s", err.Error())
}
}
}
顺便说一下,0.3.2刚刚发布,其中包含了一些修复,你会对它们感兴趣,因为它们确实会影响到ReceiveMessages
。
英文:
You have 99% of the code - as you noted ReceiveMessages
only returns the amount you requested.
So you just want to wrap your logic in a loop so you can run it continually:
for {
messages, err := receiver.ReceiveMessages(context.TODO(), 10, nil)
if err != nil {
log.Fatalf("Failed to receive Messages: %s", err.Error())
}
for _, message := range messages {
body, err := message.Body()
if err != nil {
log.Fatalf("Failed to parse message body: %s", err.Error())
}
fmt.Println("Message --->", string(body))
err = receiver.CompleteMessage(context.TODO(), message)
if err != nil {
log.Fatalf("Failed to complete message: %s", err.Error())
}
}
}
BTW, 0.3.2 was just released and it contains some fixes that you'll be interested in as they do affect ReceiveMessages.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论