private void OnMessage(BusMessage<ScaleoutMessage> message) { string sIndex = message.Headers.OfType<BusHeader>().Where(h => h.Name == StreamIndexHeader).Select(h => h.Value).FirstOrDefault(); int streamIndex; Int32.TryParse(sIndex, out streamIndex); OnReceived(streamIndex, (ulong)Interlocked.Increment(ref _payloadId), message.Data); }
public void Bus_PublishedMessage_FilterByHeader() { Person actualT1 = new Person { Id = 5 }; Person actualT2 = new Person { Id = 15 }; const string header = "type"; using (MessageBus.Core.RabbitMQBus busA = new MessageBus.Core.RabbitMQBus(), busB = new MessageBus.Core.RabbitMQBus(), busC = new MessageBus.Core.RabbitMQBus()) { using (IReceiver receiverB1 = busB.CreateReceiver(), receiverB2 = busB.CreateReceiver(), receiverC1 = busC.CreateReceiver()) { receiverB1.Subscribe<Person>(filter: new[] { new BusHeader { Name = header, Value = "T1" } }); receiverB2.Subscribe<Person>(filter: new[] { new BusHeader { Name = header, Value = "T2" } }); receiverC1.Subscribe<Person>(filter: new[] { new BusHeader { Name = header, Value = "T1" } }); receiverB1.Open(); receiverB2.Open(); receiverC1.Open(); using (IPublisher publisher = busA.CreatePublisher()) { BusMessage<Person> m1 = new BusMessage<Person> { Data = actualT1 }; m1.Headers.Add(new BusHeader { Name = header, Value = "T1" }); publisher.Send(m1); BusMessage<Person> m2 = new BusMessage<Person> { Data = actualT2 }; m2.Headers.Add(new BusHeader { Name = header, Value = "T2" }); publisher.Send(m2); } Person b1 = receiverB1.Receive<Person>(); Person b2 = receiverB2.Receive<Person>(); Person c1 = receiverC1.Receive<Person>(); b1.Should().NotBeNull(); b2.Should().NotBeNull(); c1.Should().NotBeNull(); b1.ShouldBeEquivalentTo(actualT1); b2.ShouldBeEquivalentTo(actualT2); c1.ShouldBeEquivalentTo(actualT1); } } }
public void Bus_BusMessage_HeadersAndPropertiesUsage() { const string busId = "Bus"; using (MessageBus.Core.RabbitMQBus bus = new MessageBus.Core.RabbitMQBus(c => c.SetBusId(busId))) { BusMessage<Person> message = new BusMessage<Person> { Data = new Person { Id = 5 } }; message.Headers.Add(new BusHeader { Name = "Version", Value = "Ver1" }); BusMessage<Person> received = null; ManualResetEvent ev = new ManualResetEvent(false); DateTime sent; using (ISubscriber subscriber = bus.CreateSubscriber(c => c.SetReceiveSelfPublish())) { subscriber.Subscribe((Action<BusMessage<Person>>) (m => { received = m; ev.Set(); })); subscriber.Open(); using (IPublisher publisher = bus.CreatePublisher()) { sent = DateTime.Now; publisher.Send(message); } bool wait = ev.WaitOne(TimeSpan.FromSeconds(5)); wait.Should().BeTrue(); } received.ShouldBeEquivalentTo(message, options => options.Excluding(m => m.BusId).Excluding(m => m.Sent)); received.BusId.Should().Be(busId); received.Sent.Should().BeCloseTo(sent, 1000); } }
protected override Task Send(int streamIndex, IList<Message> messages) { return Task.Factory.StartNew(() => { var busMessage = new BusMessage<ScaleoutMessage> { Data = new ScaleoutMessage(messages) }; busMessage.Headers.Add(new BusHeader { Name = StreamIndexHeader, Value = streamIndex.ToString() }); _publisher.Send(busMessage); }); }
public static RawBusMessage ToRawBusMessage <T>(this BusMessage <T> message) { if (message == null) { return(null); } RawBusMessage rawBusMessage = new RawBusMessage { Data = message.Data }; foreach (var header in message.Headers) { rawBusMessage.Headers.Add(header); } return(rawBusMessage); }
public void Bus_MessageBaseSubscription_CallReceived() { ManualResetEvent ev = new ManualResetEvent(false); MessageBasedImplementation implementation = new MessageBasedImplementation(ev); using (IBus bus = new MessageBus.Core.RabbitMQBus()) { using (ISubscription subscription = bus.RegisterSubscription(implementation, c => c.SetReceiveSelfPublish())) { subscription.Open(); using (IPublisher publisher = bus.CreatePublisher()) { Person person = new Person {Id = 5}; BusMessage<Person> busMessage = new BusMessage<Person> { Data = person }; busMessage.Headers.Add(new BusHeader { Name = "Header", Value = "RightValue" }); publisher.Send(busMessage); bool waitOne = ev.WaitOne(TimeSpan.FromSeconds(5000)); waitOne.Should().BeTrue(); busMessage.ShouldBeEquivalentTo(implementation.Message, options => options.Excluding(message => message.BusId) .Excluding(message => message.Sent)); } } } }
public void Bus_UndeliverableMessages_ReturnedToSubscriber() { using (IBus bus = new MessageBus.Core.RabbitMQBus()) { using (IPublisher publisher = bus.CreatePublisher(c => c.SetMandatoryDelivery().UseErrorHandler(this))) { Person person = new Person {Id = 5}; BusMessage<Person> busMessage = new BusMessage<Person> { Data = person }; busMessage.Headers.Add(new BusHeader { Name = "Header", Value = "Value" }); publisher.Send(busMessage); bool waitOne = _ev.WaitOne(TimeSpan.FromSeconds(5)); waitOne.Should().BeTrue(); _errorCode.Should().Be(312); _text.Should().Be("NO_ROUTE"); _message.BusId.Should().Be(bus.BusId); _message.Name.Should().Be("Person"); _message.Sent.Should().BeCloseTo(DateTime.Now, 2000); _message.Data.Should().BeOfType<Person>(); _message.Headers.OfType<BusHeader>().Should().OnlyContain(header => header.Name == "Header" && header.Value == "Value"); person.ShouldBeEquivalentTo(_message.Data); } } }
public void Bus_SimpleSubscribtion_CallReceivedOnImplementationInstace() { ManualResetEvent ev = new ManualResetEvent(false); SimpleImplementation implementation = new SimpleImplementation(ev); using (IBus bus = new RabbitMQBus()) { using (ISubscription subscription = bus.RegisterSubscription(implementation)) { subscription.Open(); using (IPublisher publisher = bus.CreatePublisher()) { Person person = new Person {Id = 5}; BusMessage<Person> busMessage = new BusMessage<Person> { Data = person }; busMessage.Headers.Add(new BusHeader { Name = "Header", Value = "Value" }); publisher.Send(busMessage); bool waitOne = ev.WaitOne(TimeSpan.FromSeconds(5)); waitOne.Should().BeTrue(); person.ShouldBeEquivalentTo(implementation.Person); } } } }
public void Bus_PublishedMessage_FilterByHeader() { Person actualT1 = new Person { Id = 5 }; Person actualT2 = new Person { Id = 15 }; Person b1 = null, b2 = null, c1 = null; const string header = "type"; ManualResetEvent ev1 = new ManualResetEvent(false), ev2 = new ManualResetEvent(false), ev3 = new ManualResetEvent(false); using (MessageBus.Core.RabbitMQBus busA = new MessageBus.Core.RabbitMQBus(), busB = new MessageBus.Core.RabbitMQBus(), busC = new MessageBus.Core.RabbitMQBus()) { using (ISubscriber subscriberB1 = busB.CreateSubscriber(), subscriberB2 = busB.CreateSubscriber(), subscriberC1 = busC.CreateSubscriber()) { subscriberB1.Subscribe<Person>(p => { b1 = p; ev1.Set(); }, filter: new[] { new BusHeader { Name = header, Value = "T1"} }); subscriberB2.Subscribe<Person>(p => { b2 = p; ev2.Set(); }, filter: new[] { new BusHeader { Name = header, Value = "T2"} }); subscriberC1.Subscribe<Person>(p => { c1 = p; ev3.Set(); }, filter: new[] { new BusHeader { Name = header, Value = "T1" } }); subscriberB1.Open(); subscriberB2.Open(); subscriberC1.Open(); using (IPublisher publisher = busA.CreatePublisher()) { BusMessage<Person> m1 = new BusMessage<Person> {Data = actualT1}; m1.Headers.Add(new BusHeader { Name = header, Value = "T1"}); publisher.Send(m1); BusMessage<Person> m2 = new BusMessage<Person> { Data = actualT2 }; m2.Headers.Add(new BusHeader { Name = header, Value = "T2" }); publisher.Send(m2); } bool wait = ev1.WaitOne(TimeSpan.FromSeconds(5)) && ev2.WaitOne(TimeSpan.FromSeconds(5)) && ev3.WaitOne(TimeSpan.FromSeconds(5)); wait.Should().BeTrue("Message should arrive to all subscribers"); b1.Should().NotBeNull(); b2.Should().NotBeNull(); c1.Should().NotBeNull(); b1.ShouldBeEquivalentTo(actualT1); b2.ShouldBeEquivalentTo(actualT2); c1.ShouldBeEquivalentTo(actualT1); } } }
public void ProcessPerson(BusMessage<Person> message) { _message = message; _ev.Set(); }
public void Bus_SubscriptionWithFilter_CallFiltered() { ManualResetEvent ev = new ManualResetEvent(false); FilterImplementation implementation = new FilterImplementation(ev); using (IBus bus = new MessageBus.Core.RabbitMQBus()) { using (ISubscription subscription = bus.RegisterSubscription(implementation, c => c.SetReceiveSelfPublish())) { subscription.Open(); using (IPublisher publisher = bus.CreatePublisher()) { Person person = new Person {Id = 5}; BusMessage<Person> busMessage = new BusMessage<Person> { Data = person }; busMessage.Headers.Add(new BusHeader { Name = "Header", Value = "WrongValue" }); publisher.Send(busMessage); bool waitOne = ev.WaitOne(TimeSpan.FromSeconds(5)); waitOne.Should().BeFalse(); } } } }