-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathProgram.cs
216 lines (177 loc) · 8.28 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Threading;
using System.Threading.Tasks;
using MessageQueue.Core.Services;
using MessageQueue.Core.Abstract;
using MessageQueue.Log.NLog.Concrete;
using MessageQueue.Core.Abstract.Inbound;
using MessageQueue.CofigurationProvider.AppSettings.Concrete;
namespace MessageQueue.Receiver
{
class Program
{
#region Private Data Members
private static int workTimeInMs;
private static IInboundFaFMq<string> inboundMessageQueue;
#endregion
static void Main(string[] args)
{
// Setting random working time per message.
var randomeNumberGenerator = new Random();
workTimeInMs = randomeNumberGenerator.Next(1, 1000) > 500 ? 1000 : 500;
Console.WriteLine("Please select the Message Queue (Receiver):" + Environment.NewLine + "1- ZeroMq" + Environment.NewLine + "2- RabbitMq" + Environment.NewLine + "3- ServiceBus");
var option = Console.ReadKey();
switch (option.Key)
{
case ConsoleKey.D1:
case ConsoleKey.NumPad1:
Test_ZeroMqFaf_Receive();
break;
case ConsoleKey.D2:
case ConsoleKey.NumPad2:
Test_RabbitMqFaF_Receive();
break;
case ConsoleKey.D3:
case ConsoleKey.NumPad3:
Test_ServiceBusFaF_Receive();
break;
default:
Test_ZeroMqFaf_Receive();
break;
}
Console.ReadKey();
}
#region ZeorMq
public static void Test_ZeroMqFaf_Receive()
{
try
{
Console.WriteLine();
Console.WriteLine("ZeroMq test inbound messages [work time per message {0}ms].", workTimeInMs);
// Creating queue object from factory.
// NOTE: it should only be disposed when exiting the program. Otherwise, it will not receive messages.
inboundMessageQueue = MessagingQueueFactory.CreateInboundFaF<string>(new AppSettingsConfigurationProvider(), "ZeroMqFaFInbound", new NQueueLogger("Default"));
inboundMessageQueue.OnMessageReady += ZeroMqInboundMessageQueue_OnMessageReady;
//inboundMessageQueue.OnMessageReadyAsync += ZeroMqInboundMessageQueue_OnMessageReadyAsync;
// Ready to start.
Console.WriteLine("Press any key to start receiving...");
Console.ReadKey();
// Checking is there any message.
Console.WriteLine("Queue has message: {0}", inboundMessageQueue.HasMessage());
// Starting.
inboundMessageQueue.StartReceivingMessage();
Console.WriteLine("Started....");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
private static void ZeroMqInboundMessageQueue_OnMessageReady(string message, IMessageReceiveOptions messageReceiveOptions)
{
Console.WriteLine("Pulled message successfully (ZeroMq)..." + Environment.NewLine + message + Environment.NewLine);
Thread.Sleep(workTimeInMs);
if (messageReceiveOptions.IsAcknowledgmentConfigured)
{
messageReceiveOptions.Acknowledge();
}
}
private static async Task ZeroMqInboundMessageQueue_OnMessageReadyAsync(string message, IMessageReceiveOptions messageReceiveOptions)
{
Console.WriteLine("Pulled message successfully (ZeroMq)..." + Environment.NewLine + message + Environment.NewLine);
await Task.Delay(workTimeInMs);
if (messageReceiveOptions.IsAcknowledgmentConfigured)
{
messageReceiveOptions.Acknowledge();
}
}
#endregion
#region RabbitMq
public static void Test_RabbitMqFaF_Receive()
{
try
{
Console.WriteLine();
Console.WriteLine("RabbitMq test inbound messages [work time per message {0}ms].", workTimeInMs);
// Creating queue object from factory.
inboundMessageQueue = MessagingQueueFactory.CreateInboundFaF<string>(new AppSettingsConfigurationProvider(), "RabbitMqFaFInbound", new NQueueLogger("Default"));
inboundMessageQueue.OnMessageReady += RabbitMqInboundMessageQueue_OnMessageReady;
//inboundMessageQueue.OnMessageReadyAsync += RabbitMqInboundMessageQueue_OnMessageReadyAsync;
// Ready to start.
Console.WriteLine("Press any key to start receiving...");
Console.ReadKey();
// Checking is there any message.
Console.WriteLine("Queue has message: {0}", inboundMessageQueue.HasMessage());
// Starting.
inboundMessageQueue.StartReceivingMessage();
Console.WriteLine("Started....");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
private static void RabbitMqInboundMessageQueue_OnMessageReady(string message, IMessageReceiveOptions messageReceiveOptions)
{
Console.WriteLine("Pulled message successfully (RabbitMq)..." + Environment.NewLine + message + Environment.NewLine);
Thread.Sleep(workTimeInMs);
if (messageReceiveOptions.IsAcknowledgmentConfigured)
{
messageReceiveOptions.Acknowledge();
}
}
private static async Task RabbitMqInboundMessageQueue_OnMessageReadyAsync(string message, IMessageReceiveOptions messageReceiveOptions)
{
Console.WriteLine("Pulled message successfully (RabbitMq)..." + Environment.NewLine + message + Environment.NewLine);
await Task.Delay(workTimeInMs);
if (messageReceiveOptions.IsAcknowledgmentConfigured)
{
messageReceiveOptions.Acknowledge();
}
}
#endregion
#region ServiceBus
public static void Test_ServiceBusFaF_Receive()
{
try
{
Console.WriteLine("ServiceBus test inbound messages [work time per message {0}ms].", workTimeInMs);
// Creating queue object from factory.
inboundMessageQueue = MessagingQueueFactory.CreateInboundFaF<string>(new AppSettingsConfigurationProvider(), "ServiceBusFaFInbound", new NQueueLogger("Default"));
//inboundMessageQueue.OnMessageReadyAsync += ServiceBusInboundMessageQueue_OnMessageReadyAsync;
inboundMessageQueue.OnMessageReady += ServiceBusInboundMessageQueue_OnMessageReady;
// Ready to start.
Console.WriteLine("Press any key to start receiving...");
Console.ReadKey();
// Checking is there any message.
Console.WriteLine("Queue has message: {0}", inboundMessageQueue.HasMessage());
// Starting.
inboundMessageQueue.StartReceivingMessage();
Console.WriteLine("Started....");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
private static void ServiceBusInboundMessageQueue_OnMessageReady(string message, IMessageReceiveOptions messageReceiveOptions)
{
Console.WriteLine("Pulled message successfully (ServiceBus)..." + Environment.NewLine + message + Environment.NewLine);
Thread.Sleep(workTimeInMs);
if (messageReceiveOptions.IsAcknowledgmentConfigured)
{
messageReceiveOptions.Acknowledge();
}
}
private static async Task ServiceBusInboundMessageQueue_OnMessageReadyAsync(string message, IMessageReceiveOptions messageReceiveOptions)
{
Console.WriteLine("Pulled message successfully (ServiceBus)..." + Environment.NewLine + message + Environment.NewLine);
await Task.Delay(workTimeInMs);
if (messageReceiveOptions.IsAcknowledgmentConfigured)
{
messageReceiveOptions.Acknowledge();
}
}
#endregion
}
}