Beispiel #1
0
        async Task RunAsync()
        {
            await Task.Yield();

            Properties peekProps = Properties.Label | Properties.LookupId;

            try
            {
                for (;;)
                {
                    Message msg = _input.Peek(peekProps, TimeSpan.Zero) ?? await _input.PeekAsync(peekProps);

                    // at this point we only have the label and lookup id of the message
                    var subscribers = _subscriptions.Subscribers(msg.Label);
                    if (subscribers == null)
                    {
                        // no subscribers, remove message from input queue anyway
                        _input.Lookup(Properties.LookupId, msg.LookupId, LookupAction.ReceiveCurrent, TimeSpan.Zero);
                        continue;
                    }

                    // read the whole message now, remove it from the queue, and invoke the subscription callbacks
                    msg = _input.Lookup(Properties.All, msg.LookupId, LookupAction.ReceiveCurrent, TimeSpan.Zero);
                    if (msg == null)
                    {
                        Console.Error.WriteLine($"WARNING: we peeked message but it was removed before we could read it {{label={msg.Label}, lookupId={msg.LookupId}}}");
                        continue;
                    }

                    _subscriptions.TryInvokeAll(msg, subscribers);
                }
            }
            catch (ObjectDisposedException)
            {
                // Stop was called
            }
            catch (QueueException ex) when(ex.ErrorCode == ErrorCode.OperationCanceled)
            {
                // Stop was called
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("WARNING: " + ex);
            }
        }