/// <summary> /// 接收信息 /// </summary> /// <param name="queueName">队列名</param> /// <param name="handler">事件处理方法</param> /// <param name="setting">设置项</param> public void BeginReceive(string queueName, EventHandler <QueueEventArgs> handler, QueueSetting setting = null) { if (setting == null) { setting = new QueueSetting(); } //声明新的频道 var channel = CreateChannelDetail(queueName).Channel; if (channel is null) { return; } var dead = setting.DeadLetter; var arguments = setting.Arguments; this.SetDeadLetter(channel, queueName, ref arguments, dead); //声明队列 this.CreateQueue(channel, queueName, setting.Persistent, setting.Exclusive, setting.AutoDelete, arguments); //消费者 var consumer = new EventingBasicConsumer(channel); //noAck=true时拿到就删除,channel.BasicQos无效 //noAck=fasle时需要应答才删除,channel.BasicQos设置最大无应答数,超过不再获得 if (setting.NoAckCount > 0 && !setting.NoAck) { channel.BasicQos(0, setting.NoAckCount, false); } //处理消息 consumer.Received += (sender, arg) => { if (setting.ReceiveDelay > 0) { Thread.Sleep(setting.ReceiveDelay); } handler(sender, new QueueEventArgs(arg.DeliveryTag, arg.Body.ToArray(), setting.Display)); }; channel.BasicConsume(queueName, setting.NoAck, consumer); }
/// <summary> /// 发布消息队列 /// </summary> /// <param name="queueName">队列名</param> /// <param name="message">队列信息</param> /// <param name="setting">设置项</param> /// <returns>发布是否成功</returns> public bool Publish <T>(string queueName, T message, QueueSetting setting = null) { if (setting == null) { setting = new QueueSetting(); } //消息序列化 var type = typeof(T).ToString().ToLower(); string _message; if (Constants.BasicTypes.Contains(type)) { _message = message.ToString(); } else { _message = JsonUtil.ToJson(message); } return(this.Publish(queueName, _message, setting.ExchangeName, setting.MatchType.ToString(), setting.RoutingKey, setting.Persistent, setting)); }
/// <summary> /// 发布消息队列 /// </summary> /// <param name="queueName">队列名</param> /// <param name="message">队列信息</param> /// <param name="exchangeName">交换器名称</param> /// <param name="exchangeType">交换器类型:Direct完全匹配、Fanout不匹配、Topic模式匹配</param> /// <param name="routingKey">路由键,当交换器名称为空时,routingKey必须为空</param> /// <param name="persistent">是否持久化</param> /// <param name="setting">配置项</param> /// <returns>发布是否成功</returns> private bool Publish(string queueName, string message, string exchangeName, string exchangeType, string routingKey, bool persistent, QueueSetting setting) { if (string.IsNullOrEmpty(exchangeName)) { if (!string.IsNullOrEmpty(routingKey)) { return(false); } exchangeName = ""; routingKey = queueName; } //声明新的频道 var channelDetail = CreateChannelDetail(queueName); if (channelDetail is null) { return(false); } var channel = channelDetail.Channel; if (channel is null) { return(false); } bool exclusive = setting.Exclusive; bool autoDelete = setting.AutoDelete; int expiration = setting.Expiration; var arguments = setting.Arguments; var dead = setting.DeadLetter; this.SetDeadLetter(channel, queueName, ref arguments, dead); //声明新的队列 this.CreateQueue(channel, queueName, persistent, exclusive, autoDelete, arguments); //声明交换器、并绑定队列 if (!channelDetail.ExchangeDeclared) { this.CreateExchange(channel, exchangeName, exchangeType, persistent, autoDelete, arguments); this.BindQueue(channel, exchangeName, queueName, routingKey); //更新 channelDetail.ExchangeDeclared = true; channelDetail.BindTime = DateTime.Now; channelDetail = channelDetails.AddOrUpdate(queueName, channelDetail, (key, oldValue) => { return(channelDetail); }); } //发布消息 try { var msgByte = Encoding.Default.GetBytes(message); var properties = channel.CreateBasicProperties(); properties.Persistent = persistent; if (expiration > 0) { properties.Expiration = expiration.ToString(); } channel.BasicPublish(exchangeName, routingKey, properties, msgByte); return(true); } catch { return(false); } }