-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathRabbitMQSamples.cs
More file actions
155 lines (141 loc) · 7.38 KB
/
Copy pathRabbitMQSamples.cs
File metadata and controls
155 lines (141 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.RabbitMQ;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
namespace WebJobs.Extensions.RabbitMQ.Samples
{
public static class RabbitMQSamples
{
// Output samples
// To run this sample with a specified amqp connection string, create a file called "appsettings.json" in the same directory.
// In the file, add:
// {
// "connectionStrings": {
// "rabbitMQ": "your connection string here"
// }
// }
// Or, if you already have an appsettings.json, add rabbitMQ and your connection string to the connection strings property.
public static void TimerTrigger_ConnectionString_StringOutput(
[TimerTrigger("00:01")] TimerInfo timer,
[RabbitMQ(QueueName = "queue")] out string outputMessage,
ILogger logger)
{
outputMessage = "new";
logger.LogInformation($"RabbitMQ output binding message: {outputMessage}");
}
public static void TimerTrigger_PocoOutput(
[TimerTrigger("00:01")] TimerInfo timer,
[RabbitMQ(HostName = "localhost", QueueName = "queue")] out TestClass outputMessage,
ILogger logger)
{
outputMessage = new TestClass(1, 1);
logger.LogInformation($"RabbitMQ output binding message: {JsonConvert.SerializeObject(outputMessage)}");
}
// To run:
// 1. Create Azure Storage Account and go to the homepage for that account
// 2. Look for Queue service and click Queues on the sidebar
// 3. Create a queue named "samples-rabbitmq-messages"
// 4. Add a message to the queue
// 5. Run this sample and you will see the queue trigger fired.
// *Note that any time the queue isn't empty, the trigger will continue to fire.
// So you can add items to the queue while the sample is running, and the trigger will be called until the queue is empty.
public static async Task ProcessMessage_RabbitMQAsyncCollector(
[QueueTrigger(@"samples-rabbitmq-messages")] string message,
[RabbitMQ(QueueName = "queue")] IAsyncCollector<RabbitMQMessage> messages,
ILogger logger)
{
logger.LogInformation($"Received queue trigger");
byte[] messageInBytes = Encoding.UTF8.GetBytes(message);
await messages.AddAsync(new RabbitMQMessage(messageInBytes) { RoutingKey = "custom-routing-key" });
}
// To run:
// 1. Create Azure Storage Account and go to the homepage for that account
// 2. Look for Queue service and click Queues on the sidebar
// 3. Create a queue named "samples-rabbitmq-messages"
// 4. Add a message to the queue in POCO format (i.e.: "{ "name": Katie }")
// 5. Run this sample and you will see the queue trigger fired.
// *Note that any time the queue isn't empty, the trigger will continue to fire.
// So you can add items to the queue while the sample is running, and the trigger will be called until the queue is empty.
public static void QueueTrigger_RabbitMQOutput(
[QueueTrigger(@"samples-rabbitmq-messages")] TestClass message,
[RabbitMQ(QueueName = "queue")] out TestClass outputMessage,
ILogger logger)
{
outputMessage = message;
logger.LogInformation($"RabbitMQ output binding message: {JsonConvert.SerializeObject(outputMessage)}");
}
// Example that binds to client
public static void BindToClient(
[TimerTrigger("01:00", RunOnStartup = true)] TimerInfo timer,
[RabbitMQ(ConnectionStringSetting = "rabbitMQ")] IModel client,
ILogger logger)
{
QueueDeclareOk queue = client.QueueDeclare("hello", false, false, false, null);
logger.LogInformation("Opening connection and creating queue!");
}
// Trigger samples
public static void RabbitMQTrigger_String(
[RabbitMQTrigger("new_test_queue", ConnectionStringSetting = "rabbitMQ")] string message,
string consumerTag,
ILogger logger)
{
logger.LogInformation($"RabbitMQ queue trigger function processed message: {message} and consumer tag: {consumerTag}");
}
public static void RabbitMQTrigger_String_NoConnectionString(
[RabbitMQTrigger(hostName: "RabbitMQHostName", userNameSetting: "%UserNameSetting%", passwordSetting: "%PasswordSetting%", port: 5672, queueName: "queue", DeadLetterExchangeName = "dlxName")] string message,
string consumerTag,
ILogger logger)
{
logger.LogInformation($"RabbitMQ queue trigger function processed message: {message} and consumer tag: {consumerTag}");
}
public static void RabbitMQTrigger_BasicDeliverEventArgs(
[RabbitMQTrigger("queue", DeadLetterExchangeName = "dlxName")] BasicDeliverEventArgs args,
ILogger logger)
{
logger.LogInformation($"RabbitMQ queue trigger function processed message: {Encoding.UTF8.GetString(args.Body)}");
}
// This sample should fail when running a console app that sends out a message incorrectly formatted.
// It should add the message to the dead letter exchange called "dlxName"
public static void RabbitMQTrigger_JsonToPOCO(
[RabbitMQTrigger("new_test_queue", DeadLetterExchangeName = "dlxName", ConnectionStringSetting = "rabbitMQ")] TestClass pocObj,
ILogger logger)
{
logger.LogInformation($"RabbitMQ queue trigger function processed message: {pocObj}");
}
// This sample waits on messages from the poison queue created by the above sample.
// It should process it correctly since it's configured to be of type string.
public static void RabbitMQTrigger_Process_PoisonQueue(
[RabbitMQTrigger("new_test_queue-poison", ConnectionStringSetting = "rabbitMQ")] string res,
ILogger logger)
{
logger.LogInformation($"RabbitMQ queue trigger function processed message: {res}");
}
public static void RabbitMQTrigger_RabbitMQOutput(
[RabbitMQTrigger("queue", DeadLetterExchangeName = "dlxName")] string inputMessage,
[RabbitMQ(
HostName = "localhost",
QueueName = "hello")] out string outputMessage,
ILogger logger)
{
outputMessage = inputMessage;
logger.LogInformation($"RabbitMQ output binding function sent message: {outputMessage}");
logger.LogInformation($"RabbitMQ output binding function sent message: {outputMessage}");
}
public class TestClass
{
private readonly int _x;
private readonly int _y;
public TestClass(int x, int y)
{
_x = x;
_y = y;
}
}
}
}