How to configure Receiver batch size for Azure Fun

2019-07-23 19:37发布

问题:

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;
}