Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SubscriptionTask.cs
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4using Waher.Events;
5
7{
11 internal class SubscriptionTask : MqTask
12 {
13 private readonly ManualResetEvent cancel;
14 private readonly TaskCompletionSource<bool> stopped;
15 private readonly MqMessageEventHandler callback;
16 private readonly string queue;
17 private readonly object state;
18
28 public SubscriptionTask(MqClient Client, string Queue, ManualResetEvent Cancel, TaskCompletionSource<bool> Stopped,
29 MqMessageEventHandler Callback, object State)
30 : base(Client)
31 {
32 this.queue = Queue;
33 this.cancel = Cancel;
34 this.stopped = Stopped;
35 this.callback = Callback;
36 this.state = State;
37 }
38
42 public override void Dispose()
43 {
44 this.stopped?.TrySetResult(true);
45 base.Dispose();
46 }
47
52 public override bool DoWork()
53 {
54 if (this.cancel?.WaitOne(0) ?? false)
55 {
56 this.Client.Information("Cancelling subscription to messages from " + this.queue);
57 this.stopped?.TrySetResult(true);
58 return false;
59 }
60 else
61 {
62 string Message = this.Client.GetOne(this.queue, 1000);
63
64 if (!(Message is null))
65 {
66 try
67 {
68 this.callback(this.Client, new MqMessageEventArgs(this.Client, Message, this.state));
69 }
70 catch (Exception ex)
71 {
72 Log.Exception(ex);
73 }
74 }
75
76 return true;
77 }
78 }
79 }
80}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
delegate Task MqMessageEventHandler(object Sender, MqMessageEventArgs e)
Delegate for MQ Message event handlers or callback methods.