示例#1
0
        /// <summary>
        /// Equals so that values can be compared
        /// </summary>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            TestBusKey key = obj as TestBusKey;

            return(Equals(key));
        }
        /// <summary>
        /// Create a new queue wrapper object in the dictionary of the bus
        /// </summary>
        public virtual void StartReceivingMessages()
        {
            if (IsListening)
            {
                throw new BusConfigurationException("Receiver is already listening to events!");
            }

            foreach (string topic in TopicFilters)
            {
                Logger.LogDebug($"Creating queue {QueueName} with topic {topic}");
                TestBusKey key = new TestBusKey(QueueName, topic);
                Context.DataQueues[key] = new TestBusQueueWrapper <EventMessage>();
            }

            IsListening = true;
        }
        /// <summary>
        /// Create a new thread with an endless loop that waits for new
        /// messages in a specific queue
        /// </summary>
        public virtual void StartHandlingMessages(EventMessageReceivedCallback callback)
        {
            foreach (string topic in TopicFilters)
            {
                Logger.LogDebug($"Creating thread for queue {QueueName} with topic {topic}");

                Thread thread = new Thread(() => {
                    while (true)
                    {
                        // If the receiver is paused, keep it waiting
                        if (IsPaused)
                        {
                            Thread.Sleep(1000);
                            continue;
                        }

                        TestBusKey key = new TestBusKey(QueueName, topic);

                        Logger.LogTrace($"Waiting for message on queue {QueueName} with topic {topic}");

                        // Wait for messages
                        Context.DataQueues[key].AutoResetEvent.WaitOne();

                        // If the thread was paused while a message came in, reset the resetevent and continue
                        if (IsPaused)
                        {
                            Context.DataQueues[key].AutoResetEvent.Set();
                            continue;
                        }

                        Context.DataQueues[key].Queue.TryDequeue(out EventMessage result);

                        Logger.LogDebug($"Message received on queue {QueueName} with topic {topic}");

                        callback(result);
                    }
                });

                thread.IsBackground = true;
                thread.Start();
            }
        }
示例#4
0
 private bool Equals(TestBusKey other) => _queueName == other._queueName && _topicName == other._topicName;