Exemplo n.º 1
0
        public void Start()
        {
            if (_bus != null)
            {
                return; //Multiple start.
            }

            _bus = _rebusConfigurer.Start();

            //now register explicit subscriptions.
            foreach (var subscription in _configuration.ExplicitSubscriptions)
            {
                var type = Type.GetType(subscription.MessageType);
                //var subscribeMessage = new SubscribeRequest()
                //{
                //	Topic = type.GetSimpleAssemblyQualifiedName(),
                //	SubscriberAddress = _configuration.TransportAddress,
                //};
                //Dictionary<String, String> headers = new Dictionary<string, string>();
                //headers[Headers.Intent] = Headers.IntentOptions.PointToPoint;
                //_bus.Advanced.Routing.Send(subscription.Endpoint, subscribeMessage, headers);
                _bus.Advanced.Routing.Subscribe(
                    subscription.Endpoint,
                    type,
                    _configuration.TransportAddress).Wait();
            }
        }
Exemplo n.º 2
0
        public async Task ItWorks()
        {
            _busStartTime = DateTime.UtcNow;
            _rebusConfigurer.Start();

            await Task.Delay(TimeSpan.FromSeconds(5));

            _activator.Dispose();

            Console.WriteLine(@"Receive attempts:
{0}

Diffs:
{1}", string.Join(Environment.NewLine, _waitedSeconds),
                              string.Join(Environment.NewLine, GetDiffs(_waitedSeconds)));
        }
        void IInitializable <IWindsorContainer> .Initialized(IWindsorContainer container)
        {
            container.Register(
                Component.For <IBus>()
                .UsingFactoryMethod(() =>
            {
                RebusConfigurer rebus =
                    Configure.With(new CastleWindsorContainerAdapter(container));

                if (BusConfiguration != null)
                {
                    rebus = BusConfiguration(rebus, container.Kernel);
                }

                // This will start Rebus - allowing one single instance of Rebus for this application.
                return(rebus.Start());
            }));
        }
Exemplo n.º 4
0
        public async Task CanDeferMessage()
        {
            var gotTheMessage = new ManualResetEvent(false);

            var receiveTime = DateTime.MaxValue;

            _activator.Handle <string>(async str =>
            {
                receiveTime = DateTime.UtcNow;
                gotTheMessage.Set();
            });

            var bus      = _configurer.Start();
            var sendTime = DateTime.UtcNow;

            await bus.Defer(TimeSpan.FromSeconds(10), "hej med dig!");

            gotTheMessage.WaitOrDie(TimeSpan.FromSeconds(20));

            var elapsed = receiveTime - sendTime;

            Assert.That(elapsed, Is.GreaterThan(TimeSpan.FromSeconds(8)));
        }
 public void Start()
 {
     _bus = _configurer.Start();
     _logger.Debug("Bus started");
 }
Exemplo n.º 6
0
        /// <summary>
        ///     Adds all bus services allowing the configuration through <paramref name="configurerAction" />
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configurerAction"></param>
        /// <returns></returns>
        public static IServiceCollection AddServiceBus(this IServiceCollection services, Action <ICqrsConfigurer> configurerAction)
        {
            var configurer = GetCqrsConfigurer(services);

            configurerAction(configurer);

            // Add default network and store inmemory implementation, if not provided
            services.TryAddSingleton <InMemNetwork>();
            services.TryAddSingleton <InMemorySubscriberStore>();
            services.TryAddSingleton <MessagesWaiter>();
            services.TryAddSingleton <IMessagesWaiter>(p => p.GetRequiredService <MessagesWaiter>());
            services.TryAddScoped <IMessagesCatcher, MessagesCatcher>();
            services.TryAddSingleton(c => (IHandleMessages <IMessage>)c.GetRequiredService <MessagesWaiter>());

            services.AddRebus(r => r);
            services.RemoveAll <NetCoreServiceProviderContainerAdapter>();
            services.RemoveAll <IBus>();

            services.AddSingleton <NetCoreServiceProviderContainerAdapterEx>();
            services.AddSingleton(provider =>
            {
                RebusConfigurer c = Configure.With(provider.GetRequiredService <NetCoreServiceProviderContainerAdapterEx>());
                ConfigureRebus(c, provider);
                return(c.Start());
            });

            services.AddSingleton <IHostedService, RebusHostedService>(r => new RebusHostedService(r, configurer));

            return(services);

            RebusConfigurer ConfigureRebus(RebusConfigurer configure, IServiceProvider provider)
            {
                var options = provider.GetRequiredService <IOptions <CqrsOptions> >();

                configurer.ServiceProvider = provider;
                var serilog = provider.GetService <ILogger>();

                return(configure.Serialization(s => s.UseNewtonsoftJson(GetJsonSettings()))
                       .Options(o =>
                {
                    o.SetNumberOfWorkers(options.Value.ServiceBusWorkers);
                    o.SetMaxParallelism(options.Value.ServiceBusWorkers);
                    o.ApplyServiceProvider(provider);
                    o.AutoSetMessageId();
                    o.CatchMessagesSent();
                    o.HandleCommandsEvents(provider);
                    o.SimpleRetryStrategy(maxDeliveryAttempts: 2, secondLevelRetriesEnabled: true);
                })
                       .Timeouts(configurer.ApplyActions)
                       .Sagas(configurer.ApplyActions)
                       .Logging(l =>
                {
                    if (serilog != null)
                    {
                        l.Serilog(serilog);
                    }
                })
                       .Subscriptions(configurer.ApplyActions)
                       .Transport(configurer.ApplyActions)
                       .Routing(configurer.ApplyActions));
            }
        }