我创建了一个简单的事件处理器主机,它从配置了租约管理默认选项的Azure IOT中心读取事件。
但是,即使我正在运行EventProcessorHost的单个实例,我也会定期跟踪异常:
ReceiverDisconnectedException:创建具有更高纪元的新接收器,因此具有纪元的当前接收器正在断开连接。原因:LeaseLost
客户重新获得租约并进一步处理消息。
这是EventProcessorHost的初始化
EventProcessorHost eventProcessorHost = new EventProcessorHost(hostName, eventHubName, EventHubConsumerGroup.DefaultGroupName,
eventHubConnectionString, storageAccountConnectionString);
var options = new EventProcessorOptions();
options.ExceptionReceived += (sender, e) =>
{
Console.WriteLine(e.Exception);
};
eventProcessorHost.PartitionManagerOptions = PartitionManagerOptions.DefaultOptions;
eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>(options).Wait();
这种行为是正常的还是我需要做些什么来避免单个实例?
下面是我的EventProcessor:
public class SimpleEventProcessor : IEventProcessor
{
Stopwatch checkpointStopWatch;
public async Task CloseAsync(PartitionContext context, CloseReason reason)
{
Console.WriteLine("Processor Shutting Down. Partition '{0}', Reason: '{1}'.", context.Lease.PartitionId, reason);
await context.CheckpointAsync();
}
public Task OpenAsync(PartitionContext context)
{
Console.WriteLine("SimpleEventProcessor initialized. Partition: '{0}', Offset: '{1}'", context.Lease.PartitionId, context.Lease.Offset);
this.checkpointStopWatch = new Stopwatch();
this.checkpointStopWatch.Start();
this.rawDataService = new RawDataService();
return Task.FromResult<object>(null);
}
public async Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
{
foreach (EventData eventData in messages)
{
string data = Encoding.UTF8.GetString(eventData.GetBytes());
Console.WriteLine(string.Format("Message received. Partition: '{0}', Data: '{1}'", context.Lease.PartitionId, data));
}
}
}