This repository contains the bindings and extension for ActiveMQ in Azure Functions. It's based on the Apache.NMS library.
These extensions has been tested with Artemis ActiveMQ.
There are two types of bindings in this extension:
ActiveMQTrigger
: This binding allows you to listen to a queueActiveMQ
: This binding allows you to send messages to a queue
The ActiveMQTrigger
binding allows you to listen to a queue.
Note: The
ActiveMQTrigger
binding is not supported in the Consumption plan. You can use it in the Premium plan or the Dedicated (App Service) plan.
Note: Scaling out to multiple instances is not yet implemented
queueName
: The name of the queue to listen to. Settings placeholders are supported, eg.%MyQueue%
Connection
: The raw endpoint (eg. amqp://localhost:5672/) or the name of the setting that contains it (eg.ActiveMQ:Endpoint
)UserName
: The username to use when connecting to the ActiveMQ server, or the setting that contains it (eg.ActiveMQ:UserName
)Password
: The password to use when connecting to the ActiveMQ server, or the setting that contains it (eg.ActiveMQ:Password
)
string
Custom
: the content of the message is deserialized to the specified type
The following example shows a function that listens to a queue, by using a string input
[FunctionName("ActiveMQTrigger")]
public static void Run(
[ActiveMQTrigger("myqueue", Connection = "ActiveMQ:Endpoint", UserName = "ActiveMQ:UserName", Password = "ActiveMQ:Password")] string message)
{
Console.WriteLine(message);
}
The following example shows a function that listens to a queue, by using a custom input
[FunctionName("ActiveMQTrigger")]
public static void Run(
[ActiveMQTrigger("myqueue", Connection = "ActiveMQ:Endpoint", UserName = "ActiveMQ:UserName", Password = "ActiveMQ:Password")] Order order)
{
Console.WriteLine(message);
}
The ActiveMQ
binding allows you to send messages to a queue.
queueName
: The name of the queue to listen to. Settings placeholders are supported, eg.%MyQueue%
Connection
: The raw endpoint (eg. amqp://localhost:5672/) or the name of the setting that contains it (eg.ActiveMQ:Endpoint
)UserName
: The username to use when connecting to the ActiveMQ server, or the setting that contains it (eg.ActiveMQ:UserName
)Password
: The password to use when connecting to the ActiveMQ server, or the setting that contains it (eg.ActiveMQ:Password
)
- Types available in-process:
string
,NmsTextMessage
,NmsBytesMessage
andActiveMQOutputMessage
. - Types available in isolated mode:
string
andActiveMQOutputMessage
.
The ActiveMQOutputMessage
is a type provided by the extension, and allows the client to set the content of the message and its properties
The following examples shows a function that sends a message to a queue in an Azure Function in-process:
[FunctionName("MyFunction")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
[ActiveMQ("myqueue", Connection = "ActiveMQ:Endpoint", UserName = "ActiveMQ:UserName", Password = "ActiveMQ:Password")] out string text)
{
text = "Hello, world!";
}
[FunctionName("MyFunction")]
[return: ActiveMQ("myqueue", Connection = "ActiveMQ:Endpoint", UserName = "ActiveMQ:UserName", Password = "ActiveMQ:Password")]
public static string Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
{
return "Hello, world!";
}
[FunctionName("MyFunction")]
[return: ActiveMQ("myqueue", Connection = "ActiveMQ:Endpoint", UserName = "ActiveMQ:UserName", Password = "ActiveMQ:Password")]
public static NmsTextMessage Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
INmsMessageFactory messageFactory)
{
var message = messageFactory.CreateTextMessage("Hello, world!");
message.Properties.SetString("OrderId", Guid.NewGuid().ToString());
return message;
}
[FunctionName("MyFunction")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
[ActiveMQ("myqueue", Connection = "ActiveMQ:Endpoint", UserName = "ActiveMQ:UserName", Password = "ActiveMQ:Password")] out NmsBytesMessage message,
INmsMessageFactory messageFactory)
{
var message = messageFactory.CreateBytesMessage("Hello, world!");
message.Properties.SetString("OrderId", Guid.NewGuid().ToString());
}
[Function("MyFunction")]
[return: ActiveMQOutput("%ActiveMQ:Queue%", Connection = "ActiveMQ:Endpoint", UserName = "ActiveMQ:UserName", Password = "ActiveMQ:Password")]
public ActiveMQOutputMessage Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
{
var json = JsonSerializer.Serialize(new { Text = "Hello, world!" });
return new ActiveMQOutputMessage
{
Text = json,
Properties = { { "OrderId", Guid.NewGuid().ToString() } }
};
}
The following examples shows a function that sends a message to a queue in an Azure Function isolated:
[Function("MyFunction")]
[ActiveMQOutput("%ActiveMQ:Queue%", Connection = "ActiveMQ:Endpoint", UserName = "ActiveMQ:UserName", Password = "ActiveMQ:Password")]
public string Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
{
return JsonSerializer.Serialize(new { Text = "Hello, world!" });
}
[Function("MyFunction")]
[ActiveMQOutput("%ActiveMQ:Queue%", Connection = "ActiveMQ:Endpoint", UserName = "ActiveMQ:UserName", Password = "ActiveMQ:Password")]
public ActiveMQOutputMessage Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
{
var json = JsonSerializer.Serialize(new { Text = "Hello, world!" });
return new ActiveMQOutputMessage
{
Text = json,
Properties = { { "OrderId", Guid.NewGuid().ToString() } }
};
}
The extension can be configured using the host.json
file. Here is an example of the configuration:
{
"version": "2.0",
"extensions": {
"activeMQ": {
"transportTimeout": 5000,
"transportStartupMaxReconnectAttempts": 1
}
}
}
This project welcomes contributions and suggestions.