No Exchange test
1.Install RabbitMQ :https://www.rabbitmq.com/download.html
2.Create two .net core projects for testing: Send and Receive
3.Send project Code:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21static void Main(string[] args)
{
NoExchangeTest(args,"NoExchangeQueue");
}
static void NoExchangeTest(string[] args,string queuename)
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: queuename, durable: false, exclusive: false, autoDelete: false, arguments: null);
string message = args.Length > 0 ? args[0] : "Hello RabbitMQ";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "", routingKey: queuename, basicProperties: null, body: body);
Console.WriteLine("[x] Sent {0} ", message);
}
}
}
Receive project code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31static void Main(string[] args)
{
ConsumeMsg(args, "NoExchangeQueue");
}
static void ConsumeMsg(string[] args, string queueName)
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var message = Encoding.UTF8.GetString(ea.Body);
Console.WriteLine("queue:"+queueName);
Console.WriteLine("[x] Received {0}", message);
Thread.Sleep(6000);
Console.WriteLine(" [x] Done");
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); //Manually send message acknowledgments
};
channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
4.Run Receive:
1
dotnet run
5.Run Send:
1
dotnet run "aaa"
6.Open RabitMQ admin :http://localhost:15672/ usrname :guest password:guest
7.Click queue tab,you will find a new queue was created named “NoExchangeQueue”