private static IConnection CreateConnection(RabbitMQMessageBusOptions options)
        {
            if (string.IsNullOrEmpty(options.HostName))
            {
                throw new Exception("请配置rabbitMQ的HostName参数");
            }

            var factory = new ConnectionFactory()
            {
                //HostName = options.HostName,
                Port        = options.Port,
                VirtualHost = options.VirtualHost,
                UserName    = options.UserName,
                Password    = options.Password,

                AutomaticRecoveryEnabled = true,
                // Protocol = Protocols.DefaultProtocol
                DispatchConsumersAsync = true
            };
            var hostNames  = options.HostName.Replace(" ", "").Split(new char[] { ',', ',' }, StringSplitOptions.RemoveEmptyEntries);
            var connection = factory.CreateConnection(hostNames);

            connection.CallbackException += Connection_CallbackException;
            return(connection);
        }
        public RabbitMQMessageBus(IServiceProvider serviceProvider, ILogger <RabbitMQMessageBus> logger, RabbitMQMessageBusOptions options)
        {
            _serviceProvider = serviceProvider;
            _logger          = logger;
            _options         = options;

            _connection    = _serviceProvider.GetService <IConnection>();
            this._producer = new RabbitMQProducer(this._serviceProvider);
        }
 private static void ValidOptions(RabbitMQMessageBusOptions options)
 {
     if (options.AutoAck == false)
     {
         AssertUtils.IsTrue(options.ManualCommitBatch >= 1, "ManualCommitBatch大于等于1");
         AssertUtils.IsTrue(options.PrefetchCount >= 1, "PrefetchCount大于等于1");
         AssertUtils.IsTrue(options.PrefetchCount >= options.ManualCommitBatch, "PrefetchCount大于等于ManualCommitBatch");
     }
     AssertUtils.IsTrue(options.DefaultConsumerThreadCount >= 1, "DefaultConsumerThreadCount大于等于1");
 }
Exemplo n.º 4
0
 public static string GetDelayTopic(RabbitMQMessageBusOptions options)
 {
     return($"{options.TopicPrefix }delay-queue");
 }
Exemplo n.º 5
0
 public static string GetDelayConsumerExchange(RabbitMQMessageBusOptions options)
 {
     return($"{options.TopicPrefix }delay-consumer-exchange");
 }
Exemplo n.º 6
0
 public static string GeteDelayExchangeName(RabbitMQMessageBusOptions options)
 {
     return($"{options.TopicPrefix }delay-exchange");
 }
        public static IServiceCollection AddRabbitMQMessageBus(this IServiceCollection services, RabbitMQMessageBusOptions options)
        {
            ValidOptions(options);
            var connection = CreateConnection(options);

            services
            .AddSingleton <RabbitMQMessageBusOptions>(options)
            .AddSingleton(connection)
            .AddSingleton <IRabbitMQMessageBus, RabbitMQMessageBus>();

            return(services);
        }