public static IServiceCollection AddRabbitConnection(this IServiceCollection services, IConfiguration config)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            services.AddOptions();

            var rabbitOption = new RabbitOption(config);

            var factory = new ConnectionFactory
            {
                Uri = new Uri(rabbitOption.Uri),
                AutomaticRecoveryEnabled = true,
                VirtualHost = "/",//此处建议根据项目使用不同的目录
            };

            services.Add(ServiceDescriptor.Singleton(factory));
            services.Add(ServiceDescriptor.Singleton(factory.CreateConnection()));
            return(services);
        }
Exemple #2
0
        public void Push(RabbitRequest request,
                         RabbitOption option, Action <ulong, bool> basicAckCall = null)
        {
            var correlationId = request.GetCorrelationId();

            if (!request.AllowDuplicatePublishing && _cacheStorage == null)
            {
                throw new Exception("不允许重复消费的队列需缓存对象");
            }
            if (!request.AllowDuplicatePublishing &&
                _cacheStorage.KeyExists(request.CacheDbIndex, correlationId))
            {
                return;
            }
            if (!request.AllowDuplicatePublishing)
            {
                var cacheModel = new Cache.Model.CacheUnitModel
                {
                    DataBaseIndex = request.CacheDbIndex,
                    Expire        = TimeSpan.FromDays(1),
                };
                _cacheStorage.SetAdd(cacheModel, correlationId, correlationId);
            }

            var pushed = RabbitObjFactory.CreatePushed(request.Header.PublishMethod);

            pushed.Factory = ConnectionFactory;

            pushed.Pushed(request, option, basicAckCall);
        }
Exemple #3
0
        public void Pushed(RabbitRequest request, RabbitOption option,
                           Action <ulong, bool> basicAckCall = null)
        {
            _basicAckCall = basicAckCall;

            var channel = Factory.CreatChannel();

            option.DeclareQueue(channel);

            var properties = channel.CreateBasicProperties();

            properties.Persistent = option.Durable;

            if (!option.AutoAck)
            {
                channel.ConfirmSelect();
                channel.BasicAcks  += Channel_BasicAcks;
                channel.BasicNacks += Channel_BasicNacks;
            }
            channel.BasicPublish(exchange: "",
                                 routingKey: option.RoutingKey,
                                 basicProperties: properties,
                                 body: request.GetData());
            if (option.AutoAck)
            {
                channel.Close();
            }
        }
Exemple #4
0
 public RabbitMQHostedService(IOptions <RabbitOption> options,
                              ILoggerFactory loggerFactory,
                              IServiceProvider serviceCollection)
 {
     _serviceCollection = serviceCollection;
     _options           = options.Value;
     _logger            = loggerFactory.CreateLogger <RabbitMQHostedService <TEvent, TEventHandler> >();
     ConsumeEvent();
 }
        private void ExecuteRabbitService(RabbitOption option)
        {
            Console.WriteLine("Start working with RabbitMQ...");

            if (option.Send)
            {
                if (!string.IsNullOrEmpty(option.Value))
                {
                    _rabbitProducerService.Send(option.Value);
                }
            }
            if (option.Receive)
            {
                _rabbitConsumerService.Subscribe();
            }
        }
        public void Pushed(RabbitRequest request,
                           RabbitOption option,
                           Action <ulong, bool> basicAckCall = null)
        {
            if (option.ExchangeOption == null)
            {
                throw new ArgumentNullException("未指定交换机信息");
            }

            var channel = Factory.CreatChannel();

            _basicAckCall = basicAckCall;

            //声明队列
            option.DeclareQueue(channel);

            //声明交换机
            option.ExchangeDeclare(channel);

            //将交换机和队列绑定
            option.BindQueue(channel);

            var properties = channel.CreateBasicProperties();

            properties.Persistent = option.Durable;

            if (!option.AutoAck && _basicAckCall != null)
            {
                channel.BasicAcks  += Channel_BasicAcks;
                channel.BasicNacks += Channel_BasicNacks;
            }
            channel.BasicPublish(exchange: option.ExchangeOption.ExchangeName,
                                 routingKey: option.RoutingKey,
                                 basicProperties: properties,
                                 body: request.GetData());
            if (option.AutoAck)
            {
                channel.Close();
            }
        }