Example #1
0
 public RabbitListener()
 {
     if (MQRabbitConfig.RabbitConfig == null)
     {
         throw new Exception("没设置MQ参数");
     }
     try
     {
         RabbitConfig options = MQRabbitConfig.RabbitConfig;
         var          factory = new ConnectionFactory()
         {
             // 这是我这边的配置,自己改成自己用就好
             HostName            = options.Host,
             UserName            = options.UserName,
             Password            = options.Password,
             Port                = options.Port,
             VirtualHost         = options.VHost,
             RequestedChannelMax = 5
         };
         this.connection = factory.CreateConnection();
         this.channel    = connection.CreateModel();
     }
     catch (Exception ex)
     {
         Console.WriteLine($"RabbitListener init error,ex:{ex.Message}");
     }
 }
Example #2
0
        public RabbitMQClient()
        {
            if (MQRabbitConfig.RabbitConfig == null)
            {
                throw new Exception("执行MQ参数失败");
            }
            RabbitConfig config = MQRabbitConfig.RabbitConfig;

            try
            {
                var factory = new ConnectionFactory()
                {
                    HostName    = config.Host,
                    UserName    = config.UserName,
                    Password    = config.Password,
                    Port        = config.Port,
                    VirtualHost = config.VHost
                };
                var connection = factory.CreateConnection();
                _channel = connection.CreateModel();
            }
            catch (Exception ex)
            {
                LogHelper.Error($"RabbitMQClient init fail,ErrorMessage{ex}");
            }
        }
Example #3
0
        public static void SetConfig(IConfiguration Configuration)
        {
            RabbitConfig rabbit = new RabbitConfig()
            {
                Host     = Configuration.GetSection("RabbitConfig:Host").Value,
                Password = Configuration.GetSection("RabbitConfig:Password").Value,
                Port     = Int32.Parse(Configuration.GetSection("RabbitConfig:Port").Value),
                VHost    = Configuration.GetSection("RabbitConfig:VHost").Value,
                UserName = Configuration.GetSection("RabbitConfig:UserName").Value,
            };

            RabbitConfig = rabbit;
        }
Example #4
0
        private static void SetMq()
        {
            MQRabbitConfig.RabbitConfig = new RabbitConfig()
            {
                Host     = "172.18.10.127",
                Password = "******",
                Port     = 5672,
                UserName = "******",
                VHost    = "/walletcloud"
            };
            RabbitConfig config = MQRabbitConfig.RabbitConfig;
            IModel       _channel;

            try
            {
                var factory = new ConnectionFactory()
                {
                    HostName    = config.Host,
                    UserName    = config.UserName,
                    Password    = config.Password,
                    Port        = config.Port,
                    VirtualHost = config.VHost
                };
                var connection = factory.CreateConnection();
                _channel = connection.CreateModel();
                _channel.ExchangeDeclare(exchange: "topic", type: "topic");
                _channel.QueueDeclare(queue: "topic_queue1", autoDelete: false);
                _channel.QueueDeclare(queue: "topic_queue2", autoDelete: false);
                _channel.QueueBind(queue: "topic_queue1", exchange: "topic", routingKey: "topic.queue1");
                _channel.QueueBind(queue: "topic_queue2", exchange: "topic", routingKey: "topic.#");
                for (int i = 0; i < 10; i++)
                {
                    var body1 = Encoding.UTF8.GetBytes("fanout_queue" + i);
                    var body2 = Encoding.UTF8.GetBytes("fanout_queue2" + i);
                    _channel.BasicPublish(exchange: "topic",
                                          routingKey: "topic.queue1",
                                          basicProperties: null,
                                          body: body1);
                    _channel.BasicPublish(exchange: "topic",
                                          routingKey: "topic.topic_queue2",
                                          basicProperties: null,
                                          body: body2);
                }
                Thread.Sleep(1000 * 1);
                var   consumer = new EventingBasicConsumer(_channel);
                Int32 all      = 0;
                consumer.Received += (model, ea) =>
                {
                    Interlocked.Increment(ref all);
                    var body   = ea.Body;
                    var result = true;
                    try
                    {
                        var message = Encoding.UTF8.GetString(body.Span.ToArray());
                        Console.WriteLine(message);
                    }
                    catch (Exception)
                    {
                        result = false;
                    }
                    if (result)
                    {
                        _channel.BasicAck(ea.DeliveryTag, false);
                    }
                    else
                    {
                        _channel.BasicNack(ea.DeliveryTag, false, true);
                    }
                };
                _channel.BasicConsume(queue: "topic_queue1", consumer: consumer);
                Thread.Sleep(1000);
                var consumer1 = new EventingBasicConsumer(_channel);
                consumer1.Received += (model, ea) =>
                {
                    Interlocked.Increment(ref all);
                    var body   = ea.Body;
                    var result = true;
                    try
                    {
                        var message = Encoding.UTF8.GetString(body.Span.ToArray());
                        Console.WriteLine(message);
                    }
                    catch (Exception)
                    {
                        result = false;
                    }
                    if (result)
                    {
                        _channel.BasicAck(ea.DeliveryTag, false);
                    }
                    else
                    {
                        _channel.BasicNack(ea.DeliveryTag, false, true);
                    }
                };
                _channel.BasicConsume(queue: "topic_queue2", consumer: consumer1);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }