RabbitMQ trigger for Azure Functions overview

Note

The RabbitMQ bindings are only fully supported on Premium and Dedicated plans. Consumption is not supported.

Use the RabbitMQ trigger to respond to messages from a RabbitMQ queue.

For information on setup and configuration details, see the overview.

Example

The following example shows a C# function that reads and logs the RabbitMQ message as a RabbitMQ Event:

[FunctionName("RabbitMQTriggerCSharp")]
public static void RabbitMQTrigger_BasicDeliverEventArgs(
    [RabbitMQTrigger("queue", ConnectionStringSetting = "rabbitMQConnectionAppSetting")] BasicDeliverEventArgs args,
    ILogger logger
    )
{
    logger.LogInformation($"C# RabbitMQ queue trigger function processed message: {Encoding.UTF8.GetString(args.Body)}");
}

The following example shows how to read the message as a POCO.

namespace Company.Function
{
    public class TestClass
    {
        public string x { get; set; }
    }

    public class RabbitMQTriggerCSharp{
        [FunctionName("RabbitMQTriggerCSharp")]
        public static void RabbitMQTrigger_BasicDeliverEventArgs(
            [RabbitMQTrigger("queue", ConnectionStringSetting = "rabbitMQConnectionAppSetting")] TestClass pocObj,
            ILogger logger
            )
        {
            logger.LogInformation($"C# RabbitMQ queue trigger function processed message: {pocObj}");
        }
    }
}

Like with Json objects, an error will occur if the message isn't properly formatted as a C# object. If it is, it is then bound to the variable pocObj, which can be used for what whatever it is needed for.

Attributes and annotations

In C# class libraries, use the RabbitMQTrigger attribute.

Here's a RabbitMQTrigger attribute in a method signature:

[FunctionName("RabbitMQTest")]
public static void RabbitMQTest([RabbitMQTrigger("queue")] string message, ILogger log)
{
    ...
}

For a complete example, see C# example.

Configuration

The following table explains the binding configuration properties that you set in the function.json file and the RabbitMQTrigger attribute.

function.json property Attribute property Description
type n/a Must be set to "RabbitMQTrigger".
direction n/a Must be set to "in".
name n/a The name of the variable that represents the queue in function code.
queueName QueueName Name of the queue to receive messages from.
hostName HostName (ignored if using ConnectStringSetting)
Hostname of the queue (Ex: 10.26.45.210)
userNameSetting UserNameSetting (ignored if using ConnectionStringSetting)
Name of the app setting that contains the username to access the queue. Ex. UserNameSetting: "%< UserNameFromSettings >%"
passwordSetting PasswordSetting (ignored if using ConnectionStringSetting)
Name of the app setting that contains the password to access the queue. Ex. PasswordSetting: "%< PasswordFromSettings >%"
connectionStringSetting ConnectionStringSetting The name of the app setting that contains the RabbitMQ message queue connection string. Please note that if you specify the connection string directly and not through an app setting in local.settings.json, the trigger will not work. (Ex: In function.json: connectionStringSetting: "rabbitMQConnection"
In local.settings.json: "rabbitMQConnection" : "< ActualConnectionstring >")
port Port (ignored if using ConnectionStringSetting) Gets or sets the Port used. Defaults to 0 which points to rabbitmq client's default port setting: 5672.

When you're developing locally, app settings go into the local.settings.json file.

Usage

The default message type is RabbitMQ Event, and the Body property of the RabbitMQ Event can be read as the types listed below:

  • An object serializable as JSON - The message is delivered as a valid JSON string.
  • string
  • byte[]
  • POCO - The message is formatted as a C# object. For a complete example, see C# example.

Dead letter queues

Dead letter queues and exchanges can't be controlled or configured from the RabbitMQ trigger. In order to use dead letter queues, pre-configure the queue used by the trigger in RabbitMQ. Please refer to the RabbitMQ documentation.

host.json settings

This section describes the global configuration settings available for this binding in versions 2.x and higher. The example host.json file below contains only the settings for this binding. For more information about global configuration settings, see host.json reference for Azure Functions version.

{
    "version": "2.0",
    "extensions": {
        "rabbitMQ": {
            "prefetchCount": 100,
            "queueName": "queue",
            "connectionString": "amqp://user:password@url:port",
            "port": 10
        }
    }
}
Property Default Description
prefetchCount 30 Gets or sets the number of messages that the message receiver can simultaneously request and is cached.
queueName n/a Name of the queue to receive messages from.
connectionString n/a The RabbitMQ message queue connection string. Please note that the connection string is directly specified here and not through an app setting.
port 0 (ignored if using connectionString) Gets or sets the Port used. Defaults to 0 which points to rabbitmq client's default port setting: 5672.

Local testing

Note

The connectionString takes precedence over "hostName", "userName", and "password". If these are all set, the connectionString will override the other two.

If you are testing locally without a connection string, you should set the "hostName" setting and "userName" and "password" if applicable in the "rabbitMQ" section of host.json:

{
    "version": "2.0",
    "extensions": {
        "rabbitMQ": {
            ...
            "hostName": "localhost",
            "username": "userNameSetting",
            "password": "passwordSetting"
        }
    }
}
Property Default Description
hostName n/a (ignored if using connectionString)
Hostname of the queue (Ex: 10.26.45.210)
userName n/a (ignored if using connectionString)
Name to access the queue
password n/a (ignored if using connectionString)
Password to access the queue

Enable Runtime Scaling

In order for the RabbitMQ trigger to scale out to multiple instances, the Runtime Scale Monitoring setting must be enabled.

In the portal, this setting can be found under Configuration > Function runtime settings for your function app.

VNETToggle

In the CLI, you can enable Runtime Scale Monitoring by using the following command:

az resource update -g <resource_group> -n <function_app_name>/config/web --set properties.functionsRuntimeScaleMonitoringEnabled=1 --resource-type Microsoft.Web/sites

Monitoring RabbitMQ endpoint

To monitor your queues and exchanges for a certain RabbitMQ endpoint:

Next steps