-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
201 lines (172 loc) · 7.9 KB
/
Program.cs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
// Copyright © Microsoft Corporation, All Rights Reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
// ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A
// PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache License, Version 2.0 for the specific language
// governing permissions and limitations under the License.
namespace QueueCRUD
{
using Microsoft.Azure.ServiceBus;
using Microsoft.Azure.ServiceBus.Management;
using System;
using System.Threading.Tasks;
public class Program : MessagingSamples.Sample
{
ManagementClient managementClient;
public async Task RunAsync(string connectionString)
{
this.managementClient = new ManagementClient(connectionString);
var queueName = Guid.NewGuid().ToString("D").Substring(0, 8);
Console.WriteLine($"Creating a new Queue with name - {queueName}");
await CreateQueueAsync(queueName).ConfigureAwait(false);
Console.WriteLine("Retrieving the created queue");
var getQueue = await GetQueueAsync(queueName).ConfigureAwait(false);
Console.WriteLine($"Updating few properties of the queue");
await UpdateQueueAsync(getQueue).ConfigureAwait(false);
Console.WriteLine("Retrieving runtime information of the queue");
await GetQueueRuntimeInfoAsync(queueName).ConfigureAwait(false);
Console.WriteLine("Deleting the queue");
await DeleteQueueAsync(queueName);
await this.managementClient.CloseAsync().ConfigureAwait(false);
}
/// <summary>
/// Creates a new Queue using the managementClient with the name provided.
/// </summary>
private async Task CreateQueueAsync(string queueName)
{
// All the values have defaults and hence optional.
// The only required parameter is the path of the queue (in this case, queueName)
var queueDescription = new QueueDescription(queueName)
{
// The duration of a peek lock; that is, the amount of time that a message is locked from other receivers.
LockDuration = TimeSpan.FromSeconds(45),
// Size of the Queue. For non-partitioned entity, this would be the size of the queue.
// For partitioned entity, this would be the size of each partition.
MaxSizeInMB = 2048,
// This value indicates if the queue requires guard against duplicate messages.
// Find out more in DuplicateDetection sample
RequiresDuplicateDetection = false,
//Since RequiresDuplicateDetection is false, the following need not be specified and will be ignored.
//DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(2),
// This indicates whether the queue supports the concept of session.
// Find out more in "Session and Workflow Management Features" sample
RequiresSession = false,
// The default time to live value for the messages
// Find out more in "TimeToLive" sample.
DefaultMessageTimeToLive = TimeSpan.FromDays(7),
// Duration of idle interval after which the queue is automatically deleted.
AutoDeleteOnIdle = TimeSpan.MaxValue,
// Decides whether an expired message due to TTL should be dead-letterd
// Find out more in "TimeToLive" sample.
EnableDeadLetteringOnMessageExpiration = false,
// The maximum delivery count of a message before it is dead-lettered
// Find out more in "DeadletterQueue" sample
MaxDeliveryCount = 8,
// Creating only one partition.
// Find out more in PartitionedQueues sample.
EnablePartitioning = false
};
try
{
QueueDescription createdQueue = await managementClient.CreateQueueAsync(queueName).ConfigureAwait(false);
}
catch (ServiceBusException ex)
{
Console.WriteLine($"Encountered exception while creating Queue -\n{ex}");
throw;
}
}
/// <summary>
/// Retrieve a queue
/// </summary>
private async Task<QueueDescription> GetQueueAsync(string queueName)
{
try
{
QueueDescription getQueue = await managementClient.GetQueueAsync(queueName).ConfigureAwait(false);
return getQueue;
}
catch (ServiceBusException ex)
{
Console.WriteLine($"Encountered exception while retrieving Queue -\n{ex}");
throw;
}
}
// Note - the following properties cannot be updated once created -
// - Path
// - RequiresSession
// - EnablePartitioning
private async Task UpdateQueueAsync(QueueDescription queueDescription)
{
try
{
Console.WriteLine($"Before update - MaxDeliveryCount:{queueDescription.MaxDeliveryCount}; " +
$"LockDuration:{queueDescription.LockDuration}");
// Updating the properties of the queue.
queueDescription.MaxDeliveryCount = 15;
queueDescription.LockDuration = TimeSpan.FromMinutes(5);
QueueDescription updatedQueue = await managementClient.UpdateQueueAsync(queueDescription).ConfigureAwait(false);
Console.WriteLine($"After update - MaxDeliveryCount:{updatedQueue.MaxDeliveryCount}; " +
$"LockDuration:{updatedQueue.LockDuration}");
}
catch (ServiceBusException ex)
{
Console.WriteLine($"Encountered exception while updating Queue -\n{ex}");
throw;
}
}
private async Task GetQueueRuntimeInfoAsync(string queueName)
{
try
{
var queueRuntimeInfo = await managementClient.GetQueueRuntimeInfoAsync(queueName).ConfigureAwait(false);
Console.WriteLine($"Retrieved runtime information of queue\n " +
$"Active messages:{queueRuntimeInfo.MessageCountDetails.ActiveMessageCount}\n " +
$"Size of queue:{queueRuntimeInfo.SizeInBytes}\n" +
$"Queue Creation time: {queueRuntimeInfo.CreatedAt}\n" +
$"Queue last updation time: {queueRuntimeInfo.UpdatedAt}\n");
}
catch (ServiceBusException ex)
{
Console.WriteLine($"Encountered exception while retrieving runtime information for Queue -\n{ex}");
throw;
}
}
private async Task DeleteQueueAsync(string queueName)
{
try
{
await this.managementClient.DeleteQueueAsync(queueName).ConfigureAwait(false);
}
catch (ServiceBusException ex)
{
Console.WriteLine($"Encountered exception while deleting Queue -\n{ex}");
throw;
}
}
public static int Main(string[] args)
{
try
{
var app = new Program();
app.RunSample(args, app.RunAsync);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return 1;
}
return 0;
}
}
}