我有兴趣从Python脚本中侦听事件中心,并在检测到入站邮件时执行一些代码。
在Ubuntu上,使用带有https://pypi.python.org/pypi/python-qpid-proton/0.10的Python 2.7和示例“receiver”脚本,我尝试订阅一个看起来像这样的URI:
amqps://<key name>:<key>@<namespace>.servicebus.windows.net/<event hub name>/ConsumerGroups/$Default/Partitions/0
返回以下错误:
proton.MessengerException: Cannot subscribe to <...>
我相信我的事件中心设置正确,因为我能够使用单独的“发送”脚本向其发送事件,Azure仪表板显示事件已到达。但我无法作为订阅者连接以接收消息。
答案 0 :(得分:1)
我尝试在Ubuntu Server上重现您的问题,但失败了。
虽然您说正确设置了您的活动中心,但我仍然建议您查看key name
&amp;再次通过Azure旧门户网站上事件中心的key
标签页,CONFIGURE
确保在key name
中定义了具有权限Listen
的政策名称Shared access policies
}。或者您可以使用RootManageSharedAccessKey
&amp;它是您服务总线的访问连接信息的关键,可以再试一次。
此外,您可以参考https://gist.github.com/tomconte/e2a4667185a9bf674f59上的示例event_hubs_receive_many.py
(基于https://qpid.apache.org/releases/qpid-proton-0.8/messenger/python/examples/async.py.html处的质子样本async.py
)来检查您的代码。
答案 1 :(得分:1)
pypi上有一个更新版本的azure-eventhub Python SDK v5。
您可以按照recv sample接收消息:
#!/usr/bin/env python
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
"""
An example to show receiving events from an Event Hub.
"""
import os
from azure.eventhub import EventHubConsumerClient
CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']
def on_event(partition_context, event):
# Put your code here.
# If the operation is i/o intensive, multi-thread will have better performance.
print("Received event from partition: {}.".format(partition_context.partition_id))
def on_partition_initialize(partition_context):
# Put your code here.
print("Partition: {} has been initialized.".format(partition_context.partition_id))
def on_partition_close(partition_context, reason):
# Put your code here.
print("Partition: {} has been closed, reason for closing: {}.".format(
partition_context.partition_id,
reason
))
def on_error(partition_context, error):
# Put your code here. partition_context can be None in the on_error callback.
if partition_context:
print("An exception: {} occurred during receiving from Partition: {}.".format(
partition_context.partition_id,
error
))
else:
print("An exception: {} occurred during the load balance process.".format(error))
if __name__ == '__main__':
consumer_client = EventHubConsumerClient.from_connection_string(
conn_str=CONNECTION_STR,
consumer_group='$Default',
eventhub_name=EVENTHUB_NAME,
)
try:
with consumer_client:
consumer_client.receive(
on_event=on_event,
on_partition_initialize=on_partition_initialize,
on_partition_close=on_partition_close,
on_error=on_error,
starting_position="-1", # "-1" is from the beginning of the partition.
)
except KeyboardInterrupt:
print('Stopped receiving.')
如果您已经在使用v1 SDK,则可以按照migration from v1 to v5来迁移程序。
答案 2 :(得分:0)
查看https://github.com/Azure/azure-event-hubs-python
尝试./examples/recv.py查看是否有效。请注意,SAS密钥需要进行URL编码,并且关联的策略已定义了Listen操作。需要从事件中心配置更新ADDRESS。