-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractListenCommand.cs
49 lines (42 loc) · 1.31 KB
/
AbstractListenCommand.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
using System;
using System.Linq;
using System.Threading;
using NATS.Client;
using NLog;
namespace nats_tools
{
public abstract class AbstractListenCommand<T> : AbstractNatsCommand<T> where T : AbstractListenOptions
{
protected AbstractListenCommand(Logger logger) : base(logger)
{
}
protected int NbMessages { get; set; }
protected abstract void OnMessage(object sender, MsgHandlerEventArgs e);
public override int Run()
{
if (Options.Subjects == null || !Options.Subjects.Any())
{
Options.Subjects = new[] { ">" };
}
foreach (var subject in Options.Subjects)
{
Logger.Info($"Listen: {subject}");
Options.Connection.SubscribeAsync(subject, OnMessage);
}
DateTime end = DateTime.Now.AddSeconds(Options.Wait);
NbMessages = Options.Count;
while (
(Options.Count <= 0 || NbMessages > 0)
&& (Options.Wait < 0 || DateTime.Now < end))
{
Thread.Sleep(100);
}
Options.Connection.Close();
Dispose();
return 0;
}
protected virtual void Dispose()
{
}
}
}