| title | RabbitMQ output bindings for Azure Functions |
|---|---|
| description | Learn to send RabbitMQ messages from Azure Functions. |
| author | cachai2 |
| ms.topic | reference |
| ms.date | 01/21/2022 |
| ms.author | cachai |
| ms.devlang | csharp |
| ms.custom | devx-track-extended-java, devx-track-js, devx-track-python |
| zone_pivot_groups | programming-languages-set-functions-lang-workers |
Use the RabbitMQ output binding to send messages to a RabbitMQ queue.
[!INCLUDE functions-rabbitmq-plans-support-note]
For information on setup and configuration details, see the overview.
::: zone pivot="programming-language-csharp"
[!INCLUDE functions-bindings-csharp-intro-with-csx]
[!INCLUDE functions-in-process-model-retirement-note]
:::code language="csharp" source="~/azure-functions-dotnet-worker/samples/Extensions/RabbitMQ/RabbitMQFunction.cs" range="12-23":::
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);
}
}
}::: zone-end ::: zone pivot="programming-language-java"
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");
}::: zone-end
::: zone pivot="programming-language-javascript"
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;
};::: zone-end
::: zone pivot="programming-language-powershell"
::: zone-end
::: zone pivot="programming-language-python"
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'::: zone-end
::: zone pivot="programming-language-csharp"
Both isolated worker process and in-process C# libraries use an attribute to define an output binding that writes to a RabbitMQ queue.
The RabbitMQOutputAttribute constructor accepts these parameters:
| Parameter | Description |
|---|---|
| QueueName | Name of the queue from which to receive messages. |
| HostName | This parameter is no longer supported and is ignored. It will be removed in a future version. |
| ConnectionStringSetting | The name of the app setting that contains the connection string for your RabbitMQ server. This setting only takes an app setting key name, you can't directly set a connection string value. For more information, see Connections. |
| UserNameSetting | This parameter is no longer supported and is ignored. It will be removed in a future version. |
| PasswordSetting | This parameter is no longer supported and is ignored. It will be removed in a future version. |
| Port | Gets or sets the port used. Defaults to 0, which points to the RabbitMQ client's default port setting of 5672. |
| DisableCertificateValidation | Gets or sets a value indicating whether certificate validation should be disabled. Not recommended for production. Does not apply when SSL is disabled. |
The RabbitMQAttribute constructor accepts these parameters:
| Parameter | Description |
|---|---|
| QueueName | Name of the queue from which to receive messages. |
| ConnectionStringSetting | The name of the app setting that contains the connection string for your RabbitMQ server. This setting only takes an app setting key name, you can't directly set a connection string value. For more information, see Connections. |
| DisableCertificateValidation | Gets or sets a value indicating whether certificate validation should be disabled. Not recommended for production. Does not apply when SSL is disabled. |
The RabbitMQOutputAttribute constructor accepts these 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 connection string for your RabbitMQ server. This setting only takes an app setting key name, you can't directly set a connection string value. For more information, see Connections. |
| Port | Gets or sets the port used. Defaults to 0, which points to the RabbitMQ client's default port setting of 5672. |
The RabbitMQAttribute constructor accepts these 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 connection string for your RabbitMQ server. This setting only takes an app setting key name, you can't directly set a connection string value. For more information, see Connections. |
| Port | Gets or sets the port used. Defaults to 0, which points to the RabbitMQ client's default port setting of 5672. |
::: zone-end
::: zone pivot="programming-language-java"
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. |
| connectionStringSetting | The name of the app setting that contains the connection string for your RabbitMQ server. This setting only takes an app setting key name, you can't directly set a connection string value. For more information, see Connections. |
| disableCertificateValidation | Gets or sets a value indicating whether certificate validation should be disabled. Not recommended for production. Does not apply when SSL is disabled. |
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 connection string for your RabbitMQ server. This setting only takes an app setting key name, you can't directly set a connection string value. For more information, see Connections. |
| port | Gets or sets the port used. Defaults to 0, which points to the RabbitMQ client's default port setting of 5672. |
::: zone-end
::: zone pivot="programming-language-javascript,programming-language-csharp,programming-language-python,programming-language-powershell"
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. |
| connectionStringSetting | The name of the app setting that contains the connection string for your RabbitMQ server. This setting only takes an app setting key name, you can't directly set a connection string value. For more information, see Connections. |
| disableCertificateValidation | Gets or sets a value indicating whether certificate validation should be disabled. Not recommended for production. Does not apply when SSL is disabled. |
| 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 connection string for your RabbitMQ server. This setting only takes an app setting key name, you can't directly set a connection string value. For more information, see Connections. |
| port | Gets or sets the Port used. Defaults to 0, which points to the RabbitMQ client's default port setting of 5672. |
[!INCLUDE app settings to local.settings.json]
::: zone-end
See the Example section for complete examples.
::: zone pivot="programming-language-csharp"
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.
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.POCO- The message is formatted as a C# object.
When working with C# functions:
- Async functions need a return value or
IAsyncCollectorinstead of anoutparameter.
::: zone-end
::: zone pivot="programming-language-java"
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. ::: zone-end
::: zone pivot="programming-language-javascript"
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.
::: zone-end
[!INCLUDE functions-rabbitmq-connections]