예제 #1
0
        /// <summary>
        /// 这个事件就是用来监听我们一些不可达的消息的内容的:比如某些情况下,如果我们在发送消息时,当前的exchange不存在或者指定的routingkey路由不到,这个时候如果要监听这种不可达的消息,就要使用 return
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BasicReturnEventHandler(object?o, BasicReturnEventArgs basic)
        {
            var rc  = basic.ReplyCode;                               //消息失败的code
            var rt  = basic.ReplyText;                               //描述返回原因的文本。
            var msg = Encoding.UTF8.GetString(basic.Body.ToArray()); //失败消息的内容
                                                                     //在这里我们可能要对这条不可达消息做处理,比如是否重发这条不可达的消息呀,或者这条消息发送到其他的路由中呀,等等
                                                                     //System.IO.File.AppendAllText("d:/return.txt", "调用了Return;ReplyCode:" + rc + ";ReplyText:" + rt + ";Body:" + msg);

            MQWebApiMsg Data = Newtonsoft.Json.JsonConvert.DeserializeObject <MQWebApiMsg>(msg);

            PushErrorEvent?.Invoke(Data);

            Log.WriteLine($"异常事件 BasicReturn: ReplyCode:{rc};ReplyText:{rt};Body:{msg},all:{Newtonsoft.Json.JsonConvert.SerializeObject(basic)}");
            //Console.WriteLine($"-------- BasicReturn: ReplyCode:{rc};ReplyText:{rt};Body:{msg}");
        }
예제 #2
0
        /// <summary>
        /// 推送数据
        /// Routingkey 的格式 交换机名字:routingKey
        /// </summary>
        /// <param name="Data"></param>
        /// <param name="Routingkey">Routingkey 的格式 交换机名字:routingKey</param>
        public void PushData(MQWebApiMsg Data, string Key)
        {
            string[] keys = Key.Split(':');
            if (keys.Length != 2)
            {
                throw new Exception("MQ Routingkey 格式不正确,");;
            }
            string ExchangeName = keys[0];

            string Routingkey = keys[1];

            ChannelPool.AutoChannel(Channel =>
            {
                Channel.PushMsg(Data, ExchangeName, Routingkey);
            });
        }
예제 #3
0
        public override Task <NullClass> PushData(MQWebApiMsg request, ServerCallContext context)
        {
            var data = new MQServer.Model.MQWebApiMsg();

            data.Data = request.Data;

            data.Host = request.Host;

            data.Path = request.Path;

            MQService.PushQueueClient.PushData(data, request.Key);

            return(Task.FromResult(new NullClass()
            {
                Res = "ok"
            }));
        }
예제 #4
0
        public bool Post(MQWebApiMsg Data)
        {
            if (Data == null || Data.Host == null)
            {
                return(false);
            }
            string Url      = Path.Combine(Data.Host, Data.Path);
            string PostJson = Newtonsoft.Json.JsonConvert.SerializeObject(Data.Data);


            string res = HttpHelper.Post(Url, PostJson, requestEncoding: Encoding.UTF8, timeout: Timeout
                                         );

#if DEBUG
            Log.WriteLine("完成一条消息推送,线程id:" + System.Threading.Thread.CurrentThread.ManagedThreadId + ",Data:" + Data.Data.ToString());
#endif
            return(true);
        }
예제 #5
0
        /// <summary>
        /// 推送消息
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="request"></param>
        public void PushData(string Key, MQWebApiData request)
        {
            AutoChannelAddress(ChannelAddress =>
            {
                var Msg  = new MQWebApiMsg();
                Msg.Data = Newtonsoft.Json.JsonConvert.SerializeObject(request.Data);
                Msg.Host = request.Host;
                Msg.Path = request.Path;
                Msg.Key  = Key;


                try
                {
                    var GrpcClient = new MQWebApiMsgServerClient(ChannelAddress);

                    //var time = DateTime.Now.AddSeconds(60);

                    var reply = GrpcClient.PushData(Msg);

#if DEBUG
                    Log.WriteLine("成功发射一条消息:" + ChannelAddress.Target.ToString());
#endif
                }
                catch (Exception ex2)
                {
                    throw;
                }
                finally
                {
                }

                return(0);
            });



            return;
        }
예제 #6
0
        /// <summary>
        /// 再次推送失败消息
        /// </summary>
        /// <param name="basic"></param>
        void PushExceptionMsgAgain(BasicDeliverEventArgs basic, MQWebApiMsg Data)
        {
            ///先作废本次消息
            RabbitChannel.BasicReject(deliveryTag: basic.DeliveryTag, false);

            if (Data == null)
            {
                ReceivedErrorEvent?.Invoke(null);
                return;
            }

            if (Data.ExceptionTimes > 2)
            {
                ReceivedErrorEvent?.Invoke(Data);

                Log.WriteLine($"消息失败超过3次,丢弃 routingKey:{basic.RoutingKey}");
                return;
            }
            ///失败次数递增
            Data.ExceptionTimes += 1;

            this.PushMsg(Data, basic.Exchange, basic.RoutingKey);
            return;
        }