How do I keep my consumer listening the messages on Azure Sevice Bus using Azure sdk for Golang v0.3.1?

huangapple go评论95阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2021年12月9日 03:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/70280842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定