Ejemplo n.º 1
0
        public static void Produce(string message, string exchangeType, RabbitmqSetting rabbitmqConnInfo)
        {
            try
            {
                var factory = InitConnectionFactory(rabbitmqConnInfo);
                using (var connection = factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        DeclareRoute(channel, exchangeType);

                        var properties = channel.CreateBasicProperties();
                        //队列持久化
                        properties.Persistent = true;
                        var bytes = Encoding.UTF8.GetBytes(message);
                        //转发消息业务规则
                        var routingKey = "DOLA.RMQ.ONE";

                        var exchangeName = EXCHANGE_NAME_PRE + exchangeType;
                        //发送信息
                        channel.BasicPublish(exchangeName, routingKey, properties, bytes);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 2
0
        public static string Consume(string userName, RabbitmqSetting rabbitmqConnInfo)
        {
            try
            {
                var factory = InitConnectionFactory(rabbitmqConnInfo);
                using (var connection = factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        DeclareRoute(channel, ExchangeType.Direct);

                        //定义这个队列的消费者
                        QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
                        var qName = GetQueueName(userName);
                        //false为手动应答,true为自动应答
                        channel.BasicConsume(qName, false, consumer);

                        BasicDeliverEventArgs ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
                        byte[] bytes             = ea.Body;
                        var    messageStr        = Encoding.UTF8.GetString(bytes);
                        channel.BasicAck(ea.DeliveryTag, false);
                        return(messageStr);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(null);
            }
        }
Ejemplo n.º 3
0
        public static ConnectionFactory InitConnectionFactory(RabbitmqSetting rabbitmqConnInfo)
        {
            //var uri = new Uri("amqp://127.0.0.1:5672/");
            var uri     = new Uri(rabbitmqConnInfo.Uri);
            var factory = new ConnectionFactory
            {
                //UserName = "******",
                //Password = "******",
                UserName           = rabbitmqConnInfo.UserName,
                Password           = rabbitmqConnInfo.Password,
                RequestedHeartbeat = 0,
                Endpoint           = new AmqpTcpEndpoint(uri)
            };

            return(factory);
        }