Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            Logger.Factory = new ConsoleLoggerFactory();

            using (var container = new WindsorContainer())
            {
                var currentContainer = container;

                container.Register(
                    Classes.FromThisAssembly().BasedOn(typeof(IConsumer)).WithServiceAllInterfaces(),
                    Component.For <IBus>().UsingFactoryMethod(() =>
                {
                    var settings = new DepotSettings
                    {
                        ConsumerFactoryBuilder = () => new WindsorConsumerFactory(currentContainer)
                    };

                    settings.AddStartupConcern(new AutoRegisterConsumers(currentContainer));

                    return(Depot.Connect("localhost/castle_consumer", settings));
                }));

                var bus = container.Resolve <IBus>();

                bus.Publish(new PubSubMessage {
                    Body = "published!"
                });

                Console.WriteLine("Finished press any key to close");
                Console.ReadKey();
            }
        }
Ejemplo n.º 2
0
        public void ShouldCustomizePublisherWithContentType()
        {
            var settings = new DepotSettings();

            settings.MessageSerializers.Register(new MessagePackMessageSerializer());

            using (var bus = Depot.Connect("localhost/integration", settings))
            {
                HelloWorldMessage lastReceived = null;

                bus.Subscribe((HelloWorldMessage hwm) =>
                {
                    lastReceived = hwm;
                });

                using (var publisher = bus.CreatePublisher <HelloWorldMessage>(
                           p => p.SerializeWith("application/x-msgpack")))
                {
                    publisher.Publish(new HelloWorldMessage {
                        Message = "subscribe!"
                    });
                    WaitForDelivery();
                }

                Assert.That(lastReceived, Is.Not.Null);
                Assert.That(lastReceived.Message, Is.EqualTo("subscribe!"));
            }
        }
Ejemplo n.º 3
0
        public void ShouldReceivedPublishedMessage()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                var numReceived = 0;

                bus.Subscribe((HelloWorldMessage hwm) =>
                              Interlocked.Increment(ref numReceived),
                              o => o.SubscribeOn("queue-1", "queue-2")
                              .SetTopology(new SharedSubscriptionTopology()));

                using (var first = bus.CreatePublisher <HelloWorldMessage>(o => o.PublishOn("exchange-queue-1")))
                    using (var second = bus.CreatePublisher <HelloWorldMessage>(o => o.PublishOn("exchange-queue-2")))
                    {
                        for (var i = 0; i < 25; ++i)
                        {
                            first.Publish(new HelloWorldMessage {
                                Message = "subscribe!"
                            });
                            second.Publish(new HelloWorldMessage {
                                Message = "subscribe!"
                            });
                        }
                    }

                WaitForDelivery();

                Assert.That(numReceived, Is.EqualTo(50));
            }
        }
Ejemplo n.º 4
0
        public void ShouldCreateSubscriberWithTaskPoolStrategy()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                HelloWorldMessage lastReceived = null;
                var numReceived = 0;
                var handler     = new Action <HelloWorldMessage>(hwm => { lastReceived = hwm; ++numReceived; });

                using (var subscription = bus.Subscribe(handler, o => o.DeliverUsing <TaskDeliveryStrategy>()))
                {
                    for (var i = 0; i < 100; ++i)
                    {
                        bus.Publish(new HelloWorldMessage
                        {
                            Message = "subscribe!"
                        });
                    }

                    WaitForDelivery();

                    Assert.That(lastReceived, Is.Not.Null);
                    Assert.That(lastReceived.Message, Is.EqualTo("subscribe!"));
                    Assert.That(numReceived, Is.EqualTo(100));

                    Assert.That(subscription.State.Workers.Count(), Is.EqualTo(0));
                }
            }
        }
Ejemplo n.º 5
0
        public void ShouldPublishFromMultipleThreads()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                using (var publisher = bus.CreatePublisher <HelloWorldMessage>())
                {
                    var threads = Enumerable.Range(0, 10).Select(_ => new Thread(() =>
                    {
                        for (var i = 0; i < 100; ++i)
                        {
                            publisher.Publish(new HelloWorldMessage());
                        }
                    })).ToArray();

                    foreach (var thread in threads)
                    {
                        thread.Start();
                    }

                    foreach (var thread in threads)
                    {
                        thread.Join();
                    }

                    WaitForDelivery();

                    Assert.That(publisher.NumPublishedMessages, Is.EqualTo(1000));
                }
            }
        }
Ejemplo n.º 6
0
        public void ShouldShutdown()
        {
            var seen = 0;

            using (var bus = Depot.Connect("localhost/integration"))
            {
                var subscription = bus.Subscribe((HelloWorldMessage hwm) =>
                {
                    Console.WriteLine("!!! Starting message");
                    WaitForDelivery();
                    ++seen;
                    Console.WriteLine("!!! Finished message");
                }, o => o.DeliverUsing <WorkerPoolDeliveryStrategy>(s => s.NumWorkers = 1));

                bus.Publish(new HelloWorldMessage
                {
                    Message = "subscribe!"
                });

                WaitForDelivery();

                subscription.Dispose();
            }

            Assert.That(seen, Is.EqualTo(1));
            var count = admin.Messages(IntegrationVHost, new Queue("HelloWorldMessage")).Count();

            Assert.That(count, Is.EqualTo(0));
        }
Ejemplo n.º 7
0
        public void ShouldReceivedPublishedMessage()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                HelloWorldMessage lastReceived = null;
                var numReceived = 0;

                bus.Subscribe((HelloWorldMessage hwm) =>
                {
                    lastReceived = hwm;
                    ++numReceived;
                });

                for (var i = 0; i < 100; ++i)
                {
                    bus.Publish(new HelloWorldMessage {
                        Message = "subscribe!"
                    });
                }

                WaitForDelivery();

                Assert.That(lastReceived, Is.Not.Null);
                Assert.That(lastReceived.Message, Is.EqualTo("subscribe!"));
                Assert.That(numReceived, Is.EqualTo(100));
            }
        }
Ejemplo n.º 8
0
        public void ShouldHaveDefaultSerializer()
        {
            var settings = new DepotSettings
            {
                MessageSerializers =
                {
                    Default = new MessagePackMessageSerializer()
                }
            };

            using (var bus = Depot.Connect("localhost/integration", settings))
            {
                HelloWorldMessage lastReceived = null;

                bus.Subscribe((HelloWorldMessage hwm) =>
                {
                    lastReceived = hwm;
                });

                bus.Publish(new HelloWorldMessage {
                    Message = "subscribe!"
                });

                WaitForDelivery();

                Assert.That(lastReceived, Is.Not.Null);
                Assert.That(lastReceived.Message, Is.EqualTo("subscribe!"));
            }
        }
Ejemplo n.º 9
0
        public void ShouldBeAbleToPauseWorker()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                var seen = 0;

                var subscription = bus.Subscribe <HelloWorldMessage>(
                    m => Interlocked.Increment(ref seen),
                    c => c.DeliverUsing <WorkerPoolDeliveryStrategy>());

                // pause the worker
                var state  = subscription.State;
                var worker = state.Workers.Single();
                subscription.Workers.Pause(worker.Name);

                // publish a message to the queue
                bus.Publish(new HelloWorldMessage());
                WaitForDelivery();

                // we shouldn't have processed the message and the worker should be paused
                Assert.That(seen, Is.EqualTo(0));
                state = subscription.State;
                Assert.That(state.Workers.First().Status, Is.EqualTo(WorkerStatus.Paused));

                // resume the worker
                subscription.Workers.Resume(worker.Name);
                WaitForDelivery();
                Assert.That(seen, Is.EqualTo(1));
                state = subscription.State;
                Assert.That(state.Workers.First().Status, Is.Not.EqualTo(WorkerStatus.Paused));
            }
        }
Ejemplo n.º 10
0
        public void ShouldSubscribeToMessageInterfacesWithCustomMessageFactory()
        {
            var serializer = new JsonMessageSerializer(
                new CastleMessageTypeFactory());

            var depotSettings = new DepotSettings();

            depotSettings.MessageSerializers.Register(serializer);

            using (var bus = Depot.Connect("localhost/integration", depotSettings))
            {
                IHelloWorldMessage lastReceived = null;
                bus.Subscribe((IHelloWorldMessage hwm) =>
                {
                    lastReceived = hwm;
                });
                bus.Publish(new HelloWorldMessage {
                    Message = "subscribe!"
                });

                WaitForDelivery();

                Assert.That(lastReceived, Is.Not.Null);
                Assert.That(lastReceived.Message, Is.EqualTo("subscribe!"));

                Assert.That(admin.Exists(IntegrationVHost, new Queue("HelloWorldMessage")), "did not create queue");
                Assert.That(admin.Exists(IntegrationVHost, new Exchange("HelloWorldMessage")), "did not create exchange");
            }
        }
Ejemplo n.º 11
0
 public void ShouldCreateSubscriptionWithName()
 {
     using (var bus = Depot.Connect("localhost/integration"))
     {
         var subscription = bus.Subscribe((HelloWorldMessage m) => { }, o => o.WithName("fribble"));
         Assert.That(subscription.Name, Is.EqualTo("fribble"));
     }
 }
Ejemplo n.º 12
0
        public void ShouldPublishMessageOnDefaultPublisher()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                bus.Publish(new HelloWorldMessage());
            }

            Assert.That(admin.Exchanges(IntegrationVHost).Any(e => e.Name == "HelloWorldMessage"));
        }
Ejemplo n.º 13
0
        public void ShouldSubscribeMessage()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                bus.Subscribe((HelloWorldMessage hwm) => { });

                Assert.That(admin.Exists(IntegrationVHost, new Queue("HelloWorldMessage")), "did not create queue");
                Assert.That(admin.Exists(IntegrationVHost, new Exchange("HelloWorldMessage")), "did not create exchange");
            }
        }
Ejemplo n.º 14
0
        public void ShouldCreateSubscriberWithSpecificSubscriptionQueue()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                var subscription = bus.Subscribe((HelloWorldMessage m) => { }, o => o.SubscribeOn("gimme-dem-messages"));

                var queueName = subscription.Queues.Single().Name;
                Assert.That(queueName, Is.EqualTo("gimme-dem-messages"));
            }
        }
Ejemplo n.º 15
0
        public void ShouldFindSubscriptionByName()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                var existing = bus.Subscribe((HelloWorldMessage hwm) => { });

                var subscription = bus.FindSubscription(existing.Name);

                Assert.That(subscription, Is.Not.Null);
            }
        }
Ejemplo n.º 16
0
        public void ShouldNotStartSubscriptionIfSubscriptionConfigurationIsInvalidAndThrowsIfStarted()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                var subscription = bus.Subscribe(
                    (HelloWorldMessage m) => { },
                    o => o.DeliverUsing <WorkerPoolDeliveryStrategy>(s => s.NumWorkers = 0));

                Assert.That(subscription.IsStarted, Is.False);
                Assert.Throws <ChinchillaException>(subscription.Start);
            }
        }
Ejemplo n.º 17
0
        public void ShouldCreatePublisherWithCustomPublishExchange()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                using (var publisher = bus.CreatePublisher <HelloWorldMessage>(o => o.PublishOn("custom-exchange-name")))
                {
                    publisher.Publish(new HelloWorldMessage());
                    Assert.That(publisher.NumPublishedMessages, Is.EqualTo(1));
                }
            }

            Assert.That(admin.Exchanges(IntegrationVHost).Any(e => e.Name == "custom-exchange-name"));
        }
Ejemplo n.º 18
0
        public void ShouldHaveCustomPublisherFaultStrategy()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                using (var publisher = bus.CreatePublisher <HelloWorldMessage>(
                           o => o.Confirm(true).OnFailure <RetryOnFailures>()))
                {
                    // TODO: Find a way to make this nack, so we can test the fault strategy
                    publisher.Publish(new HelloWorldMessage());

                    WaitForDelivery();
                }
            }
        }
Ejemplo n.º 19
0
        public void ShouldWaitForAllMessagesToBeConfirmedWhenDisposing()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                var publisher = bus.CreatePublisher <HelloWorldMessage>(p => p.Confirm(true));

                var receipts = Enumerable.Range(0, 100)
                               .Select(_ => publisher.Publish(new HelloWorldMessage()))
                               .ToArray();

                publisher.Dispose();

                Assert.That(receipts.All(r => r.IsConfirmed));
            }
        }
Ejemplo n.º 20
0
        public void ShouldCreatePublisherWithoutConfirms()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                var publisher = bus.CreatePublisher <HelloWorldMessage>(p => p.Confirm(false));

                var receipts = Enumerable.Range(0, 100)
                               .Select(_ => publisher.Publish(new HelloWorldMessage()))
                               .ToArray();

                publisher.Dispose();

                Assert.That(receipts.All(r => r.Status == PublishStatus.None));
            }
        }
Ejemplo n.º 21
0
        public void ShouldPublishMultipleMessages()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                using (var publisher = bus.CreatePublisher <HelloWorldMessage>())
                {
                    for (var i = 0; i < 100; ++i)
                    {
                        publisher.Publish(new HelloWorldMessage());
                    }

                    Assert.That(publisher.NumPublishedMessages, Is.EqualTo(100));
                }
            }
        }
Ejemplo n.º 22
0
        public static void Main(string[] args)
        {
            Logger.Factory = new ConsoleLoggerFactory();

            using (var depot = Depot.Connect("localhost/samplepubsub"))
            {
                depot.Subscribe <PubSubMessage>(m => Console.WriteLine(m.Body), o => o.SubscribeOn("subscriber-1"));
                depot.Subscribe <PubSubMessage>(m => Console.WriteLine(m.Body), o => o.SubscribeOn("subscriber-2"));

                depot.Publish(new PubSubMessage {
                    Body = "published!"
                });
            }

            Console.WriteLine("Finished press any key to close");
            Console.ReadKey();
        }
Ejemplo n.º 23
0
        public void ShouldPublishWithCustomRouter()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                var publisher = bus.CreatePublisher <HelloWorldMessage>(o => o.RouteWith <CustomRouter>());

                using (publisher)
                {
                    for (var i = 0; i < 100; ++i)
                    {
                        publisher.Publish(new HelloWorldMessage());
                    }

                    Assert.That(publisher.NumPublishedMessages, Is.EqualTo(100));
                }
            }
        }
Ejemplo n.º 24
0
        public void ShouldCreateRequestResponseOnBus()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                bus.Subscribe(new CapitalizeMessageConsumer());

                CapitalizedMessage capitalized = null;
                bus.Request <CapitalizeMessage, CapitalizedMessage>(
                    new CapitalizeMessage("where am i?"),
                    response => { capitalized = response; });

                WaitForDelivery();

                Assert.That(capitalized, Is.Not.Null);
                Assert.That(capitalized.Result, Is.EqualTo("WHERE AM I?"));
            }
        }
Ejemplo n.º 25
0
        public void ShouldHandleExceptionsWithDefaultExceptionHandler()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                bus.Subscribe((HelloWorldMessage hwm) => { throw new Exception("ERMAGHERD, EXPLODE!!11"); });
                bus.Publish(new HelloWorldMessage {
                    Message = "subscribe!"
                });

                WaitForDelivery();

                Assert.That(admin.Exists(IntegrationVHost, new Queue("ErrorQueue")));
                Assert.That(admin.Exists(IntegrationVHost, new Exchange("ErrorExchange")));

                var messages = admin.Messages(IntegrationVHost, new Queue("ErrorQueue"));
                Assert.That(messages.Count(), Is.EqualTo(1));
            }
        }
Ejemplo n.º 26
0
        public void ShouldRunAdditionalRegisteredListeners()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                var didRunAfterAccept = false;

                bus.Subscribe <HelloWorldMessage>((hwm, ctx) =>
                                                  ctx.Delivery.RegisterDeliveryListener(new ActionDeliveryListener(() => didRunAfterAccept = true)));

                bus.Publish(new HelloWorldMessage {
                    Message = "subscribe!"
                });

                WaitForDelivery();

                Assert.That(didRunAfterAccept, Is.True);
            }
        }
Ejemplo n.º 27
0
        public void ShouldSupportAsyncForBasicRequestResponse()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                bus.Subscribe(new CapitalizeMessageConsumer());

                var response = bus.RequestAsync <CapitalizeMessage, CapitalizedMessage>(
                    new CapitalizeMessage("where am i?"));

                response.Wait();

                var capitalized = response.Result;

                WaitForDelivery();

                Assert.That(capitalized, Is.Not.Null);
                Assert.That(capitalized.Result, Is.EqualTo("WHERE AM I?"));
            }
        }
Ejemplo n.º 28
0
        public static void Main(string[] args)
        {
            var running = true;

            Logger.Factory = new ConsoleLoggerFactory();

            var publisher = new Thread(() =>
            {
                using (var bus = Depot.Connect("localhost"))
                {
                    while (running)
                    {
                        bus.Publish(new HelloWorldMessage
                        {
                            Message = "Good morning"
                        });

                        Thread.Sleep(1000);
                    }
                }
            });

            publisher.Start();

            //var consumer = new Thread(() =>
            //{
            //    using (var bus = Depot.Connect("localhost"))
            //    {
            //        bus.Subscribe<HelloWorldMessage>(m => System.Console.WriteLine(m.Message));

            //        while (running)
            //        {
            //            Thread.Sleep(1000);
            //        }
            //    }
            //});
            //consumer.Start();

            System.Console.WriteLine("Waiting for you!");
            System.Console.ReadLine();

            running = false;
        }
Ejemplo n.º 29
0
        public void ShouldSubscribeWithConsumerWithCustomConfiguration()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                using (bus.Subscribe <CustomConfigurationConsumer>())
                {
                    for (var i = 0; i < 1; ++i)
                    {
                        bus.Publish(new HelloWorldMessage {
                            Message = "subscribe!"
                        });
                    }

                    WaitForDelivery();

                    Assert.That(admin.Exists(IntegrationVHost, new Queue("custom-subscription-endpoint")), "did not create queue");
                }
            }
        }
Ejemplo n.º 30
0
        public void ShouldAllowConfigurationOfCustomDeliveryFailureStrategies()
        {
            using (var bus = Depot.Connect("localhost/integration"))
            {
                var numExceptions = 0;

                bus.Subscribe(
                    (HelloWorldMessage hwm) => { throw new Exception("ERMAGHERD, EXPLODE!!11"); },
                    opt => opt.OnFailure <CustomSubscriptionFailureStrategy>(s => s.OnException(() => ++ numExceptions)));

                bus.Publish(new HelloWorldMessage {
                    Message = "subscribe!"
                });

                WaitForDelivery();

                Assert.That(numExceptions, Is.EqualTo(1));
            }
        }