Skip to content

Latest commit

 

History

History
325 lines (247 loc) · 11.3 KB

functions-bindings-rabbitmq-output.md

File metadata and controls

325 lines (247 loc) · 11.3 KB
title description author ms.assetid ms.topic ms.date ms.author ms.custom
RabbitMQ output bindings for Azure Functions
Learn to send RabbitMQ messages from Azure Functions.
cachai2
reference
12/17/2020
cachai

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

The following example shows a C# function that sends a RabbitMQ message when triggered by a TimerTrigger every 5 minutes using the method return value as the output:

[FunctionName("RabbitMQOutput")]
[return: RabbitMQ(QueueName = "outputQueue", ConnectionStringSetting = "rabbitMQConnectionAppSetting")]
public static string Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    return $"{DateTime.Now}";
}

The following example shows how to use the IAsyncCollector interface to send messages.

[FunctionName("RabbitMQOutput")]
public static async Task Run(
[RabbitMQTrigger("sourceQueue", ConnectionStringSetting = "rabbitMQConnectionAppSetting")] string rabbitMQEvent,
[RabbitMQ(QueueName = "destinationQueue", ConnectionStringSetting = "rabbitMQConnectionAppSetting")]IAsyncCollector<string> outputEvents,
ILogger log)
{
     // send the message
    await outputEvents.AddAsync(JsonConvert.SerializeObject(rabbitMQEvent));
}

The following example shows how to send the messages as POCOs.

namespace Company.Function
{
    public class TestClass
    {
        public string x { get; set; }
    }
    public static class RabbitMQOutput{
        [FunctionName("RabbitMQOutput")]
        public static async Task Run(
        [RabbitMQTrigger("sourceQueue", ConnectionStringSetting = "rabbitMQConnectionAppSetting")] TestClass rabbitMQEvent,
        [RabbitMQ(QueueName = "destinationQueue", ConnectionStringSetting = "rabbitMQConnectionAppSetting")]IAsyncCollector<TestClass> outputPocObj,
        ILogger log)
        {
            // send the message
            await outputPocObj.AddAsync(rabbitMQEvent);
        }
    }
}

The following example shows a RabbitMQ output binding in a function.json file and a C# script 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 the C# script code:

using System;
using Microsoft.Extensions.Logging;

public static void Run(string input, out string outputMessage, ILogger log)
{
    log.LogInformation(input);
    outputMessage = input;
}

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 = function (context, input) {
    context.bindings.myQueueItem = input.body;
    context.done();
};

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'

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

Attributes and annotations

In C# class libraries, use the RabbitMQAttribute.

Here's a RabbitMQAttribute attribute in a method signature:

[FunctionName("RabbitMQOutput")]
public static async Task Run(
[RabbitMQTrigger("SourceQueue", ConnectionStringSetting = "TriggerConnectionString")] string rabbitMQEvent,
[RabbitMQ("DestinationQueue", ConnectionStringSetting = "OutputConnectionString")]IAsyncCollector<string> outputEvents,
ILogger log)
{
    ...
}

For a complete example, see C# example.

Attributes are not supported by C# Script.

Attributes are not supported by JavaScript.

Attributes are not supported by Python.

The RabbitMQOutput annotation allows you to create a function that runs when sending a RabbitMQ message. Configuration options available include queue name and connection string name. For additional parameter details please visit the RabbitMQOutput Java annotations.

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 and the RabbitMQ attribute.

function.json property Attribute property Description
type n/a Must be set to "RabbitMQ".
direction n/a Must be set to "out".
name n/a The name of the variable that represents the queue in function code.
queueName QueueName Name of the queue to send messages to.
hostName HostName (ignored if using ConnectStringSetting)
Hostname of the queue (Ex: 10.26.45.210)
userName UserName (ignored if using ConnectionStringSetting)
Name of the app setting that contains the username to access the queue. Ex. UserNameSetting: "< UserNameFromSettings >"
password Password (ignored if using ConnectionStringSetting)
Name of the app setting that contains the password to access the queue. Ex. UserNameSetting: "< UserNameFromSettings >"
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.

[!INCLUDE app settings to local.settings.json]

Usage

Use the following parameter types for the output binding:

  • byte[] - If the parameter value is null when the function exits, Functions does not create a message.
  • string - If the parameter value is null when the function exits, Functions does not create a message.
  • POCO - If the parameter value isn't formatted as a C# object, an error will be received. For a complete example, see C# example.

When working with C# functions:

  • Async functions need a return value or IAsyncCollector instead of an out parameter.

Use the following parameter types for the output binding:

  • byte[] - If the parameter value is null when the function exits, Functions does not create a message.
  • string - If the parameter value is null when the function exits, Functions does not create a message.
  • POCO - If the parameter value isn't formatted as a C# object, an error will be received. For a complete example, see C# Script example.

When working with C# Script functions:

  • Async functions need a return value or IAsyncCollector instead of an out parameter.

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

Refer to the Python example.

Use the following parameter types for the output binding:

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

Next steps