How to configure Receiver batch size for Azure Fun

2019-07-23 19:24发布

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?

2条回答
干净又极端
2楼-- · 2019-07-23 19:55

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;
}
查看更多
Summer. ? 凉城
3楼-- · 2019-07-23 20:05

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).

查看更多
登录 后发表回答