Esempio n. 1
0
        public void Pub <T>(MQMessage <T> message)
        {
            IConnection connection = null;
            IModel      channel    = null;

            try
            {
                var queueArg = new Dictionary <string, object>();
                //exchangeArg.Add("x-message-ttl", message.MessageTTL);//队列上消息过期时间,应小于队列过期时间
                queueArg.Add("x-dead-letter-exchange", MQ_DLX_EXCHANGE);    //过期消息转向路由
                queueArg.Add("x-dead-letter-routing-key", MQ_DLX_ROUTEKEY); //过期消息转向路由相匹配routingkey, 如果不指定沿用死信队列的routingkey
                Subscribe(ref connection, ref channel, message.ConsumerExchange, message.ConsumerQueue, message.ConsumerRouingKey, null, queueArg);
                var properties = channel.CreateBasicProperties();
                properties.DeliveryMode = 2;//数据持久化
                var headers = new Dictionary <string, object>();
                headers.Add("x-match", "all");
                headers.Add("RouteKey", message.ConsumerRouingKey);
                properties.Headers = headers;
                var msgBytes = JSONHelper.SerializeToByte(message);
                channel.BasicPublish(message.ConsumerExchange, message.ConsumerRouingKey, properties, msgBytes);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                UnSubscribe(connection, channel);
            }
        }
Esempio n. 2
0
 public void Reject <T>(IModel channel, BasicDeliverEventArgs ea, MQMessage <T> message)
 {
     if (message.RejectTime < 5)
     {
         var rejectTime = message.RejectTime;
         foreach (var item in MQ_RETRY_QUEUE_LIST)
         {
             if (rejectTime.Equals(item.RejectTime))
             {
                 var headers = new Dictionary <string, object>();
                 headers.Add("x-match", "all");
                 headers.Add("RouteKey", item.QueueName);
                 message.RejectTime += 1; //重试次数加一
                 new DLXProducter().Pub <T>(message, headers);
             }
         }
     }
     channel.BasicAck(ea.DeliveryTag, false);
 }
Esempio n. 3
0
        public void Pub <T>(MQMessage <T> message, Dictionary <string, object> headers = null)
        {
            IConnection connection = null;
            IModel      channel    = null;

            try
            {
                connection = CreateConnectFactory().CreateConnection();
                channel    = connection.CreateModel();
                channel.BasicQos(0, 1, false);
                channel.ExchangeDeclare(MQ_DLX_EXCHANGE, ExchangeType.Headers, true, false, null);

                foreach (var item in MQ_RETRY_QUEUE_LIST)
                {
                    if (message.RejectTime.Equals(item.RejectTime))
                    {
                        channel.QueueDeclare(item.QueueName, true, false, false, new Dictionary <string, object>()
                        {
                            { "x-message-ttl", item.TTL },
                            { "x-dead-letter-exchange", MQ_USER_EXCHANGE }
                        });
                        channel.QueueBind(item.QueueName, MQ_DLX_EXCHANGE, string.Empty, new Dictionary <string, object>()
                        {
                            { "x-match", "all" },
                            { "RouteKey", item.QueueName }
                        });
                    }
                }
                var properties = channel.CreateBasicProperties();
                properties.DeliveryMode = 2;//数据持久化
                properties.Headers      = headers;
                var msgBytes = JSONHelper.SerializeToByte(message);
                channel.BasicPublish(MQ_DLX_EXCHANGE, string.Empty, properties, msgBytes);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                UnSubscribe(connection, channel);
            }
        }