Ejemplo n.º 1
0
        public IMessageProducer <TMessage> GetProducer <TMessage>(IReloadingManager <RabbitConnectionSettings> settings,
                                                                  bool isDurable, IRabbitMqSerializer <TMessage> serializer)
        {
            // on-the fly connection strings switch is not supported currently for rabbitMq
            var currSettings         = settings.CurrentValue;
            var subscriptionSettings = new RabbitMqSubscriptionSettings
            {
                ConnectionString = currSettings.ConnectionString,
                ExchangeName     = currSettings.ExchangeName,
                IsDurable        = isDurable,
            };

            return((IMessageProducer <TMessage>)_producers.GetOrAdd(subscriptionSettings, CreateProducer));

            IStopable CreateProducer(RabbitMqSubscriptionSettings s)
            {
                var publisher = new RabbitMqPublisher <TMessage>(s);

                if (isDurable && _queueRepository.Value != null)
                {
                    publisher.SetQueueRepository(_queueRepository.Value);
                }
                else
                {
                    publisher.DisableInMemoryQueuePersistence();
                }

                return(publisher
                       .SetSerializer(serializer)
                       .SetLogger(_logger)
                       .Start());
            }
        }
Ejemplo n.º 2
0
    public RabbitMqDistributedEventBus(
        IOptions <AbpRabbitMqEventBusOptions> options,
        IConnectionPool connectionPool,
        IRabbitMqSerializer serializer,
        IServiceScopeFactory serviceScopeFactory,
        IOptions <AbpDistributedEventBusOptions> distributedEventBusOptions,
        IRabbitMqMessageConsumerFactory messageConsumerFactory,
        ICurrentTenant currentTenant,
        IUnitOfWorkManager unitOfWorkManager,
        IGuidGenerator guidGenerator,
        IClock clock,
        IEventHandlerInvoker eventHandlerInvoker)
        : base(
            serviceScopeFactory,
            currentTenant,
            unitOfWorkManager,
            distributedEventBusOptions,
            guidGenerator,
            clock,
            eventHandlerInvoker)
    {
        ConnectionPool             = connectionPool;
        Serializer                 = serializer;
        MessageConsumerFactory     = messageConsumerFactory;
        AbpRabbitMqEventBusOptions = options.Value;

        HandlerFactories = new ConcurrentDictionary <Type, List <IEventHandlerFactory> >();
        EventTypes       = new ConcurrentDictionary <string, Type>();
    }
        public RabbitMQSubscribeMiddleware(
            IChannelPool channelPool,
            IRabbitMqSerializer serializer)
        {
            this.serializer  = serializer;
            this.channelPool = channelPool;

            ChannelAccessor = channelPool.Acquire(ChannelQueueName, ConnectionName);


            QueueDeclareConfiguration QueueConfiguration = new QueueDeclareConfiguration(ChannelQueueName);

            QueueConfiguration.Declare(ChannelAccessor.Channel);

            var properties = ChannelAccessor.Channel.CreateBasicProperties();

            properties.Persistent = true;



            Consumer           = new EventingBasicConsumer(ChannelAccessor.Channel);
            Consumer.Received += (model, ea) =>
            {
                var    body    = ea.Body.ToArray();
                string message = (string)serializer.Deserialize(ea.Body.ToArray(), typeof(string));
                Console.WriteLine(" [Subscribe] Received {0}", string.Format(message, DateTime.Now));
                ChannelAccessor.Channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
            };

            ChannelAccessor.Channel.BasicConsume(
                queue: QueueConfiguration.QueueName,
                autoAck: false,
                consumer: Consumer
                );
        }
Ejemplo n.º 4
0
        public RabbitMqDistributedEventBus(
            IConnectionPool connectionPool,
            IConsumerFactory consumerFactory,
            IRabbitMqSerializer serializer,
            IEventBusSubscriptionsManager subscriptionsManager,
            IServiceProvider serviceProvider,
            IOptions <EventBusOptions> eventBusOptions,
            IOptions <RabbitMqOptions> rabbitMOptions)
            : base(subscriptionsManager, serviceProvider, eventBusOptions)
        {
            RabbitMqOptions              = rabbitMOptions.Value;
            ConnectionPool               = connectionPool;
            ConsumerFactory              = consumerFactory;
            Serializer                   = serializer;
            _subsManager.OnEventRemoved += SubsManager_OnEventRemoved;

            Consumer = ConsumerFactory.Create(
                new ExchangeDeclareConfiguration(
                    RabbitMqOptions.Exchange,
                    type: "direct",
                    durable: true),
                new QueueDeclareConfiguration(
                    RabbitMqOptions.QueueName,
                    durable: true,
                    exclusive: false,
                    autoDelete: false),
                RabbitMqOptions.ConnectionName
                );
            Consumer.OnMessageReceived(ProcessEventAsync);
        }
Ejemplo n.º 5
0
        public RabbitMqPublisher <TMessage> Create <TMessage>(
            IRabbitMqSerializer <TMessage> serializer,
            string connectionString,
            string @namespace,
            string endpoint)
        {
            try
            {
                var settings = RabbitMqSubscriptionSettings
                               .CreateForPublisher(connectionString, @namespace, endpoint)
                               .MakeDurable();

                return(new RabbitMqPublisher <TMessage>(settings)
                       .SetSerializer(serializer)
                       .SetPublishStrategy(new DefaultFanoutPublishStrategy(settings))
                       .PublishSynchronously()
                       .SetLogger(_log)
                       .Start());
            }
            catch (Exception ex)
            {
                _log.WriteErrorAsync(nameof(RabbitMqPublishersFactory), nameof(Create), null, ex).Wait();
                throw;
            }
        }
        public RabbitMqPublisher <TMessageModel> SetSerializer(IRabbitMqSerializer <TMessageModel> serializer)
        {
            ThrowIfStarted();

            _serializer = serializer;
            return(this);
        }
Ejemplo n.º 7
0
        public RabbitMqPublisher <TMessage> GetProducer <TMessage>(RabbitConnectionSettings settings,
                                                                   bool isDurable, IRabbitMqSerializer <TMessage> serializer)
        {
            var subscriptionSettings = new RabbitMqSubscriptionSettings
            {
                ConnectionString = settings.ConnectionString,
                ExchangeName     = settings.ExchangeName,
                IsDurable        = isDurable,
            };

            return((RabbitMqPublisher <TMessage>)_producers.GetOrAdd(subscriptionSettings, CreateProducer).Value);

            Lazy <IStopable> CreateProducer(RabbitMqSubscriptionSettings s)
            {
                // Lazy ensures RabbitMqPublisher will be created and started only once
                // https://andrewlock.net/making-getoradd-on-concurrentdictionary-thread-safe-using-lazy/
                return(new Lazy <IStopable>(() => new RabbitMqPublisher <TMessage>(s)
                                            .DisableInMemoryQueuePersistence()
                                            .SetSerializer(serializer)
                                            .SetPublishStrategy(new TopicPublishStrategy())
                                            .SetLogger(_logger)
                                            .PublishSynchronously()
                                            .Start()));
            }
        }
Ejemplo n.º 8
0
 public ContextProvider(IRabbitMqSerializer serializer, RabbitMqOptions options)
 {
     _serializer = serializer;
     HeaderName  = string.IsNullOrWhiteSpace(options.Context?.Header)
         ? "message_context"
         : options.Context.Header;
 }
 public RabbitMQPublishMiddleware(
     IChannelPool channelPool,
     IRabbitMqSerializer serializer)
 {
     this.serializer  = serializer;
     this.channelPool = channelPool;
     ChannelAccessor  = channelPool.Acquire(ChannelQueueName, ConnectionName);
 }
Ejemplo n.º 10
0
 public RabbitMqClient(IConnection connection, IRabbitMqSerializer serializer, IContextProvider contextProvider,
                       RabbitMqOptions options, ILogger <RabbitMqClient> logger)
 {
     _serializer      = serializer;
     _contextProvider = contextProvider;
     _options         = options;
     _logger          = logger;
     _channel         = connection.CreateModel();
 }
Ejemplo n.º 11
0
 public RabbitMqDistributedEventBus(
     IOptions <RabbitMqDistributedEventBusOptions> options,
     IChannelPool channelPool,
     IRabbitMqSerializer serializer)
 {
     ChannelPool = channelPool;
     Serializer  = serializer;
     Options     = options.Value;
 }
 public RabbitMqClient(IConnection connection, IContextProvider contextProvider, IRabbitMqSerializer serializer,
                       RabbitMqOptions options, ILogger <RabbitMqClient> logger)
 {
     _contextProvider = contextProvider;
     _serializer      = serializer;
     _logger          = logger;
     _contextEnabled  = options.Context?.Enabled == true;
     _channel         = connection.CreateModel();
     _loggerEnabled   = options.Logger?.Enabled ?? false;
 }
 public RabbitBasicDemo(
     IChannelPool channelPool,
     IRabbitMqSerializer serializer,
     ILogger <RabbitBasicDemo> logger)
 {
     _logger          = logger;
     this.serializer  = serializer;
     this.channelPool = channelPool;
     ChannelAccessor  = channelPool.Acquire(ChannelQueueName, ConnectionName);
 }
 public MyRabbitMqDistributedEventBus(
     IOptions <AbpRabbitMqEventBusOptions> options,
     IConnectionPool connectionPool,
     IRabbitMqSerializer serializer,
     IServiceScopeFactory serviceScopeFactory,
     IOptions <AbpDistributedEventBusOptions> distributedEventBusOptions,
     IRabbitMqMessageConsumerFactory messageConsumerFactory) :
     base(options, connectionPool, serializer, serviceScopeFactory, distributedEventBusOptions, messageConsumerFactory)
 {
 }
Ejemplo n.º 15
0
 public RabbitMqClient(IConnection connection, IContextProvider contextProvider, IRabbitMqSerializer serializer,
                       RabbitMqOptions options, ILogger <RabbitMqClient> logger)
 {
     _connection        = connection;
     _contextProvider   = contextProvider;
     _serializer        = serializer;
     _logger            = logger;
     _contextEnabled    = options.Context?.Enabled == true;
     _loggerEnabled     = options.Logger?.Enabled ?? false;
     _spanContextHeader = options.GetSpanContextHeader();
 }
Ejemplo n.º 16
0
 public RabbitMqClient(IConnection connection, IContextProvider contextProvider, IRabbitMqSerializer serializer,
                       RabbitMqOptions options, ILogger <RabbitMqClient> logger)
 {
     _connection        = connection;
     _contextProvider   = contextProvider;
     _serializer        = serializer;
     _logger            = logger;
     _contextEnabled    = options.Context?.Enabled == true;
     _loggerEnabled     = options.Logger?.Enabled ?? false;
     _spanContextHeader = options.GetSpanContextHeader();
     _persistMessages   = options?.MessagesPersisted ?? false;
     _maxChannels       = options.MaxProducerChannels <= 0 ? 1000 : options.MaxProducerChannels;
 }
Ejemplo n.º 17
0
 public RabbitMqClient(IConnection connection, IContextProvider contextProvider, IRabbitMqSerializer serializer,
                       RabbitMqOptions options, ILogger <RabbitMqClient> logger)
 {
     _contextProvider   = contextProvider;
     _serializer        = serializer;
     _logger            = logger;
     _contextEnabled    = options.Context?.Enabled == true;
     _channel           = connection.CreateModel();
     _loggerEnabled     = options.Logger?.Enabled ?? false;
     _spanContextHeader = string.IsNullOrWhiteSpace(options.SpanContextHeader)
         ? "span_context"
         : options.SpanContextHeader;
 }
Ejemplo n.º 18
0
 protected BrokerApplicationBase(RabbitMqCorrelationManager correlationManager, ILoggerFactory loggerFactory, ILog logger, ISlackNotificationsSender slackNotificationsSender,
                                 CurrentApplicationInfo applicationInfo, MessageFormat messageFormat = MessageFormat.Json)
 {
     _correlationManager = correlationManager;
     LoggerFactory       = loggerFactory;
     Logger = logger;
     _slackNotificationsSender = slackNotificationsSender;
     ApplicationInfo           = applicationInfo;
     MessageDeserializer       = messageFormat == MessageFormat.Json
         ? new JsonMessageDeserializer <TMessage>()
         : (IMessageDeserializer <TMessage>) new MessagePackMessageDeserializer <TMessage>();
     MessageSerializer = messageFormat == MessageFormat.Json
         ? new JsonMessageSerializer <TMessage>()
         : (IRabbitMqSerializer <TMessage>) new MessagePackMessageSerializer <TMessage>();
 }
        public RabbitMQEventBus(
            IOptions <RabbitMqEventBusOptions> options,
            IConnectionPool connectionPool,
            IRabbitMqSerializer serializer,
            IRabbitMqMessageConsumerFactory messageConsumerFactory)
            : base()
        {
            ConnectionPool          = connectionPool;
            Serializer              = serializer;
            MessageConsumerFactory  = messageConsumerFactory;
            RabbitMqEventBusOptions = options.Value;

            HandlerFactories = new ConcurrentDictionary <Type, List <IEventHandlerFactory> >();
            EventTypes       = new ConcurrentDictionary <string, Type>();
            Initialize();
        }
Ejemplo n.º 20
0
        private void AddRabbitPublisher <T>(ContainerBuilder builder,
                                            RabbitPublisherSettings settings,
                                            IRabbitMqPublishStrategy rabbitMqPublishStrategy = null,
                                            IRabbitMqSerializer <T> serializer = null)
        {
            rabbitMqPublishStrategy = rabbitMqPublishStrategy ?? new DefaultFanoutPublishStrategy(settings);
            serializer = serializer ?? new JsonMessageSerializer <T>();

            builder.RegisterInstance(
                new RabbitMqPublisher <T>(settings)
                .SetSerializer(serializer)
                .SetLogger(_log)
                .SetPublishStrategy(rabbitMqPublishStrategy)
                .DisableInMemoryQueuePersistence())
            .As <IMessageProducer <T> >()
            .As <IStartStop>();
        }
 public RabbitMQBusComMediator(
     IMediator mediator,
     IOptions <AbpRabbitMqEventBusOptions> options,
     IConnectionPool connectionPool,
     IRabbitMqSerializer serializer,
     IServiceScopeFactory serviceScopeFactory,
     IOptions <AbpDistributedEventBusOptions> distributedEventBusOptions,
     IRabbitMqMessageConsumerFactory messageConsumerFactory)
     :   base(options,
              connectionPool,
              serializer,
              serviceScopeFactory,
              distributedEventBusOptions,
              messageConsumerFactory)
 {
     _mediator = mediator;
 }
Ejemplo n.º 22
0
        public RabbitMqDistributedEventBus(
            IOptions <RocketRabbitMqEventBusOptions> options,
            IConnectionPool connectionPool,
            IRabbitMqSerializer serializer,
            IServiceScopeFactory serviceScopeFactory,
            IOptions <RocketDistributedEventBusOptions> distributedEventBusOptions,
            IRabbitMqMessageConsumerFactory messageConsumerFactory) : base(serviceScopeFactory)
        {
            ConnectionPool                   = connectionPool;
            Serializer                       = serializer;
            MessageConsumerFactory           = messageConsumerFactory;
            RocketDistributedEventBusOptions = distributedEventBusOptions.Value;
            RocketRabbitMqEventBusOptions    = options.Value;

            HandlerFactories = new ConcurrentDictionary <Type, List <IEventHandlerFactory> > ();
            EventTypes       = new ConcurrentDictionary <string, Type> ();
        }
Ejemplo n.º 23
0
 public RabbitMqSubscriber(IServiceProvider serviceProvider)
 {
     _serviceProvider     = serviceProvider;
     _connection          = _serviceProvider.GetRequiredService <IConnection>();
     _channel             = _connection.CreateModel();
     _publisher           = _serviceProvider.GetRequiredService <IBusPublisher>();
     _rabbitMqSerializer  = _serviceProvider.GetRequiredService <IRabbitMqSerializer>();
     _conventionsProvider = _serviceProvider.GetRequiredService <IConventionsProvider>();
     _contextProvider     = _serviceProvider.GetRequiredService <IContextProvider>();
     _logger = _serviceProvider.GetService <ILogger <RabbitMqSubscriber> >();
     _exceptionToMessageMapper = _serviceProvider.GetService <IExceptionToMessageMapper>() ??
                                 new EmptyExceptionToMessageMapper();
     _pluginsExecutor = _serviceProvider.GetService <IRabbitMqPluginsExecutor>();
     _options         = _serviceProvider.GetService <RabbitMqOptions>();
     _loggerEnabled   = _options.Logger?.Enabled ?? false;
     _retries         = _options.Retries >= 0 ? _options.Retries : 3;
     _retryInterval   = _options.RetryInterval > 0 ? _options.RetryInterval : 2;
 }
Ejemplo n.º 24
0
        public JobQueue(
            IOptions <BackgroundJobOptions> backgroundJobOptions,
            IOptions <RabbitMqBackgroundJobOptions> rabbitMqBackgroundJobOptions,
            IChannelPool channelPool,
            IRabbitMqSerializer serializer,
            IBackgroundJobExecuter jobExecuter)
        {
            BackgroundJobOptions         = backgroundJobOptions.Value;
            RabbitMqBackgroundJobOptions = rabbitMqBackgroundJobOptions.Value;
            Serializer  = serializer;
            JobExecuter = jobExecuter;
            ChannelPool = channelPool;

            JobConfiguration   = BackgroundJobOptions.GetJob(typeof(TArgs));
            QueueConfiguration = GetOrCreateJobQueueConfiguration();

            Logger = NullLogger <JobQueue <TArgs> > .Instance;
        }
Ejemplo n.º 25
0
        public RabbitMqDistributedEventBus(
            IOptions <RabbitMqDistributedEventBusOptions> options,
            IConnectionPool connectionPool,
            IRabbitMqSerializer serializer,
            IServiceProvider serviceProvider,
            IOptions <DistributedEventBusOptions> distributedEventBusOptions)
        {
            ConnectionPool                     = connectionPool;
            Serializer                         = serializer;
            ServiceProvider                    = serviceProvider;
            DistributedEventBusOptions         = distributedEventBusOptions.Value;
            RabbitMqDistributedEventBusOptions = options.Value;

            HandlerFactories = new ConcurrentDictionary <Type, List <IEventHandlerFactory> >();
            EventTypes       = new ConcurrentDictionary <string, Type>();

            ConsumerChannel = CreateConsumerChannel();
            Subscribe(DistributedEventBusOptions.Handlers);
        }
 public RabbitMqSubscriber(IApplicationBuilder app)
 {
     _serviceProvider     = app.ApplicationServices.GetRequiredService <IServiceProvider>();
     _connection          = app.ApplicationServices.GetRequiredService <IConnection>();
     _channel             = _connection.CreateModel();
     _publisher           = app.ApplicationServices.GetRequiredService <IBusPublisher>();
     _rabbitMqSerializer  = app.ApplicationServices.GetRequiredService <IRabbitMqSerializer>();;
     _conventionsProvider = app.ApplicationServices.GetRequiredService <IConventionsProvider>();;
     _contextProvider     = app.ApplicationServices.GetRequiredService <IContextProvider>();
     _logger = app.ApplicationServices.GetService <ILogger <RabbitMqSubscriber> >();
     _exceptionToMessageMapper = _serviceProvider.GetService <IExceptionToMessageMapper>() ??
                                 new EmptyExceptionToMessageMapper();
     _middlewares    = _serviceProvider.GetServices <IRabbitMqMiddleware>();
     _hasMiddlewares = _middlewares.Any();
     _options        = _serviceProvider.GetService <RabbitMqOptions>();
     _loggerEnabled  = _options.Logger?.Enabled ?? false;
     _retries        = _options.Retries >= 0 ? _options.Retries : 3;
     _retryInterval  = _options.RetryInterval > 0 ? _options.RetryInterval : 2;
 }
Ejemplo n.º 27
0
Archivo: JobQueue.cs Proyecto: zxbe/abp
        public JobQueue(
            IOptions <AbpBackgroundJobOptions> backgroundJobOptions,
            IOptions <AbpRabbitMqBackgroundJobOptions> rabbitMqAbpBackgroundJobOptions,
            IChannelPool channelPool,
            IRabbitMqSerializer serializer,
            IBackgroundJobExecuter jobExecuter,
            IServiceScopeFactory serviceScopeFactory)
        {
            AbpBackgroundJobOptions         = backgroundJobOptions.Value;
            AbpRabbitMqBackgroundJobOptions = rabbitMqAbpBackgroundJobOptions.Value;
            Serializer          = serializer;
            JobExecuter         = jobExecuter;
            ServiceScopeFactory = serviceScopeFactory;
            ChannelPool         = channelPool;

            JobConfiguration   = AbpBackgroundJobOptions.GetJob(typeof(TArgs));
            QueueConfiguration = GetOrCreateJobQueueConfiguration();

            Logger = NullLogger <JobQueue <TArgs> > .Instance;
        }
Ejemplo n.º 28
0
 public RabbitMqSubscriber(IServiceProvider serviceProvider)
 {
     _serviceProvider     = serviceProvider;
     _channel             = _serviceProvider.GetRequiredService <IConnection>().CreateModel();
     _publisher           = _serviceProvider.GetRequiredService <IBusPublisher>();
     _rabbitMqSerializer  = _serviceProvider.GetRequiredService <IRabbitMqSerializer>();
     _conventionsProvider = _serviceProvider.GetRequiredService <IConventionsProvider>();
     _contextProvider     = _serviceProvider.GetRequiredService <IContextProvider>();
     _logger                   = _serviceProvider.GetService <ILogger <RabbitMqSubscriber> >();
     _options                  = _serviceProvider.GetService <RabbitMqOptions>();
     _pluginsExecutor          = _serviceProvider.GetService <IRabbitMqPluginsExecutor>();
     _exceptionToMessageMapper = _serviceProvider.GetService <IExceptionToMessageMapper>();
     _retries                  = _options.Retries >= 0 ? _options.Retries : 3;
     _retryInterval            = _options.RetryInterval > 0 ? _options.RetryInterval : 2;
     _qosOptions               = _options?.Qos ?? new RabbitMqOptions.QosOptions();
     if (_qosOptions.PrefetchCount < 1)
     {
         _qosOptions.PrefetchCount = 1;
     }
 }
Ejemplo n.º 29
0
        public JobQueue(
            IOptions <RocketBackgroundJobOptions> backgroundJobOptions,
            IOptions <RocketRabbitMqBackgroundJobOptions> rabbitMqRocketBackgroundJobOptions,
            IChannelPool channelPool,
            IRabbitMqSerializer serializer,
            IBackgroundJobExecuter jobExecuter,
            IServiceScopeFactory serviceScopeFactory,
            IExceptionNotifier exceptionNotifier)
        {
            RocketBackgroundJobOptions         = backgroundJobOptions.Value;
            RocketRabbitMqBackgroundJobOptions = rabbitMqRocketBackgroundJobOptions.Value;
            Serializer          = serializer;
            JobExecuter         = jobExecuter;
            ServiceScopeFactory = serviceScopeFactory;
            ExceptionNotifier   = exceptionNotifier;
            ChannelPool         = channelPool;

            JobConfiguration   = RocketBackgroundJobOptions.GetJob(typeof(TArgs));
            QueueConfiguration = GetOrCreateJobQueueConfiguration();

            Logger = NullLogger <JobQueue <TArgs> > .Instance;
        }
Ejemplo n.º 30
0
        public RabbitMqDistributedEventBus(
            IOptions <AbpRabbitMqEventBusOptions> options,
            IConnectionPool connectionPool,
            IRabbitMqSerializer serializer,
            IServiceScopeFactory serviceScopeFactory,
            IOptions <AbpDistributedEventBusOptions> distributedEventBusOptions,
            IRabbitMqMessageConsumerFactory messageConsumerFactory,
            ICurrentTenant currentTenant,
            IEventErrorHandler errorHandler,
            IOptions <AbpEventBusOptions> abpEventBusOptions)
            : base(serviceScopeFactory, currentTenant, errorHandler)
        {
            ConnectionPool                = connectionPool;
            Serializer                    = serializer;
            MessageConsumerFactory        = messageConsumerFactory;
            AbpEventBusOptions            = abpEventBusOptions.Value;
            AbpDistributedEventBusOptions = distributedEventBusOptions.Value;
            AbpRabbitMqEventBusOptions    = options.Value;

            HandlerFactories = new ConcurrentDictionary <Type, List <IEventHandlerFactory> >();
            EventTypes       = new ConcurrentDictionary <string, Type>();
        }