Exemplo n.º 1
0
Arquivo: MQ.cs Projeto: CTLRX/ForCTLRX
 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);
         }
     }
 }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        static RabbitPool()
        {
            RabbitPool.factory         = new ConnectionFactory();
            RabbitPool.connections     = new List <RabbitConnection>();
            RabbitPool.autoConnections = new List <RabbitConnection>();
            RabbitPool.objLock         = new object();
            RabbitPool.maxPoolSize     = 10;
            RabbitPool._addresss       = string.Empty;
            RabbitPool._username       = string.Empty;
            RabbitPool._pwd            = string.Empty;
            RabbitPool._port           = string.Empty;
            RabbitPool.timer           = new System.Timers.Timer(60000.0);
            RabbitPool.t = 0;
            string text = ConfigurationManager.AppSettings["Rabbit_MaxPoolSize"];

            if (!string.IsNullOrEmpty(text))
            {
                int num = 0;
                if (int.TryParse(text, out num) && num > 0)
                {
                    RabbitPool.maxPoolSize = num;
                }
            }
            RabbitPool._addresss = ConfigurationManager.AppSettings["MQServer"];
            if (string.IsNullOrEmpty(RabbitPool._addresss))
            {
                throw new Exception("未指定MQ服务器");
            }
            RabbitPool._username = ConfigurationManager.AppSettings["RabbitUserName"];
            if (string.IsNullOrEmpty(RabbitPool._username))
            {
                throw new Exception("未指定MQ服务器帐号");
            }
            RabbitPool._pwd = ConfigurationManager.AppSettings["RabbitPassword"];
            if (string.IsNullOrEmpty(RabbitPool._pwd))
            {
                throw new Exception("未指定MQ服务器密码");
            }
            RabbitPool._port = ConfigurationManager.AppSettings["RabbitPort"];
            if (RabbitPool._addresss.IndexOf(':') > 0)
            {
                RabbitPool.factory.HostName = RabbitPool._addresss.Substring(0, RabbitPool._addresss.IndexOf(':'));
                RabbitPool.factory.Port     = int.Parse(RabbitPool._addresss.Substring(RabbitPool._addresss.IndexOf(':') + 1));
            }
            else
            {
                RabbitPool.factory.HostName = RabbitPool._addresss;
            }
            RabbitPool.factory.UserName = RabbitPool._username;
            RabbitPool.factory.Password = RabbitPool._pwd;
            if (!string.IsNullOrEmpty(RabbitPool._port))
            {
                RabbitPool.factory.Port = int.Parse(RabbitPool._port);
            }
            RabbitPool.factory.AutomaticRecoveryEnabled = true;
            RabbitPool.InitPool();
            // RabbitPool.timer.Elapsed += new ElapsedEventHandler(RabbitPool.Timer_Elapsed);
            RabbitPool.timer.AutoReset = true;
            RabbitPool.timer.Enabled   = true;
        }
Exemplo n.º 4
0
Arquivo: MQ.cs Projeto: CTLRX/ForCTLRX
        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);
                }
            }
        }
Exemplo n.º 5
0
        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);
        }