public void CloseBehviour() { var queue = new SharedQueue(); queue.Enqueue("Hello World"); //queue.Close(); var item = queue.Dequeue().ToString(); Console.Out.WriteLine("item = {0}", item); ThreadPool.QueueUserWorkItem(state => { // expect the queue to block here Console.WriteLine("Dequeue in thread"); try { var item2 = queue.Dequeue().ToString(); } catch (Exception e) { Console.WriteLine("Exception in thread\n{0}", e.ToString()); } }); Thread.Sleep(100); Console.WriteLine("Closing queue"); queue.Close(); Console.WriteLine("Done"); }
public void ClearConsumers() { callbacks.Clear(); sharedQueue.Close(); // Dequeue will stop blocking and throw an EndOfStreamException Console.WriteLine("Waiting for ClearConsumers lock"); lock (sharedQueueLock) { Console.WriteLine("Got ClearConsumers lock"); sharedQueue = new SharedQueue(); } //Console.WriteLine("Cleared ClearConsumers lock"); }
public EasyNetQConsumer(IModel model, SharedQueue queue) : base(model) { this.queue = queue; }
///<summary>Creates a fresh QueueingBasicConsumer, ///initialising the Model and Queue properties to the given ///values.</summary> public QueueingBasicConsumer(IModel model, SharedQueue queue) : base(model) { m_queue = queue; }
public static void EnqueueAfter(int delayMs, SharedQueue q, object v) { DelayedEnqueuer de = new DelayedEnqueuer(q, delayMs, v); new Thread(new ThreadStart(de.EnqueueValue)).Start(); }
public void TestTimeoutShort() { SharedQueue q = new SharedQueue(); q.Enqueue(123); ResetTimer(); object v; bool r = q.Dequeue(TimingInterval, out v); Assert.Greater(SafetyMargin, ElapsedMs()); Assert.IsTrue(r); Assert.AreEqual(123, v); }
public void TestTimeoutLong() { SharedQueue q = new SharedQueue(); ResetTimer(); object v; bool r = q.Dequeue(TimingInterval, out v); Assert.Less(TimingInterval - SafetyMargin, ElapsedMs()); Assert.IsTrue(!r); Assert.AreEqual(null, v); }
public void TestEnumerator() { SharedQueue q = new SharedQueue(); VolatileInt c1 = new VolatileInt(); VolatileInt c2 = new VolatileInt(); Thread t1 = new Thread(delegate() { foreach (int v in q) c1.v+=v; }); Thread t2 = new Thread(delegate() { foreach (int v in q) c2.v+=v; }); t1.Start(); t2.Start(); q.Enqueue(1); q.Enqueue(2); q.Enqueue(3); q.Close(); t1.Join(); t2.Join(); Assert.AreEqual(6, c1.v + c2.v); }
public void TestDequeueNoWait3() { SharedQueue q = new SharedQueue(); Assert.AreEqual(null, q.DequeueNoWait(null)); }
///<summary>Construct an enumerator for the given ///SharedQueue.</summary> public SharedQueueEnumerator(SharedQueue <T> queue) { m_queue = queue; m_current = default(T); }
public QueueingAutoAckConsumer(IModel model, SharedQueue<BasicDeliverEventArgs> queue) : base(model, queue) { }
///<summary>Construct an enumerator for the given ///SharedQueue.</summary> public SharedQueueEnumerator(SharedQueue <T> queue) { _queue = queue; _current = default; }
public static void EnqueueAfter(int delayMs, SharedQueue queue, object v) { var enqueuer = new DelayedEnqueuer(queue, delayMs, v); new Thread(enqueuer.EnqueueValue).Start(); }
public void TestEnumerator() { var queue = new SharedQueue(); var c1 = new VolatileInt(); var c2 = new VolatileInt(); var thread1 = new Thread(() => { foreach (int v in queue) c1.v += v; }); var thread2 = new Thread(() => { foreach (int v in queue) c2.v += v; }); thread1.Start(); thread2.Start(); queue.Enqueue(1); queue.Enqueue(2); queue.Enqueue(3); queue.Close(); thread1.Join(); thread2.Join(); Assert.AreEqual(6, c1.v + c2.v); }
/// <summary> /// Creates a fresh <see cref="QueueingBasicConsumer"/>, /// initialising the <see cref="DefaultBasicConsumer.Model"/> /// and <see cref="Queue"/> properties to the given values. /// </summary> public QueueingBasicConsumer(IModel model, SharedQueue<BasicDeliverEventArgs> queue) : base(model) { Queue = queue; }
public void TestDequeueNoWait1() { var queue = new SharedQueue(); queue.Enqueue(1); Assert.AreEqual(1, queue.DequeueNoWait(0)); Assert.AreEqual(0, queue.DequeueNoWait(0)); }
public RabbitMqBasicConsumer(IModel model, SharedQueue<BasicGetResult> queue) : base(model) { this.queue = queue; }
public void TestDoublePoll() { SharedQueue q = new SharedQueue(); EnqueueAfter(TimingInterval * 2, q, 123); ResetTimer(); object v; bool r; r = q.Dequeue(TimingInterval, out v); Assert.Less(TimingInterval - SafetyMargin, ElapsedMs()); Assert.Greater(TimingInterval + SafetyMargin, ElapsedMs()); Assert.IsTrue(!r); Assert.AreEqual(null, v); r = q.Dequeue(TimingInterval * 2, out v); Assert.Less(TimingInterval * 2 - SafetyMargin, ElapsedMs()); Assert.Greater(TimingInterval * 2 + SafetyMargin, ElapsedMs()); Assert.IsTrue(r); Assert.AreEqual(123, v); }
public void Restart() { isStarted = true; //CLose connections and channels if (subscriberChannel != null) { try { subscriberChannel.Close(); } catch { } } if (workChannel != null) { try { workChannel.Close(); } catch { } } if (conn != null) { try { conn.Close(); } catch { } try { conn.Dispose(); } catch { } } //TODO: CreateConnection() can always throw BrokerUnreachableException so keep that in mind when calling conn = connectionFactory.CreateConnection(); //Create shared queue SharedQueue<BasicDeliverEventArgs> queue = new SharedQueue<BasicDeliverEventArgs>(); //Create work channel and declare exchanges and queues workChannel = conn.CreateModel(); /* Work this into subscriber dispose and restart //Cancel consumers on server if(workCTag != null) { try { workChannel.BasicCancel(workCTag); } catch { } } if (subscriberCTag != null) { try { workChannel.BasicCancel(subscriberCTag); } catch { } } */ //Redeclare exchanges and queues AmqpUtils.DeclareExchangeAndQueues(workChannel, exchangeInfo, exchangeDeclareSync, subscriberId); //Listen on work queue workConsumer = new QueueingBasicConsumer(workChannel, queue); string workQueueName = AmqpUtils.GetWorkQueueName(exchangeInfo); workChannel.BasicConsume(workQueueName, false, workConsumer); //Listen on subscriber queue subscriberChannel = conn.CreateModel(); subscriberConsumer = new QueueingBasicConsumer(subscriberChannel, queue); string subscriberWorkQueueName = AmqpUtils.GetSubscriberQueueName(exchangeInfo, subscriberId); subscriberChannel.BasicConsume(subscriberWorkQueueName, false, subscriberConsumer); }
public void TestTimeoutInfinite() { SharedQueue q = new SharedQueue(); EnqueueAfter(TimingInterval, q, 123); ResetTimer(); object v; bool r = q.Dequeue(Timeout.Infinite, out v); Assert.Less(TimingInterval - SafetyMargin, ElapsedMs()); Assert.IsTrue(r); Assert.AreEqual(123, v); }
private bool TryGetRequest(SharedQueue<BasicDeliverEventArgs> queue, out HttpRequestPacket request, out IBasicProperties properties) { object obj; BasicDeliverEventArgs evt; request = null; properties = null; obj = queue.DequeueNoWait(null); if (obj != null) { evt = (BasicDeliverEventArgs)obj; } else { return false; } //Get message properties properties = evt.BasicProperties; //Deserialize message bool wasDeserialized = true; try { request = HttpRequestPacket.Deserialize(evt.Body); //Add/Update Content-Length Header request.Headers["Content-Length"] = new string[] { (request.Content == null ? 0 : request.Content.Length).ToString() }; //Add/Update Subscriber-Id header request.Headers[Common.Shared.SUBSCRIBER_ID_HEADER] = new string[] { this.subscriberId }; } catch { wasDeserialized = false; } //Ack or reject message if (evt.ConsumerTag == workConsumer.ConsumerTag) { if (wasDeserialized) { workConsumer.Model.BasicAck(evt.DeliveryTag, false); return true; } else { workConsumer.Model.BasicReject(evt.DeliveryTag, false); return false; } } else if (evt.ConsumerTag == subscriberConsumer.ConsumerTag) { if (wasDeserialized) { subscriberConsumer.Model.BasicAck(evt.DeliveryTag, false); return true; } else { subscriberConsumer.Model.BasicReject(evt.DeliveryTag, false); return false; } } else { throw new InvalidOperationException("Message was dequeued by an unexpected/unknown consumer"); } }
public void TestTimeoutNegative() { SharedQueue q = new SharedQueue(); ResetTimer(); object v; bool r = q.Dequeue(-10000, out v); Assert.Greater(SafetyMargin, ElapsedMs()); Assert.IsTrue(!r); Assert.AreEqual(null, v); }
//Will block until a request is received from either queue public MessageContext Dequeue() { if (disposed) throw new ObjectDisposedException("Subscriber has been disposed"); if(workConsumer == null || subscriberConsumer == null) throw new InvalidOperationException("Start the subscriber prior to calling Dequeue"); HttpRequestPacket request; IBasicProperties properties; SharedQueue<BasicDeliverEventArgs> queue1 = null, queue2 = null; while (true) { if (disposed) throw new ObjectDisposedException("Subscriber has been disposed"); if (lastProcessedQueue == subscriberConsumer.Queue) { queue1 = workConsumer.Queue; queue2 = subscriberConsumer.Queue; } else { queue1 = subscriberConsumer.Queue; queue2 = workConsumer.Queue; } try { if (TryGetRequest(queue1, out request, out properties)) { lastProcessedQueue = queue1; break; } if (TryGetRequest(queue2, out request, out properties)) { lastProcessedQueue = queue2; break; } } catch (Exception e) { if (!(e is System.IO.EndOfStreamException)) { //TODO: Log exception: Don't know what else to expect here } //TODO: IS this the best place to place reconnection logic? In a catch block?? //Loop until a connection is made bool successfulRestart = false; while (true) { try { Restart(); successfulRestart = true; } catch { } if (disposed) throw new ObjectDisposedException("Subscriber has been disposed"); if (successfulRestart) break; Thread.Sleep(1); } //Check for next message continue; } Thread.Sleep(1); //Nothing was found in both queues so take a 1ms nap } return new MessageContext { Request = request, ReplyToQueue = properties == null ? null : properties.ReplyTo, CorrelationId = properties.CorrelationId }; }
public DelayedEnqueuer(SharedQueue q, int delayMs, object v) { m_q = q; m_delayMs = delayMs; m_v = v; }
public void TestCloseWhenFull() { SharedQueue q = new SharedQueue(); object v; q.Enqueue(1); q.Enqueue(2); q.Enqueue(3); q.Close(); DelayedEnqueuer de = new DelayedEnqueuer(q, 0, 4); ExpectEof(new Thunk(de.EnqueueValue)); Assert.AreEqual(1, q.Dequeue()); Assert.AreEqual(2, q.DequeueNoWait(0)); bool r = q.Dequeue(1, out v); Assert.IsTrue(r); Assert.AreEqual(3, v); ExpectEof(new Thunk(de.Dequeue)); }
public void TestBgLong() { SharedQueue q = new SharedQueue(); EnqueueAfter(TimingInterval * 2, q, 123); ResetTimer(); object v; bool r = q.Dequeue(TimingInterval, out v); Assert.Greater(TimingInterval + SafetyMargin, ElapsedMs()); Assert.IsTrue(!r); Assert.AreEqual(null, v); }
public void TestCloseWhenWaiting() { SharedQueue q = new SharedQueue(); DelayedEnqueuer de = new DelayedEnqueuer(q, 0, null); Thread t = new Thread(new ThreadStart(de.BackgroundEofExpectingDequeue)); t.Start(); Thread.Sleep(SafetyMargin); q.Close(); t.Join(); }
/// <summary> /// Initializes a new instance of the <see cref="SharedQueueDecorator"/> class. /// </summary> /// <param name="sharedQueue">The shared queue.</param> internal SharedQueueDecorator(SharedQueue<BasicDeliverEventArgs> sharedQueue) { _sharedQueue = sharedQueue; }
public void TestDelayedEnqueue() { SharedQueue q = new SharedQueue(); ResetTimer(); EnqueueAfter(TimingInterval, q, 1); Assert.AreEqual(0, q.DequeueNoWait(0)); Assert.Greater(TimingInterval - SafetyMargin, ElapsedMs()); Assert.AreEqual(1, q.Dequeue()); Assert.Less(TimingInterval - SafetyMargin, ElapsedMs()); }
public void ClearConsumers() { sharedQueue.Close(); // Dequeue will stop blocking and throw an EndOfStreamException lock (sharedQueueLock) { logger.DebugWrite("Clearing consumer subscriptions"); sharedQueue = new SharedQueue(); subscriptions.Clear(); } }
public void TestDequeueNoWait2() { SharedQueue q = new SharedQueue(); q.Enqueue(1); Assert.AreEqual(1, q.Dequeue()); Assert.AreEqual(0, q.DequeueNoWait(0)); }
public LocalQueueingBasicConsumer(IModel model, SharedQueue queue) : base(model) { this.m_queue = queue; }
///<summary>Construct an enumerator for the given ///SharedQueue.</summary> public SharedQueueEnumerator(SharedQueue queue) { m_queue = queue; }