public void TestDequeue()
        {
            MessageDispatchChannel channel = new MessageDispatchChannel();

            MessageDispatch dispatch1 = new MessageDispatch();
            MessageDispatch dispatch2 = new MessageDispatch();
            MessageDispatch dispatch3 = new MessageDispatch();

            channel.Start();
            Assert.IsTrue(channel.Running == true);

            DateTime timeStarted = DateTime.Now;

            Assert.IsTrue(channel.Dequeue(TimeSpan.FromMilliseconds(1000)) == null);

            DateTime timeFinished = DateTime.Now;

            TimeSpan elapsed = timeFinished - timeStarted;

            Assert.IsTrue(elapsed.TotalMilliseconds >= 999);

            channel.Enqueue(dispatch1);
            channel.Enqueue(dispatch2);
            channel.Enqueue(dispatch3);
            Assert.IsTrue(channel.Empty == false);
            Assert.IsTrue(channel.Count == 3);
            Assert.IsTrue(channel.Dequeue(TimeSpan.FromMilliseconds(Timeout.Infinite)) == dispatch1);
            Assert.IsTrue(channel.Dequeue(TimeSpan.Zero) == dispatch2);
            Assert.IsTrue(channel.Dequeue(TimeSpan.FromMilliseconds(1000)) == dispatch3);

            Assert.IsTrue(channel.Count == 0);
            Assert.IsTrue(channel.Empty == true);
        }
        public void TestDequeueNoWait()
        {
            MessageDispatchChannel channel = new MessageDispatchChannel();

            MessageDispatch dispatch1 = new MessageDispatch();
            MessageDispatch dispatch2 = new MessageDispatch();
            MessageDispatch dispatch3 = new MessageDispatch();

            Assert.IsTrue(channel.Running == false);
            Assert.IsTrue(channel.DequeueNoWait() == null);

            channel.Enqueue(dispatch1);
            channel.Enqueue(dispatch2);
            channel.Enqueue(dispatch3);

            Assert.IsTrue(channel.DequeueNoWait() == null);
            channel.Start();
            Assert.IsTrue(channel.Running == true);

            Assert.IsTrue(channel.Empty == false);
            Assert.IsTrue(channel.Count == 3);
            Assert.IsTrue(channel.DequeueNoWait() == dispatch1);
            Assert.IsTrue(channel.DequeueNoWait() == dispatch2);
            Assert.IsTrue(channel.DequeueNoWait() == dispatch3);

            Assert.IsTrue(channel.Count == 0);
            Assert.IsTrue(channel.Empty == true);
        }
        public void TestEnqueue()
        {
            MessageDispatchChannel channel   = new MessageDispatchChannel();
            MessageDispatch        dispatch1 = new MessageDispatch();
            MessageDispatch        dispatch2 = new MessageDispatch();

            Assert.IsTrue(channel.Empty == true);
            Assert.IsTrue(channel.Count == 0);

            channel.Enqueue(dispatch1);

            Assert.IsTrue(channel.Empty == false);
            Assert.IsTrue(channel.Count == 1);

            channel.Enqueue(dispatch2);

            Assert.IsTrue(channel.Empty == false);
            Assert.IsTrue(channel.Count == 2);
        }
        public void TestRemoveAll()
        {
            MessageDispatchChannel channel = new MessageDispatchChannel();

            MessageDispatch dispatch1 = new MessageDispatch();
            MessageDispatch dispatch2 = new MessageDispatch();
            MessageDispatch dispatch3 = new MessageDispatch();

            channel.Enqueue(dispatch1);
            channel.Enqueue(dispatch2);
            channel.Enqueue(dispatch3);

            channel.Start();
            Assert.IsTrue(channel.Running == true);
            Assert.IsTrue(channel.Empty == false);
            Assert.IsTrue(channel.Count == 3);
            Assert.IsTrue(channel.RemoveAll().Length == 3);
            Assert.IsTrue(channel.Count == 0);
            Assert.IsTrue(channel.Empty == true);
        }
Exemple #5
0
 public void Execute(MessageDispatch dispatch)
 {
     // Add the data to the queue.
     _messageQueue.Enqueue(dispatch);
     Wakeup();
 }
Exemple #6
0
        public void Dispatch(MessageDispatch dispatch)
        {
            var listener = _listener;

            try
            {
                lock ( _syncRoot )
                {
                    if (_clearDispatchList)
                    {
                        // we are reconnecting so lets flush the in progress messages
                        _clearDispatchList = false;
                        _unconsumedMessages.Clear();

                        // on resumption a pending delivered ACK will be out of sync with
                        // re-deliveries.
                        _pendingAck = null;
                    }

                    if (!_unconsumedMessages.Stopped)
                    {
                        if (listener != null && _unconsumedMessages.Started)
                        {
                            var message = CreateStompMessage(dispatch);

                            BeforeMessageIsConsumed(dispatch);

                            try
                            {
                                var expired = !IgnoreExpiration && message.IsExpired();

                                if (!expired)
                                {
                                    listener(message);
                                }

                                AfterMessageIsConsumed(dispatch, expired);
                            }
                            catch (Exception e)
                            {
                                if (_session.IsAutoAcknowledge || _session.IsIndividualAcknowledge)
                                {
                                    // Redeliver the message
                                }
                                else
                                {
                                    // Transacted or Client ACK: Deliver the next message.
                                    AfterMessageIsConsumed(dispatch, false);
                                }

                                Tracer.Error(ConsumerInfo.ConsumerId + " Exception while processing message: " + e);
                            }
                        }
                        else
                        {
                            _unconsumedMessages.Enqueue(dispatch);
                        }
                    }
                }

                if (++_dispatchedCount % 1000 != 0)
                {
                    return;
                }
                _dispatchedCount = 0;
                Thread.Sleep(1);
            }
            catch (Exception e)
            {
                _session.Connection.OnSessionException(_session, e);
            }
        }