private MqRedis(ConnectionMultiplexer redisConn, IModel channel) { this._redisConn = redisConn; this._mqChannel = channel; this._mqCountSender = new MqCountSender(this); VirtualRoot.BuildEventPath <Per20SecondEvent>("周期将本节点的MqCountData广播到Mq上去", LogEnum.None, message => { this._mqCountSender.SendMqCounts(MqCountRoot.GetMqCount()); }, typeof(MqCountRoot)); }
private MqRedis(ConnectionMultiplexer redisConn, IModel channel) { this._redisConn = redisConn; this._mqChannel = channel; this._mqCountSender = new MqCountSender(this); VirtualRoot.BuildEventPath <Per20SecondEvent>("周期将本节点的MqCountData广播到Mq上去", LogEnum.None, typeof(MqCountRoot), PathPriority.Normal, message => { this._mqCountSender.SendMqCounts(MqCountRoot.GetMqCount()); }); VirtualRoot.BuildCmdPath <RefreshMinerTestIdMqCommand>(this.GetType(), LogEnum.DevConsole, message => { RefreshClientTestId(); }); RefreshClientTestId(); }
private static void StartConsumer(IModel channel, AbstractMqMessagePath[] mqMessagePaths) { if (mqMessagePaths == null || mqMessagePaths.Length == 0) { return; } Task.Factory.StartNew(() => { DateTime startOn = DateTime.Now; // mq消费者不是立即启动的,而是异步启动的,在满足了后续的条件后才会启动的。 while (!mqMessagePaths.All(a => a.IsReadyToBuild)) { // WebApiServer由于启动时加载大量redis数据导致耗时长 if (startOn.AddSeconds(120) < DateTime.Now) { NTMinerConsole.UserFail("订阅Mq失败,因为超时"); return; } System.Threading.Thread.Sleep(100); } foreach (var mqMessagePathsByQueue in mqMessagePaths.GroupBy(a => a.Queue)) { string queue = mqMessagePathsByQueue.Key; bool durable = queue.EndsWith(MqKeyword.DurableQueueEndsWith); bool autoAck = !durable; channel.QueueDeclare( queue: queue, durable: durable, exclusive: false, autoDelete: !durable, arguments: null); EventingBasicConsumer consumer = new EventingBasicConsumer(channel); foreach (var mqMessagePath in mqMessagePathsByQueue) { mqMessagePath.Build(channel); } consumer.Received += (model, ea) => { MqCountRoot.ReceivedCount(ea.RoutingKey, queue); // 多线程消费,否则mq会成为瓶颈,尤其WebApiServer消息量大 Task.Factory.StartNew(() => { bool isPass = false; foreach (var mqMessagePath in mqMessagePathsByQueue) { try { bool r = mqMessagePath.Go(ea); if (r) { isPass = true; } } catch (Exception e) { Logger.ErrorDebugLine(e); } } if (!isPass) { Logger.WarnDebugLine($"路由键 {ea.RoutingKey} 没有消费者"); } if (!autoAck) { channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); } }); }; channel.BasicConsume(queue: queue, autoAck: autoAck, consumer: consumer); } NTMinerConsole.UserOk("订阅Mq成功"); }); }
public void BasicPublish(string routingKey, IBasicProperties basicProperties, byte[] body) { MqCountRoot.SendCount(routingKey); this._mqChannel.BasicPublish(MqKeyword.NTMinerExchange, routingKey, basicProperties, body); }