예제 #1
0
        /// <summary>
        /// 发布消息到内置死信队列
        /// </summary>
        /// <param name="queue">队列名称</param>
        /// <param name="exchange">交换机名称</param>
        /// <param name="routingKey">路由键</param>
        /// <param name="body">消息内容</param>
        /// <param name="retryCount">重试次数</param>
        /// <param name="exception">异常</param>
        /// <returns></returns>
        private bool PublishToDead(
            string queue,
            string exchange,
            string routingKey,
            string body,
            int retryCount,
            Exception exception)
        {
            //内置死信队列、交换机、路由键
            var deadLetterQueue      = $"{queue.ToLower()}.{(exception != null ? "error" : "fail")}";
            var deadLetterExchange   = DefaultDeadLetterExchange;
            var deadLetterRoutingKey = $"{queue.ToLower()}.deadletter";

            //内置死信队列内容
            var deadLetterBody = new DeadLetterQueue
            {
                Body           = body,
                CreateDateTime = DateTime.Now,
                Exception      = exception,
                ExceptionMsg   = exception?.Message,
                Queue          = queue,
                RoutingKey     = routingKey,
                Exchange       = exchange,
                RetryCount     = retryCount
            };

            //发送内置死信消息,注意此处与原生的死信消息无关
            return(Publish(deadLetterExchange, deadLetterQueue, deadLetterRoutingKey, deadLetterBody.ToJson()));
        }
예제 #2
0
        /// <summary>
        /// 发布消息到死信队列
        /// </summary>
        /// <param name="queue">队列名称</param>
        /// <param name="exchange">交换机名称</param>
        /// <param name="routingKey">路由键</param>
        /// <param name="body">消息内容</param>
        /// <param name="retryCount">重试次数</param>
        /// <param name="exception">异常</param>
        /// <returns></returns>
        private bool PublishToDead <T>(
            string queue,
            string exchange,
            string routingKey,
            string body,
            int retryCount,
            Exception exception)
        {
            //死信队列、交换机、路由键
            var deadLetterQueue      = $"{queue.ToLower()}.{(exception != null ? "error" : "fail")}";
            var deadLetterExchange   = DefaultDeadLetterExchange;
            var deadLetterRoutingKey = $"{queue.ToLower()}.deadletter";
            var attribute            = typeof(T).GetAttribute <RabbitMqAttribute>();

            if (attribute != null)
            {
                if (!attribute.DeadLetterExchange.IsNullOrEmpty())
                {
                    deadLetterExchange = attribute.DeadLetterExchange;
                }
                if (!attribute.DeadLetterRoutingKey.IsNullOrEmpty())
                {
                    deadLetterRoutingKey = attribute.DeadLetterRoutingKey;
                }
            }

            //死信队列内容
            var deadLetterBody = new DeadLetterQueue
            {
                Body           = body,
                CreateDateTime = DateTime.Now,
                Exception      = exception,
                ExceptionMsg   = exception?.Message,
                Queue          = queue,
                RoutingKey     = routingKey,
                Exchange       = exchange,
                RetryCount     = retryCount
            };

            return(Publish(deadLetterExchange, deadLetterQueue, deadLetterRoutingKey, deadLetterBody.ToJson()));
        }