Exemplo n.º 1
0
        //发布订阅模式
        public void Product_Faout()
        {
            Console.WriteLine("exchange start");

            using (IConnection conn = RabbitConnectionFactory.GetConnectionFactory().CreateConnection())
            {
                using (IModel channel = conn.CreateModel())
                {
                    string exchangeName = "exchange1";

                    channel.ExchangeDeclare(
                        exchange: exchangeName,
                        type: "fanout"
                        );

                    while (true)
                    {
                        Console.WriteLine("please input message:");
                        string message = Console.ReadLine();

                        byte[] body = Encoding.UTF8.GetBytes(message);

                        channel.BasicPublish(
                            exchange: exchangeName,
                            routingKey: "",
                            basicProperties: null,
                            body: body
                            );
                        Console.WriteLine("publish success");
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void Producer()
        {
            Console.WriteLine("RabbitMQ start");


            using (IConnection conn = RabbitConnectionFactory.GetConnectionFactory().CreateConnection())
            {
                using (IModel channel = conn.CreateModel())
                {
                    Console.WriteLine("create channel success");

                    string queueName = "Worker";

                    channel.QueueDeclare(
                        queue: queueName,
                        durable: false,
                        exclusive: false,
                        autoDelete: false,
                        arguments: null
                        );

                    while (true)
                    {
                        Console.WriteLine("message contant");

                        string message = Console.ReadLine();

                        byte[] body = Encoding.UTF8.GetBytes(message);

                        channel.BasicPublish(
                            exchange: "",
                            routingKey: queueName,
                            basicProperties: null,
                            body: body
                            );

                        Console.WriteLine("send message success:" + message);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void Product_Topic(string[] args)
        {
            if (args.Length == 0)
            {
                throw new ArgumentException("args");
            }

            Console.WriteLine("exchange direct start ");

            using (IConnection connection = RabbitConnectionFactory.GetConnectionFactory().CreateConnection())
            {
                using (IModel channel = connection.CreateModel())
                {
                    string exchangeName = "exchange3";

                    string routeKey = args[0];

                    channel.ExchangeDeclare(
                        exchange: exchangeName,
                        type: "topic"
                        );

                    while (true)
                    {
                        Console.WriteLine("please input message:");
                        string message = Console.ReadLine();

                        byte[] body = Encoding.UTF8.GetBytes(message);

                        channel.BasicPublish(
                            exchange: exchangeName,
                            routingKey: routeKey,
                            basicProperties: null,
                            body: body);

                        Console.WriteLine("publish success");
                    }
                }
            }
        }