Esempio n. 1
0
        public BaseProducer(ExchangeTypes type, RabbitMQConnectionModel server, RabbitMQChannelModel queue)
        {
            //1.创建连接
            _connection = server.Factory.CreateConnection();
            //2.创建通道
            _channel = _connection.CreateModel();
            //队列配置
            _queue = queue;

            switch (type)
            {
            case ExchangeTypes.direct:
                //3.声明一个交换机
                _channel.ExchangeDeclare(queue.ExchangeName, ExchangeType.Direct, true, false);
                break;

            case ExchangeTypes.topic:
                break;
            }
            if (type != ExchangeTypes.topic)
            {
                //4.声明一个队列
                _channel.QueueDeclare(_queue.QueueName,
                                      queue.Durable, _queue.Exclusive,
                                      queue.AutoDelete);
                //5.将队列绑定到交换机
                _channel.QueueBind(queue.QueueName, queue.ExchangeName, queue.ExchangeName, null);
            }
        }
Esempio n. 2
0
 public RabbitMQDefaultConsumer(RabbitMQConnectionModel server, RabbitMQChannelModel queue)
 {
     //创建连接
     Connection = server.Factory.CreateConnection();
     //创建通道
     Channel = Connection.CreateModel();
     //队列配置
     Queue = queue;
     //事件基本消费者
     Consumer = new EventingBasicConsumer(Channel);
 }
Esempio n. 3
0
        /// <summary>
        /// 创建生产者
        /// </summary>
        /// <param name="type"></param>
        /// <param name="server"></param>
        /// <param name="queue"></param>
        /// <returns></returns>
        public static BaseProducer CreateProducer(ExchangeTypes type, RabbitMQConnectionModel server, RabbitMQChannelModel queue)
        {
            return(new RabbitMQDefaultProducer(server, queue));

            /*var producer = new RabbitMQProducerModel();
             * //创建连接
             * producer.Connection = server.Factory.CreateConnection();
             * //创建通道
             * producer.Channel = producer.Connection.CreateModel();
             * //队列配置
             * producer.Queue = queue;
             * switch (type)
             * {
             *  case ExchangeTypes.topic:
             *      //声明一个队列
             *      producer.Channel.QueueDeclare(queue.QueueName, queue.Durable, queue.Exclusive, queue.AutoDelete);
             *      break;
             * }
             * return producer;*/
        }
Esempio n. 4
0
        private static void Main(string[] args)
        {
            var conn  = new RabbitMQConnectionModel("192.168.1.101", "zero", "123456");
            var queue = RabbitMQChannelFactory.CreateDefaultQueue("testq");
            //事件基本生产者
            var producer = RabbitMQProducerFactory.CreateProducer(
                ExchangeTypes.defult, conn, queue
                );

            Console.WriteLine("RabbitMQ连接成功,请输入消息,输入exit退出!");
            var input = string.Empty;

            do
            {
                input = Console.ReadLine();
                producer.Publish(input);
            }while (input.Trim().ToLower() != "exit");

            producer.Dispose();
            Console.ReadLine();
        }
Esempio n. 5
0
        private static void Main(string[] args)
        {
            var conn  = new RabbitMQConnectionModel("192.168.1.101", "zero", "123456");
            var queue = RabbitMQChannelFactory.CreateDefaultQueue("testq");
            //事件基本消费者
            var defaultMQ = new RabbitMQDefaultConsumer(conn, queue);


            //接收到消息事件
            defaultMQ.Consumer.Received += (sender, msg) =>
            {
                var message = Encoding.UTF8.GetString(msg.Body);
                Console.WriteLine($"收到消息:{message}_{msg.DeliveryTag}");
                defaultMQ.Channel.BasicAck(msg.DeliveryTag, false);
            };
            defaultMQ.Channel.BasicConsume(queue.QueueName, false, defaultMQ.Consumer);


            Console.WriteLine($"消费者已启动{queue.QueueName}");

            Console.ReadLine();
            defaultMQ.Dispose();
        }
Esempio n. 6
0
 public RabbitMQDefaultProducer(RabbitMQConnectionModel server, RabbitMQChannelModel queue) : base(ExchangeTypes.defult, server, queue)
 {
 }