RabbitMQ_ExchangeType Direct(2)

DirectExchangeTest

Exchange: Exchange is a rout map
ExchangeType: Direct, the message will be pushed to specified queue which have binded with the exchange.
1.Send project code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 DirectExchangeTest(args, "DirectTestQueue");
static void DirectExchangeTest(string[] args,string quename)
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
string exchangeName = "DirectTest";
string routingKey = "DirectRK";
channel.ExchangeDeclare(exchangeName,ExchangeType.Direct,false,false,null);
channel.QueueDeclare(queue: "DirectTestQueue", durable: false, exclusive: false, autoDelete: false, arguments: null);
channel.QueueBind(quename, exchangeName, routingKey, null);
string message = args.Length > 0 ? exchangeName+" "+args[0] : "Hello RabbitMQ";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchangeName,routingKey, basicProperties: null, body: body);
Console.WriteLine("[x] Sent {0} ", message);

}
}
}

2.Receive project code:
1
ConsumeMsg(args, "DirectTestQueue");


3.Run command for send project

4.Run command for receive project

5.Check Exchange and queue

  1. Publish a meesage via RabbitMQ management, You will find the message can’t be published if the routingkey is not equal with “DirectRK”