Rediger

Del via


RabbitMQ output binding for Azure Functions overview

Note

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

Use the RabbitMQ output binding to send messages to a RabbitMQ queue.

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

Example

A C# function can be created by using one of the following C# modes:

  • Isolated worker model: Compiled C# function that runs in a worker process that's isolated from the runtime. Isolated worker process is required to support C# functions running on LTS and non-LTS versions .NET and the .NET Framework.
  • In-process model: Compiled C# function that runs in the same process as the Functions runtime.
  • C# script: Used primarily when you create C# functions in the Azure portal.
[Function(nameof(RabbitMQFunction))]
[RabbitMQOutput(QueueName = "destinationQueue", ConnectionStringSetting = "RabbitMQConnection")]
public static string Run([RabbitMQTrigger("queue", ConnectionStringSetting = "RabbitMQConnection")] string item,
    FunctionContext context)
{
    var logger = context.GetLogger(nameof(RabbitMQFunction));

    logger.LogInformation(item);

    var message = $"Output message created at {DateTime.Now}";
    return message;
}

The following Java function uses the @RabbitMQOutput annotation from the Java RabbitMQ types to describe the configuration for a RabbitMQ queue output binding. The function sends a message to the RabbitMQ queue when triggered by a TimerTrigger every 5 minutes.

@FunctionName("RabbitMQOutputExample")
public void run(
@TimerTrigger(name = "keepAliveTrigger", schedule = "0 */5 * * * *") String timerInfo,
@RabbitMQOutput(connectionStringSetting = "rabbitMQConnectionAppSetting", queueName = "hello") OutputBinding<String> output,
final ExecutionContext context) {
    output.setValue("Some string");
}

The following example shows a RabbitMQ output binding in a function.json file and a JavaScript function that uses the binding. The function reads in the message from an HTTP trigger and outputs it to the RabbitMQ queue.

Here's the binding data in the function.json file:

{
    "bindings": [
        {
            "type": "httpTrigger",
            "direction": "in",
            "authLevel": "function",
            "name": "input",
            "methods": [
                "get",
                "post"
            ]
        },
        {
            "type": "rabbitMQ",
            "name": "outputMessage",
            "queueName": "outputQueue",
            "connectionStringSetting": "rabbitMQConnectionAppSetting",
            "direction": "out"
        }
    ]
}

Here's JavaScript code:

module.exports = async function (context, input) {
    context.bindings.outputMessage = input.body;
};

The following example shows a RabbitMQ output binding in a function.json file and a Python function that uses the binding. The function reads in the message from an HTTP trigger and outputs it to the RabbitMQ queue.

Here's the binding data in the function.json file:

{
    "scriptFile": "__init__.py",
    "bindings": [
        {
            "authLevel": "function",
            "type": "httpTrigger",
            "direction": "in",
            "name": "req",
            "methods": [
                "get",
                "post"
            ]
        },
        {
            "type": "http",
            "direction": "out",
            "name": "$return"
        },​​
        {
            "type": "rabbitMQ",
            "name": "outputMessage",
            "queueName": "outputQueue",
            "connectionStringSetting": "rabbitMQConnectionAppSetting",
            "direction": "out"
        }
    ]
}

In _init_.py:

import azure.functions as func

def main(req: func.HttpRequest, outputMessage: func.Out[str]) -> func.HttpResponse:
    input_msg = req.params.get('message')
    outputMessage.set(input_msg)
    return 'OK'

Attributes

Both in-process and isolated worker process C# libraries use the attribute to define the function. C# script instead uses a function.json configuration file.

The attribute's constructor takes the following parameters:

Parameter Description
QueueName Name of the queue from which to receive messages.
HostName Hostname of the queue, such as 10.26.45.210. Ignored when using ConnectStringSetting.
UserNameSetting Name of the app setting that contains the username to access the queue, such as UserNameSetting: "%< UserNameFromSettings >%". Ignored when using ConnectStringSetting.
PasswordSetting Name of the app setting that contains the password to access the queue, such as PasswordSetting: "%< PasswordFromSettings >%". Ignored when using ConnectStringSetting.
ConnectionStringSetting The name of the app setting that contains the RabbitMQ message queue connection string. The trigger won't work when you specify the connection string directly instead through an app setting. For example, when you have set ConnectionStringSetting: "rabbitMQConnection", then in both the local.settings.json and in your function app you need a setting like "RabbitMQConnection" : "< ActualConnectionstring >".
Port Gets or sets the port used. Defaults to 0, which points to the RabbitMQ client's default port setting of 5672.

In C# class libraries, use the RabbitMQTrigger attribute.

Here's a RabbitMQTrigger attribute in a method signature for an isolated worker process library:

[Function(nameof(RabbitMQFunction))]
[RabbitMQOutput(QueueName = "destinationQueue", ConnectionStringSetting = "RabbitMQConnection")]
public static string Run([RabbitMQTrigger("queue", ConnectionStringSetting = "RabbitMQConnection")] string item,
    FunctionContext context)
{

Annotations

The RabbitMQOutput annotation allows you to create a function that runs when a RabbitMQ message is created.

The annotation supports the following configuration settings:

Setting Description
queueName Name of the queue from which to receive messages.
hostName Hostname of the queue, such as 10.26.45.210. Ignored when using ConnectStringSetting.
userNameSetting Name of the app setting that contains the username to access the queue, such as UserNameSetting: "%< UserNameFromSettings >%". Ignored when using ConnectStringSetting.
passwordSetting Name of the app setting that contains the password to access the queue, such as PasswordSetting: "%< PasswordFromSettings >%". Ignored when using ConnectStringSetting.
connectionStringSetting The name of the app setting that contains the RabbitMQ message queue connection string. The trigger won't work when you specify the connection string directly instead through an app setting. For example, when you have set ConnectionStringSetting: "rabbitMQConnection", then in both the local.settings.json and in your function app you need a setting like "RabbitMQConnection" : "< ActualConnectionstring >".
port Gets or sets the port used. Defaults to 0, which points to the RabbitMQ client's default port setting of 5672.

See the output binding example for more detail.

Configuration

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

function.json property Description
type Must be set to RabbitMQ.
direction Must be set to out.
name The name of the variable that represents the queue in function code.
queueName Name of the queue to send messages to.
hostName Hostname of the queue, such as 10.26.45.210. Ignored when using connectStringSetting.
userName Name of the app setting that contains the username to access the queue, such as UserNameSetting: "< UserNameFromSettings >". Ignored when using connectStringSetting.
password Name of the app setting that contains the password to access the queue, such as UserNameSetting: "< UserNameFromSettings >". Ignored when using connectStringSetting.
connectionStringSetting The name of the app setting that contains the RabbitMQ message queue connection string. The trigger won't work when you specify the connection string directly instead of through an app setting in local.settings.json. For example, when you have set connectionStringSetting: "rabbitMQConnection" then in both the local.settings.json and in your function app you need a setting like "rabbitMQConnection" : "< ActualConnectionstring >".
port Gets or sets the Port used. Defaults to 0, which points to the RabbitMQ client's default port setting of 5672.

When you're developing locally, add your application settings in the local.settings.json file in the Values collection.

See the Example section for complete examples.

Usage

The parameter type supported by the RabbitMQ trigger depends on the Functions runtime version, the extension package version, and the C# modality used.

The RabbitMQ bindings currently support only string and serializable object types when running in an isolated worker process.

For a complete example, see C# example.

Use the following parameter types for the output binding:

  • byte[] - If the parameter value is null when the function exits, Functions doesn't create a message.
  • string - If the parameter value is null when the function exits, Functions doesn't create a message.
  • POJO - If the parameter value isn't formatted as a Java object, an error will be received.

The queue message is available via context.bindings.<NAME> where <NAME> matches the name defined in function.json. If the payload is JSON, the value is deserialized into an object.

Refer to the Python example.

Next steps