/// <summary> /// 拉模式消费死信队列 /// </summary> /// <returns></returns> public static async Task <IModel> PullMessageConsumeForDLXAsync(Func <string, Task <ConsumeActionEnum> > handler, RMQSubscribeModel subscribeModel) { var connection = RabbitMQBaseHelper.CreateMQConnectionInPoolNew(); // var channel = connection.CreateModel(); var channel = DefineQueue(connection, subscribeModel); BasicGetResult response = channel.BasicGet(RabbitMQConstant.DLXPrefix + subscribeModel.QueueName, false); if (response != null) { string message = Encoding.UTF8.GetString(response.Body); ConsumeActionEnum consumeResult = ConsumeActionEnum.RETRY; Task <ConsumeActionEnum> result = handler(message); consumeResult = await result; if (consumeResult == ConsumeActionEnum.ACCEPT) { channel.BasicAck(response.DeliveryTag, false); //消息从队列中删除 } else if (consumeResult == ConsumeActionEnum.RETRY) { channel.BasicNack(response.DeliveryTag, false, true); //消息重回队列 } else { channel.BasicNack(response.DeliveryTag, false, false); //消息直接丢弃 } } else { Console.WriteLine($"{RabbitMQConstant.DLXPrefix + subscribeModel.QueueName}暂无可消费信息"); } RabbitMQBaseHelper.ResetMQConnectionToFree(connection); return(channel); }
public bool DeleteQueueOnServer(string queueName) { var connection = RabbitMQBaseHelper.CreateMQConnectionInPoolNew(); using (var channel = connection.CreateModel())//建立通讯信道 { channel.QueueDelete(queueName, false, false); channel.QueueDelete(RabbitMQConstant.DLXPrefix + queueName, false, false); RabbitMQBaseHelper.ResetMQConnectionToFree(connection); } return(true); }
/// <summary> /// 定义一个队列 /// </summary> /// <param name="subscribeModel"></param> /// <returns></returns> public bool DeclareQueueOnServer(RMQSubscribeModel subscribeModel) { var connection = RabbitMQBaseHelper.CreateMQConnectionInPoolNew(); using (var channel = connection.CreateModel())//建立通讯信道 { SubscribeHelper.DefineQueue(connection, subscribeModel); RabbitMQBaseHelper.ResetMQConnectionToFree(connection); } return(true); }
/// <summary> /// 发布(发布者确认模式) /// </summary> /// <typeparam name="T">消息类型</typeparam> /// <param name="message">消息内容</param> /// <param name="exChangeName">交换器名称</param> /// <param name="isNeedDLX">是否需要死信交换器</param> /// <returns></returns> public static bool ConfirmsPush <T>(RMQPublishModel <T> publishModel) { var exchangeTypeString = RabbitMQBaseHelper.GetExchangeTypeString(publishModel.ExchangeType); var connection = RabbitMQBaseHelper.CreateMQConnectionInPoolNew(); try { using (var channel = connection.CreateModel())//建立通讯信道 { bool isSuccess = true; channel.ExchangeDeclare(publishModel.ExchangeName, exchangeTypeString, true, false, null); //死信交换器 if (publishModel.IsNeedDLX) { string dlx_exchange = RabbitMQConstant.DLXPrefix + publishModel.ExchangeName.Trim(); channel.ExchangeDeclare(dlx_exchange, exchangeTypeString, true, false, null); } // 开启发送方确认模式 channel.ConfirmSelect(); channel.BasicQos(0, publishModel.SendMessageCount, false); //分发机制为触发式 var properties = channel.CreateBasicProperties(); properties.DeliveryMode = 2; //1表示不持久,2.表示持久化 string msgJson = JsonHelper.SerializeObject(publishModel.Message); var body = Encoding.UTF8.GetBytes(msgJson); bool mandatory = true; channel.BasicPublish(publishModel.ExchangeName, publishModel.RoutingKey, mandatory, properties, body); channel.BasicReturn += (ch, ea) => { isSuccess = false; }; channel.WaitForConfirmsOrDie(); return(isSuccess); } } catch (Exception) { return(false); } finally { RabbitMQBaseHelper.ResetMQConnectionToFree(connection); } }
/// <summary> /// 消息死信队列消费(fanout) /// </summary>` /// <typeparam name="T">消息类型</typeparam> /// <param name="handler">回调</param> /// <param name="exChangeName">交换器名</param> /// <param name="queueName">队列名</param> /// <param name="routingKey">路由名(默认和队列一致)</param> /// <param name="isNeedDLX">是否需要死信队列</param> public static IModel FanoutConsumeForDLX(Func <string, Task <ConsumeActionEnum> > handler, RMQSubscribeModel subscribeModel) { var connection = RabbitMQBaseHelper.CreateMQConnectionInPoolNew(); try { return(ConsumeForDLX(connection, handler, subscribeModel)); } catch (Exception) { //重新连接 return(ConsumeForDLX(connection, handler, subscribeModel)); // throw ex; } finally { RabbitMQBaseHelper.ResetMQConnectionToFree(connection); } }
/// <summary> /// 获得队列的消息数量 /// </summary> /// <param name="queueName"></param> /// <returns></returns> public uint GetQueueMessageCount(string queueName) { var connection = RabbitMQBaseHelper.CreateMQConnectionInPoolNew(); uint messageCount = 0; using (var channel = connection.CreateModel())//建立通讯信道 { try { messageCount = channel.MessageCount(queueName); } catch { } finally { RabbitMQBaseHelper.ResetMQConnectionToFree(connection); } } return(messageCount); }