How to configure Receiver batch size for Azure Functions EventHub listener?

风格不统一 提交于 2019-12-11 08:19:52

问题


In the latest Microsoft.Azure.WebJobs.ServiceBus package, it gives you the ability to receive batches of messages from eventhubs. I would like to set how many messages I want to receive in a batch.

The core ServiceBus library allows you to overload the Receive() function and provide the batch size.

How does one do this in the initial config of an EventHubs receiver, or is something else required?


回答1:


You can do this in Functions via the eventHub configuration block in host.json as described here. E.g.:

{
    "eventHub": {
        "maxBatchSize": 500,
        "prefetchCount": 100
    }
}

We apply those configuration settings to the EventProcessorOptions when we create the EventProcessorHost (see here).




回答2:


Steph,

The MaxBatchSize can be configured through EventProcessorOptions, you can pass it as a parameter when creating a new EventHubConfiguration.

var options = EventProcessorOptions.DefaultOptions;
options.MaxBatchSize = 50;
var eventHubConfig = new EventHubConfiguration(options);
string eventHubName = "MyHubName";
eventHubConfig.AddSender(eventHubName, "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=SendRule;SharedAccessKey=xxxxxxxx");
eventHubConfig.AddReceiver(eventHubName, "Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=ReceiveRule;SharedAccessKey=yyyyyyy");

config.UseEventHub(eventHubConfig);
JobHost host = new JobHost(config);

As you can notice in the source code of EventHubConfiguration.cs if no EventProcessorOptions is specified, the MaxBatchSize is set to 1000 instead of 10 by default.

public EventHubConfiguration(
        EventProcessorOptions options, 
        PartitionManagerOptions partitionOptions = null)
{
    if (options == null)
    {
        options = EventProcessorOptions.DefaultOptions;
        options.MaxBatchSize = 1000;
     }
     _partitionOptions = partitionOptions;

     _options = options;
}


来源:https://stackoverflow.com/questions/40676821/how-to-configure-receiver-batch-size-for-azure-functions-eventhub-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!