Exemple #1
0
        //public bool Receive(out string message) => this.Receive(out message, out string _);

        //public bool Receive(out string message, out string routingKey)
        //{
        //    message = string.Empty;
        //    routingKey = string.Empty;
        //    try
        //    {
        //        BasicGetResult basicGetResult = this.Channel.BasicGet(this.consumerConfigInfo.QueueName, true);
        //        if (basicGetResult == null)
        //        {
        //            Thread.Sleep(500);
        //            return false;
        //        }
        //        byte[] body = basicGetResult.Body.ToArray();
        //        message = CommonLibrary.BinaryDeserialize(body);
        //        routingKey = basicGetResult.RoutingKey;
        //        return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        //RecordLog.WriteLog("QueueName:" + this.consumerConfigInfo.QueueName + ",Receive ", "", "", this.consumerConfigInfo.QueueName, ex);
        //        Thread.Sleep(500);
        //        this.Dispose();
        //        return false;
        //    }
        //}

        // public bool ReceiveNeedAck(out string message, out ulong deliveryTag) => this.ReceiveNeedAck(out message, out deliveryTag, out string _);

        public bool ReceiveNeedAck(out string message, out ulong deliveryTag, out string routingKey)
        {
            message     = string.Empty;
            routingKey  = string.Empty;
            deliveryTag = 0UL;
            try
            {
                BasicGetResult basicGetResult = this.Channel.BasicGet(this.consumerConfigInfo.QueueName, false);
                if (basicGetResult == null)
                {
                    Thread.Sleep(500);
                    return(false);
                }
                byte[] body = basicGetResult.Body.ToArray();
                message     = CommonLibrary.BinaryDeserialize(body);
                routingKey  = basicGetResult.RoutingKey;
                deliveryTag = basicGetResult.DeliveryTag;
                return(true);
            }
            catch (Exception ex)
            {
                // RecordLog.WriteLog("QueueName:" + this.consumerConfigInfo.QueueName + ",ReceiveNeedAck", "", "", this.consumerConfigInfo.QueueName, ex);
                Thread.Sleep(500);
                this.Dispose();
                return(false);
            }
        }
Exemple #2
0
 public bool SendWithNotRetry(byte[] messageBodyBytes, string routingKey, bool isMessageDurable = true)
 {
     try
     {
         IBasicProperties basicProperties = CommonLibrary.CreateBasicProperties(this.Channel, new BasicPropertiesConfigInfo()
         {
             Durable = isMessageDurable
         });
         this.Channel.BasicPublish(this.producerConfigInfo.ExchangeName, routingKey, basicProperties, messageBodyBytes);
         return(true);
     }
     catch (Exception ex)
     {
         //   RecordLog.WriteLog("ExchangeName:" + this.producerConfigInfo.ExchangeName + ",RoutingKey:" + routingKey + ",发送失败", this.producerConfigInfo.ExchangeName, routingKey, "", ex);
         this.Dispose();
         return(false);
     }
 }
Exemple #3
0
        public bool Send(
            string message,
            string routingKey,
            BasicPropertiesConfigInfo basicPropertiesConfigInfo)
        {
            IBasicProperties basicProperties = basicPropertiesConfigInfo == null ? (IBasicProperties)null : CommonLibrary.CreateBasicProperties(this.Channel, basicPropertiesConfigInfo);

            byte[] body = CommonLibrary.BinarySerialize(message);
            //try
            //{
            this.Channel.BasicPublish(this.producerConfigInfo.ExchangeName, routingKey, basicProperties, body);
            this.RepairException();
            return(true);
            //}
            //catch (Exception ex)
            //{
            //    //RecordLog.WriteLog("ExchangeName:" + this.producerConfigInfo.ExchangeName + ",RoutingKey:" + routingKey + ",发送失败:本次消息已记录到本地临时文件", this.producerConfigInfo.ExchangeName, routingKey, "", ex);
            //    this.SaveException(message, routingKey, this.producerConfigInfo, basicPropertiesConfigInfo);
            //    this.Dispose();
            //    return false;
            //}
        }