public IDisposable Subscribe( IMessagingSession messagingSession, string destination, Action <BinaryMessage, Action <bool> > callback, string messageType, int priority) { if (m_IsDisposing) { throw new ObjectDisposedException("ProcessingGroup " + Name); } var taskFactory = m_SchedulingStrategy.GetTaskFactory(priority); var subscription = new SingleAssignmentDisposable(); subscription.Disposable = messagingSession.Subscribe(destination, (message, ack) => { Interlocked.Increment(ref m_TasksInProgress); taskFactory.StartNew(() => { Interlocked.Increment(ref m_ReceivedMessages); //if subscription is disposed unack message immediately if (subscription.IsDisposed) { ack(false); } else { callback(message, ack); Interlocked.Increment(ref m_ProcessedMessages); } Interlocked.Decrement(ref m_TasksInProgress); }, TaskCreationOptions.HideScheduler); }, messageType); return(subscription); }
public void SendFailureTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var delivered = new ManualResetEvent(false); IMessagingSession messagingSession = transport.CreateSession(() => Console.WriteLine("onFailure called")); /*FieldInfo field = typeof(RabbitMqSession).GetField("m_Connection", BindingFlags.NonPublic | BindingFlags.Instance); * var connection = field.GetValue(messagingSession) as IConnection; * connection.Abort(1, "All your base are belong to us");*/ while (true) { try { messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = typeof(byte[]).Name }, 0); } catch (Exception e) { Console.WriteLine(e.Message + "!!!!!!!!!"); throw; } Thread.Sleep(1000); Console.WriteLine('.'); } } }
public MessageContextualInfoProvider(IMessagingSession session) { _session = session; Title = "Messaging Log"; ShortTitle = "ServiceBus"; }
public void PerformanceTest(int messageSize, bool confirmedSending) { var messageBytes = new byte[messageSize]; new Random().NextBytes(messageBytes); using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { IMessagingSession messagingSession = transport.CreateSession(null, confirmedSending); Stopwatch sw = Stopwatch.StartNew(); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = messageBytes, Type = typeof(byte[]).Name }, 0); int sendCounter; for (sendCounter = 0; sw.ElapsedMilliseconds < 4000; sendCounter++) { messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = messageBytes, Type = typeof(byte[]).Name }, 0); } int receiveCounter = 0; var ev = new ManualResetEvent(false); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => receiveCounter++, typeof(byte[]).Name); ev.WaitOne(2000); Console.WriteLine("Send: {0} per second. {1:0.00} Mbit/s", sendCounter / 4, 1.0 * sendCounter * messageSize / 4 / 1024 / 1024 * 8); Console.WriteLine("Receive: {0} per second. {1:0.00} Mbit/s", receiveCounter / 2, 1.0 * receiveCounter * messageSize / 2 / 1024 / 1024 * 8); } }
public void AcknowledgeTest() { var delivered1 = new ManualResetEvent(false); var delivered2 = new ManualResetEvent(false); bool processedOnce = false; using (var transport = new InMemoryTransport()) { IMessagingSession messagingSession = transport.CreateSession(null); messagingSession.Subscribe(TEST_TOPIC, (message, ack) => { var receivedGuid = new Guid(message.Bytes); Console.WriteLine("subscription1: message {0} of type {1}", receivedGuid, message.Type); if (!processedOnce) { Console.WriteLine("Unack for the first delivery"); processedOnce = true; ack(false); delivered1.Set(); return; } Console.WriteLine("Ack for the second delivery"); delivered2.Set(); }, typeof(byte[]).Name); var guid = Guid.NewGuid(); messagingSession.Send(TEST_TOPIC, new BinaryMessage { Bytes = guid.ToByteArray(), Type = typeof(byte[]).Name }, 0); Assert.That(delivered1.WaitOne(1000), Is.True, "message was not delivered"); Assert.That(delivered2.WaitOne(1000), Is.True, "message was not delivered redelivered on unack"); } }
public void UnknownMessageTypeHandlerWaitingDoesNotPreventTransportDisposeTest() { var received = new ManualResetEvent(false); Thread connectionThread = null; using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { IMessagingSession messagingSession = transport.CreateSession(null); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { connectionThread = Thread.CurrentThread; received.Set(); }, "type1"); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1" }, 0); Assert.That(received.WaitOne(100), Is.True, "Message was not delivered"); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type2" }, 0); } GC.Collect(); Thread.Sleep(30000); Assert.That(connectionThread.ThreadState, Is.EqualTo(ThreadState.Stopped), "Processing thread is still active in spite of transport dispose"); }
public void SessionIsTreatedAsBrokenAfterSendFailureWithAlreadyClosedExceptionTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var onFailureCalled = new AutoResetEvent(false); IMessagingSession messagingSession = transport.CreateSession(() => { onFailureCalled.Set(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId); }); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "messageType" }, 0); FieldInfo field = typeof(RabbitMqSession).GetField("m_Connection", BindingFlags.NonPublic | BindingFlags.Instance); var connection = field.GetValue(messagingSession) as IConnection; connection.Abort(1, "All your base are belong to us"); AlreadyClosedException ex = null; try { messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "messageType" }, 0); } catch (AlreadyClosedException e) { ex = e; } Assert.That(ex, Is.Not.Null, "Exception was not thrown on send fail"); Assert.That(ex, Is.InstanceOf <AlreadyClosedException>(), "Wrong exception type was thrown on send fail"); Assert.That(transport.SessionsCount, Is.EqualTo(0), "session was not removed after send failed AlreadyClosedException "); Assert.That(onFailureCalled.WaitOne(500), Is.True, "Subsciptionwas not notefied on failure"); } }
public void NackTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var delivered = new ManualResetEvent(false); IMessagingSession messagingSession = transport.CreateSession(null); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = typeof(byte[]).Name }, 0); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { Console.WriteLine("message:" + message.Type); delivered.Set(); acknowledge(false); }, typeof(byte[]).Name); Assert.That(delivered.WaitOne(300), Is.True, "Message was not delivered"); } using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var delivered = new ManualResetEvent(false); IMessagingSession messagingSession = transport.CreateSession(null); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => delivered.Set(), typeof(byte[]).Name); Assert.That(delivered.WaitOne(1000), Is.True, "Message was not returned to queue"); } }
public void SendTest() { using (var transport = new InMemoryTransport()) { var delivered1 = new ManualResetEvent(false); var delivered2 = new ManualResetEvent(false); IMessagingSession messagingSession = transport.CreateSession(null); messagingSession.Subscribe(TEST_TOPIC, (message, ack) => { delivered1.Set(); Console.WriteLine("subscription1: message:" + message.Type); }, typeof(byte[]).Name); messagingSession.Subscribe(TEST_TOPIC, (message, ack) => { delivered2.Set(); Console.WriteLine("subscription2: message:" + message.Type); }, typeof(byte[]).Name); messagingSession.Send(TEST_TOPIC, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = typeof(byte[]).Name }, 0); Assert.That(delivered1.WaitOne(1000), Is.True, "message was not delivered to all subscribers"); Assert.That(delivered2.WaitOne(1000), Is.True, "message was not delivered to all subscribers"); Thread.Sleep(1000); } }
public MessageContextualInfoProvider(IMessagingSession session) { _session = session; Title = "Messaging Log"; ShortTitle = "ServiceBus"; }
public void MessageOfUnknownTypeShouldPauseProcessingTillCorrespondingHandlerIsRegisteredTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { IMessagingSession messagingSession = transport.CreateSession(null); var type1Received = new AutoResetEvent(false); var type2Received = new AutoResetEvent(false); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { type1Received.Set(); acknowledge(true); }, "type1"); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1" }, 0); Assert.That(type1Received.WaitOne(500), Is.True, "Message of subscribed type was not delivered"); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type2" }, 0); //Give time for type2 message to be pushed back by mq //Thread.Sleep(500); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1" }, 0); Assert.That(type1Received.WaitOne(500), Is.False, "Message of not subscribed type has not paused processing"); Assert.That(type2Received.WaitOne(500), Is.False, "Message of not subscribed type has not paused processing"); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { type2Received.Set(); acknowledge(true); }, "type2"); Assert.That(type1Received.WaitOne(500), Is.True, "Processing was not resumed after handler for unknown message type was registered"); Assert.That(type2Received.WaitOne(500), Is.True, "Processing was not resumed after handler for unknown message type was registered"); } }
public void Start() { if (_isStarted) { return; } _isStarted = true; var registry = Activator.CreateInstance(_registryType).As <FubuRegistry>(); registry.NodeName = _name; registry.ServiceBus.EnableInMemoryTransport(); registry.Services.ReplaceService <IEnvelopeHandler, ExternalNodeEnvelopeHandler>(); TestNodes.Alterations.Each(x => x(registry)); var container = new Container(x => { x.ForSingletonOf <IMessageRecorder>().Use <MessageRecorder>(); x.Forward <IMessageRecorder, IListener>(); }); registry.StructureMap(container); Runtime = registry.ToRuntime(); Uri = Runtime.Get <ChannelGraph>().ReplyUriList().First(); _recorder = Runtime.Get <IMessageRecorder>(); // Wireup the messaging session so the MessageHistory gets notified of messages on this node _messageListener = Runtime.Get <IMessagingSession>(); FubuMVC.Core.Services.Messaging.GlobalMessageTracking.Messaging.AddListener(_messageListener); }
public void AttemptToSubscribeSameDestinationAndMessageTypeTwiceFailureTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { IMessagingSession messagingSession = transport.CreateSession(null); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { }, "type1"); Assert.That(() => messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { }, "type1"), Throws.TypeOf <InvalidOperationException>()); } }
public void Send( IMessagingSession messagingSession, string publish, BinaryMessage message, int ttl) { messagingSession.Send(publish, message, ttl); Interlocked.Increment(ref m_SentMessages); }
public void Dispose() { if (_messagingSession == null) { return; } _messagingSession.Dispose(); _messagingSession = null; }
public void UnsubscribeTest() { using (var transport = new InMemoryTransport()) { var ev = new AutoResetEvent(false); IMessagingSession messagingSession = transport.CreateSession(null); IDisposable subscription = messagingSession.Subscribe(TEST_TOPIC, (message, ack) => ev.Set(), null); messagingSession.Send(TEST_TOPIC, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = null }, 0); Assert.That(ev.WaitOne(500), Is.True, "Message was not delivered"); subscription.Dispose(); Assert.That(ev.WaitOne(500), Is.False, "Message was delivered for canceled subscription"); } }
public void UnsubscribeTest(string messageType) { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var ev = new AutoResetEvent(false); IMessagingSession messagingSession = transport.CreateSession(null); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = messageType }, 0); IDisposable subscription = messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => ev.Set(), messageType); Assert.That(ev.WaitOne(500), Is.True, "Message was not delivered"); subscription.Dispose(); Assert.That(ev.WaitOne(500), Is.False, "Message was delivered for canceled subscription"); } }
public void HandlerWaitStopsAndMessageOfUnknownTypeReturnsToQueueOnUnsubscribeTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { IMessagingSession messagingSession = transport.CreateSession(null); var received = new AutoResetEvent(false); IDisposable subscription = messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { received.Set(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId); }, "type2"); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1" }, 0); Assert.That(received.WaitOne(500), Is.False, "Message of not subscribed type has not paused processing"); subscription.Dispose(); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => received.Set(), "type1"); Assert.That(received.WaitOne(500), Is.True, "Message was not returned to queue"); } }
public void DisposeTest() { ManualResetEvent delivered = new ManualResetEvent(false); int deliveredMessagesCount = 0; var transport = new InMemoryTransport(); IMessagingSession messagingSession = transport.CreateSession(null); messagingSession.Subscribe(TEST_TOPIC, (message, ack) => { delivered.WaitOne(); Interlocked.Increment(ref deliveredMessagesCount); }, null); messagingSession.Send(TEST_TOPIC, new BinaryMessage(), 0); Thread.Sleep(200); var task = Task.Factory.StartNew(transport.Dispose); Assert.That(task.Wait(200), Is.False, "transport was disposd before all message processing finished"); delivered.Set(); Assert.That(task.Wait(1000), Is.True, "transport was not disposd after all message processing finished"); }
public void ConnectionFailureTest() { using (var transport = new RabbitMqTransport(_logFactory, HOST, "guest", "guest")) { var onFailureCalled = new AutoResetEvent(false); IMessagingSession messagingSession = transport.CreateSession(() => { onFailureCalled.Set(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId); }); messagingSession.Send(TEST_EXCHANGE, new BinaryMessage { Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "messageType" }, 0); messagingSession.Subscribe(TEST_QUEUE, (message, acknowledge) => { }, "messageType"); FieldInfo field = typeof(RabbitMqSession).GetField("m_Connection", BindingFlags.NonPublic | BindingFlags.Instance); var connection = field.GetValue(messagingSession) as IConnection; connection.Abort(1, "All your base are belong to us"); Assert.That(onFailureCalled.WaitOne(500), Is.True, "Subsciptionwas not notefied on failure"); } }
public MessageRecordListener(IMessagingSession session) { _session = session; }
public void SetSession(IMessagingSession messagingSession) { _messagingSession = messagingSession; }
public MessageContextualInfoProvider(IMessagingSession session) { _session = session; }
public MessageRecordListener(IMessagingSession session) { _session = session; }
public MessageContextualInfoProvider(IMessagingSession session) { _session = session; }