public void SendMessage <T>(string exchangeName, T msg, string exchangeType, string qName = "", string routingKey = "", SerializeType type = SerializeType.Json) where T : class { if (string.IsNullOrEmpty(exchangeName) || msg == null) { return; } var body = SerializeObject(type, msg); Action a1 = () => { using (var channel = connHelper.GetConnection().CreateModel()) { //设置交换器的类型 channel.ExchangeDeclare(exchangeName, exchangeType); if (!string.IsNullOrWhiteSpace(qName)) { //声明一个队列,设置队列是否持久化,排他性,与自动删除 channel.QueueDeclare(qName, durable: true, false, false, null); //绑定消息队列,交换器,routingkey channel.QueueBind(qName, exchangeName, routingKey); } var properties = channel.CreateBasicProperties(); //队列持久化 properties.Persistent = true; //发送信息 channel.BasicPublish(exchangeName, routingKey, properties, body); } }; PolicyHelper.GetRetryTimesPolicy(3, ex => { var task = new Task(a1); TasksCache.Enqueue(task); }).Execute(() => { a1.Invoke(); }); }
public void SendMessage <T>(string qName, T msg, SerializeType type = SerializeType.Json) where T : class { if (string.IsNullOrEmpty(qName) || msg == null) { return; } var body = SerializeObject(type, msg); Action a1 = () => { using (var channel = connHelper.GetConnection().CreateModel()) { channel.QueueDeclare(qName, durable: true, false, false, null); var basicProperties = new BasicProperties { Persistent = true }; //RabbitMQ有一个默认的exchange="",此时qName即为routingKey channel.BasicPublish("", qName, basicProperties, body); } }; PolicyHelper.GetRetryTimesPolicy(3, ex => { var task = new Task(a1); TasksCache.Enqueue(task); }).Execute(() => { a1.Invoke(); }); }