Use the RabbitMQ trigger to respond to messages from a RabbitMQ queue.
For information on setup and configuration details, see the overview.
ExampleYou can create a C# function by using one of the following C# modes:
[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 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's then bound to the variable pocObj, which can be used for what whatever it's needed for.
The following Java function uses the @RabbitMQTrigger
annotation from the Java RabbitMQ types to describe the configuration for a RabbitMQ queue trigger. The function grabs the message placed on the queue and adds it to the logs.
@FunctionName("RabbitMQTriggerExample")
public void run(
@RabbitMQTrigger(connectionStringSetting = "rabbitMQConnectionAppSetting", queueName = "queue") String input,
final ExecutionContext context)
{
context.getLogger().info("Java HTTP trigger processed a request." + input);
}
The following example shows a RabbitMQ trigger binding in a function.json file and a JavaScript function that uses the binding. The function reads and logs a RabbitMQ message.
Here's the binding data in the function.json file:
{ââ
"bindings": [
{ââ
"name": "myQueueItem",
"type": "rabbitMQTrigger",
"direction": "in",
"queueName": "queue",
"connectionStringSetting": "rabbitMQConnectionAppSetting"
}ââ
]
}ââ
Here's the JavaScript script code:
module.exports = async function (context, myQueueItem) {ââ
context.log('JavaScript RabbitMQ trigger function processed work item', myQueueItem);
}ââ;
The following example demonstrates how to read a RabbitMQ queue message via a trigger.
A RabbitMQ binding is defined in function.json where type is set to RabbitMQTrigger
.
{ââ
"scriptFile": "__init__.py",
"bindings": [
{ââ
"name": "myQueueItem",
"type": "rabbitMQTrigger",
"direction": "in",
"queueName": "queue",
"connectionStringSetting": "rabbitMQConnectionAppSetting"
}ââ
]
}ââ
import logging
import azure.functions as func
def main(myQueueItem) -> None:
logging.info('Python RabbitMQ trigger function processed a queue item: %s', myQueueItem)
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 usingConnectStringSetting
. 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)
{
In C# class libraries, use the RabbitMQTrigger attribute.
Here's a RabbitMQTrigger
attribute in a method signature for an in-process library:
[FunctionName("RabbitMQTest")]
public static void RabbitMQTest([RabbitMQTrigger("queue")] string message, ILogger log)
{
...
}
Annotations
The RabbitMQTrigger
annotation allows you to create a function that runs when a RabbitMQ message is created.
The annotation supports the following configuration options:
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 usingConnectStringSetting
. 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
. 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 toRabbitMQTrigger
. direction Must be set to in
. name The name of the variable that represents the queue in function code. 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
.
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.
UsageImportant
The RabbitMQ binding doesn't support Microsoft Entra authentication and managed identities. You can use Azure Key Vault to centrally managed your RabbitMQ connection strings. To learn more, see Manage Connections.
The parameter type supported by the RabbitMQ trigger depends on the C# modality used.
The RabbitMQ bindings currently support only string and serializable object types when running in an isolated process.
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 complete code, see C# example.For a complete example, see C# example.
Refer to Java annotations.
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.
Dead letter queuesDead letter queues and exchanges can't be controlled or configured from the RabbitMQ trigger. To use dead letter queues, pre-configure the queue used by the trigger in RabbitMQ. Refer to the RabbitMQ documentation.
host.json settingsThis section describes the configuration settings available for this binding in version 2.x and later. Settings in the host.json file apply to all functions in a function app instance. For more information about function app configuration settings, see host.json reference for Azure Functions.
{
"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. 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're 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)
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.
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:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4