/// <summary> /// 每次重启时,清空队列 /// </summary> public static void DeleteQueue() { using (IConnection conn = RabbitMqFactory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { try { channel.QueueDelete(QueueName, true, false); } catch (Exception e) { LogWrite.WriteLogInfo($"重启时,存在消费者,不删除队列,异常{e.Message}"); } } } }
//测试方法 // public Task Execute(IJobExecutionContext context) // { // Console.WriteLine("微信推送"); // // #region 测试 // var stockSignalSingles = MongoService.GetAllStockSignalSingle(); // var model1 = stockSignalSingles.FirstOrDefault(); // var model2 = stockSignalSingles.Last(); // List<stock_signal_single> list = new List<stock_signal_single> // { // model1,model2 // }; // DirectExchangeSendMsg(list); // #endregion // // // return Task.FromResult(0); // } public static void DirectExchangeSendMsg(List <stock_signal_single> signalNowList) { using (IConnection conn = RabbitMqFactory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null); channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null); channel.QueueBind(QueueName, ExchangeName, routingKey: "signalList"); var props = channel.CreateBasicProperties(); props.Persistent = true; var listToJson = JsonConvert.SerializeObject(signalNowList); var msgBody = BinarySerializeHelper.SerializeObject(listToJson); channel.BasicPublish(exchange: ExchangeName, routingKey: "signalList", basicProperties: props, body: msgBody); } } }
public static void DirectExchangeSendMsg() { using (IConnection conn = RabbitMqFactory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null); channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null); channel.QueueBind(QueueName, ExchangeName, routingKey: "key1"); // channel.QueueDeclare(QueueName2, durable: true, autoDelete: false, exclusive: false, arguments: null); // channel.QueueBind(QueueName2, ExchangeName, routingKey: "key2"); var props = channel.CreateBasicProperties(); props.Persistent = true; var chgModel = Service.GetChgData(); var msgBody = BinarySerializeHelper.SerializeObject(chgModel); // var msgBody = Encoding.UTF8.GetBytes(bytes); channel.BasicPublish(exchange: ExchangeName, routingKey: "key1", basicProperties: props, body: msgBody); } } }