RabbitMQ学习2--开发

话不多说,直接上代码,这边主要是演示了消息队列中的发送和接受。

发送方

// 连接配置
private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory()
{
    HostName = "localhost",
    UserName = "guest",
    Password = "zcd@jing",
    Port = 5672
};
// 路由名称
const string TopExchangeName = "topic.test.exchange";
//队列名称
const string TopQueueName = "topic.test.queue";

public static void TopicExchangeSendMsg()
{
    using (IConnection conn = rabbitMqFactory.CreateConnection())
    {
        using (IModel channel = conn.CreateModel())
        {
            channel.ExchangeDeclare(TopExchangeName, "topic", durable: false,autoDelete:false, arguments: null);
            channel.QueueDeclare(TopQueueName, durable: false, autoDelete: false,exclusive:false, arguments: null);
            channel.QueueBind(TopQueueName, TopExchangeName, routingKey: TopQueueName);
            //var props = channel.CreateBasicProperties();
            //props.Persistent = true;
            string vadata = Console.ReadLine();
            while (vadata != "exit")
            {
                var msgBody = Encoding.UTF8.GetBytes(vadata);
                channel.BasicPublish(exchange: TopExchangeName, routingKey:TopQueueName,basicProperties: null, body: msgBody);
                Console.WriteLine(string.Format("***发送时间:{0},发送完成,输入exit退消息送", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                vadata = Console.ReadLine();
            }
        }
    }
}
static void Main(string[] args)
{
    TopicExchangeSendMsg();
    Console.WriteLine("按任意值,退出程序");
    Console.ReadKey();
}

接收方

// 连接配置
private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory()
{
    HostName = "localhost",
    UserName = "guest",
    Password = "zcd@jing",
    Port = 5672
};
// 路由名称
const string TopExchangeName = "topic.test.exchange";
//队列名称
const string TopQueueName = "topic.test.queue";

public static void TopicAcceptExchange()
{
    using (IConnection conn = rabbitMqFactory.CreateConnection())
    {
        using (IModel channel = conn.CreateModel())
        {
            channel.ExchangeDeclare(TopExchangeName, "topic", durable: false,autoDelete:false, arguments: null);
            channel.QueueDeclare(TopQueueName, durable: false, autoDelete: false,exclusive:false, arguments: null);
            channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
            channel.QueueBind(TopQueueName, TopExchangeName, routingKey: TopQueueName);
            var consumer = new EventingBasicConsumer(channel);
            channel.BasicConsume(TopQueueName, noAck: false, consumer: consumer);
            consumer.Received += (model, ea) =>
            {
                var msgBody = Encoding.UTF8.GetString(ea.Body);
                Console.WriteLine(string.Format("***接收时间:{0},消息内容:{1}",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody));
                int dots = msgBody.Split('.').Length - 1;
                System.Threading.Thread.Sleep(dots * 1000);
                Console.WriteLine(" [x] Done");
                channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
            };
        }
    }
}

static void Main(string[] args)
{
    TopicAcceptExchange();
    Console.WriteLine("按任意值,退出程序");
    Console.ReadKey();
}

你可能感兴趣的:(RabbitMQ学习2--开发)