-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainQueue.cs
167 lines (132 loc) · 4.02 KB
/
MainQueue.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
// ReSharper disable ReplaceAsyncWithTaskReturn
public class MainQueue
{
SqlConnection sqlConnection = null!;
async Task CreateQueue()
{
#region CreateQueue
var manager = new QueueManager("endpointTable", sqlConnection);
await manager.Create();
#endregion
}
async Task DeleteQueue()
{
#region DeleteQueue
var manager = new QueueManager("endpointTable", sqlConnection);
await manager.Drop();
#endregion
}
async Task Send()
{
string headers = null!;
byte[] body = null!;
#region Send
var manager = new QueueManager("endpointTable", sqlConnection);
var message = new OutgoingMessage(
id: Guid.NewGuid(),
headers: headers,
bodyBytes: body);
await manager.Send(message);
#endregion
}
async Task SendBatch()
{
string headers1 = null!;
byte[] body1 = null!;
string headers2 = null!;
byte[] body2 = null!;
#region SendBatch
var manager = new QueueManager("endpointTable", sqlConnection);
var messages = new List<OutgoingMessage>
{
new(
id: Guid.NewGuid(),
headers: headers1,
bodyBytes: body1),
new(
id: Guid.NewGuid(),
headers: headers2,
bodyBytes: body2),
};
await manager.Send(messages);
#endregion
}
async Task Read()
{
#region Read
var manager = new QueueManager("endpointTable", sqlConnection);
var message = await manager.Read(rowVersion: 10);
if (message != null)
{
Console.WriteLine(message.Headers);
if (message.Body != null)
{
using var reader = new StreamReader(message.Body);
var bodyText = await reader.ReadToEndAsync();
Console.WriteLine(bodyText);
}
}
#endregion
}
async Task ReadBatch()
{
#region ReadBatch
var manager = new QueueManager("endpointTable", sqlConnection);
var result = await manager.Read(
size: 5,
startRowVersion: 10,
func: async (message, cancel) =>
{
Console.WriteLine(message.Headers);
if (message.Body == null)
{
return;
}
using var reader = new StreamReader(message.Body);
var bodyText = await reader.ReadToEndAsync(cancel);
Console.WriteLine(bodyText);
});
Console.WriteLine(result.Count);
Console.WriteLine(result.LastRowVersion);
#endregion
}
async Task Consume()
{
#region Consume
var manager = new QueueManager("endpointTable", sqlConnection);
var message = await manager.Consume();
if (message != null)
{
Console.WriteLine(message.Headers);
if (message.Body != null)
{
using var reader = new StreamReader(message.Body);
var bodyText = await reader.ReadToEndAsync();
Console.WriteLine(bodyText);
}
}
#endregion
}
async Task ConsumeBatch()
{
#region ConsumeBatch
var manager = new QueueManager("endpointTable", sqlConnection);
var result = await manager.Consume(
size: 5,
func: async (message, cancel) =>
{
Console.WriteLine(message.Headers);
if (message.Body == null)
{
return;
}
using var reader = new StreamReader(message.Body);
var bodyText = await reader.ReadToEndAsync(cancel);
Console.WriteLine(bodyText);
});
Console.WriteLine(result.Count);
Console.WriteLine(result.LastRowVersion);
#endregion
}
}