public static void SendMessage(string queueName, object messages, long timeDelay = 0L) { using (IConnection rabbitConnection = RabbitPool.CreateRealConnection()) { using (IModel model = rabbitConnection.CreateModel()) { IBasicProperties basicProperties = model.CreateBasicProperties(); basicProperties.DeliveryMode = 2; basicProperties.Persistent = true; if (timeDelay > 0L) { string value = "exchange." + queueName + ".delay"; Dictionary <string, object> dictionary = new Dictionary <string, object>(); basicProperties.Expiration = timeDelay.ToString(); dictionary.Add("x-dead-letter-exchange", value); dictionary.Add("x-dead-letter-routing-key", "routing-delay"); model.QueueDeclare(queueName + ".delay", true, false, false, dictionary); string s = JsonConvert.ObjectToJson(messages, false, false, "yyyy-MM-dd HH:mm:ss"); byte[] bytes = Encoding.UTF8.GetBytes(s); model.BasicPublish("", queueName + ".delay", basicProperties, bytes); return; } model.QueueDeclare(queueName, true, false, false, null); string s2 = JsonConvert.ObjectToJson(messages, false, false, "yyyy-MM-dd HH:mm:ss"); byte[] bytes2 = Encoding.UTF8.GetBytes(s2); model.BasicPublish("", queueName, basicProperties, bytes2); } } }
public void ListenTopic(string queueName, bool isRemoveIdentity = false) { string queueName2 = queueName; if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["Rabbit_TopicIdentity"]) && !isRemoveIdentity) { queueName = queueName2 + "." + ConfigurationManager.AppSettings["Rabbit_TopicIdentity"].ToLower(); } else { string expr_75 = AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[] { '\\' }); string str = expr_75.Substring(expr_75.LastIndexOf('\\') + 1); queueName = queueName2 + "." + str; } IConnection connection = RabbitPool.CreateRealConnection(); IModel channel = connection.CreateModel(); channel.ExchangeDeclare(queueName2, "fanout", true, false, null); channel.QueueDeclare(queueName, true, false, false, null); channel.QueueBind(queueName, queueName2, "", null); channel.BasicQos(0u, 1, false); EventingBasicConsumer eventingBasicConsumer = new EventingBasicConsumer(channel); eventingBasicConsumer.Received += delegate(object sender, BasicDeliverEventArgs e) { string @string = Encoding.UTF8.GetString(e.Body); this.Handler(queueName, e.DeliveryTag, channel, @string); }; channel.BasicConsume(queueName, false, eventingBasicConsumer); }
public static void PublishTopic(string queueName, object messages) { string topicName = GetTopicName(queueName); using (IConnection rabbitConnection = RabbitPool.CreateRealConnection()) { using (IModel model = rabbitConnection.CreateModel()) { model.ExchangeDeclare(topicName, "fanout", true, false, null); IBasicProperties basicProperties = model.CreateBasicProperties(); basicProperties.DeliveryMode = 2; basicProperties.Persistent = true; string s = JsonConvert.ObjectToJson(messages, false, false, "yyyy-MM-dd HH:mm:ss"); byte[] bytes = Encoding.UTF8.GetBytes(s); model.BasicPublish(topicName, "", basicProperties, bytes); } } }
public void ListenQueue(string queueName) { string exchange = "exchange." + queueName + ".delay"; IConnection connection = RabbitPool.CreateRealConnection(); IModel channel = connection.CreateModel(); channel.QueueDeclare(queueName, true, false, false, null); channel.BasicQos(0u, 1, false); channel.ExchangeDeclare(exchange, "direct", true, false, null); channel.QueueBind(queueName, exchange, "routing-delay", null); EventingBasicConsumer eventingBasicConsumer = new EventingBasicConsumer(channel); eventingBasicConsumer.Received += delegate(object sender, BasicDeliverEventArgs e) { string @string = Encoding.UTF8.GetString(e.Body); this.Handler(queueName, e.DeliveryTag, channel, @string); }; channel.BasicConsume(queueName, false, eventingBasicConsumer); }