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);
}
}
}
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.
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. |
When you're developing locally, app settings go into the local.settings.json file.
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 anout
parameter.