【问题标题】:Receive information about a topic subscription on Azure Service Bus接收有关 Azure 服务总线上的主题订阅的信息
【发布时间】:2017-08-30 07:42:00
【问题描述】:

我使用Azure Service Bus 中的过滤器订阅了一个主题,使用Python 3.x 开发,当我等待发送到该主题的信息(通过过滤器的信息)时,我无法接收它。

我需要创建一个一直在监听的守护进程,当我收到该信息时,我会将其发送到我的应用程序的内部服务,因此接收器在循环内的线程中运行 While True

我用来接收消息的代码如下:

while True:
    msg = bus_service.receive_subscription_message(topic_name, subscription_name, peek_lock=True)
    print('Mensaje Recibido-->',msg.body)
    data = msg.body
    send_MyApp(data.decode("utf-8"))
    msg.delete()

我运行它时得到的是下一个信息:

Message --> None
Exception in thread Thread-1:
Traceback (most recent call last):
File "..\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "..\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "../Python/ServiceBusSuscription/receive_subscription.py", line 19, in receive_subscription
send_MyApp(data.decode("utf-8"))
AttributeError: 'NoneType' object has no attribute 'decode'

如果我从线程中运行接收器,这就是它显示的错误消息(同样,当超时被跳过时,我应该删除哪个超时,因为在等待它的守护进程中它不能跳过)。基本上都是一样的错误:

Traceback (most recent call last):
  File "../Python/ServiceBusSuscription/receive_subscription.py", line 76, in <module>
    main()
  File "../Python/ServiceBusSuscription/receive_subscription.py", line 72, in main
    demo(bus_service)
  File "../Python/ServiceBusSuscription//receive_subscription.py", line 25, in demo
    print(msg.body.decode("utf-8"))
AttributeError: 'NoneType' object has no attribute 'decode'

我没有收到我正在等待的信息,并且还跳过了服务总线超时(我没有编程)。

有人可以帮助我吗? Microsoft 的文档确实没有多大帮助。

提前致谢

更新

我认为问题出在 Azure 服务总线以及订阅和筛选器上。实际上,我有 23 个过滤器,我认为 Azure 服务总线仅适用于 1 个订阅 :( 但我不确定这一点。

【问题讨论】:

  • 打印msg 时会发生什么?我的猜测是你没有从服务总线得到任何回报。
  • 没错,我什么都没有 :( 正如您在跟踪中看到的那样,第一行是消息的结果:无 :(
  • 我应该会收到消息(因为我是用其他应用程序发送的,当然)。此外,我使用 Java 开发了相同的代码,并且收到了我在 Python 中等待的消息。

标签: python-3.x azureservicebus azure-servicebus-queues azure-servicebus-topics


【解决方案1】:

我尝试成功重现您的问题,然后我发现如果您的主题中没有消息,它就会发生。

所以在解码msg.body的字节之前,你需要检查msg.body的值或类型是None还是type(None),如下所示。

data = msg.body
if data != None: 
# Or if type(data) == type(b''):
    send_MyApp(data.decode("utf-8"))
    msg.delete()
else:
    ...

希望对你有帮助。

【讨论】:

  • 我认为您的回答是正确的,因为我认为问题在于 Azure 服务总线中的订阅和筛选器。我将更新问题,并将您的建议添加到我的代码中,谢谢!
  • 只有一个问题,是“如果数据==无”还是“如果数据!=无”??
  • @jjmartinez if data != None:。抱歉,是我的错。我已经更新了我的邮政编码。
猜你喜欢
  • 2017-10-23
  • 1970-01-01
  • 2020-12-30
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 2018-08-27
  • 2016-02-10
  • 2016-12-29
相关资源
最近更新 更多