/
Program.cs
47 lines (41 loc) · 1.42 KB
/
Program.cs
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using RabbitMQ.Client;
namespace Producer
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("I am Direct Publisher");
sendMessage();
sendMessage();
sendMessage();
Console.WriteLine("Done!");
Console.ReadLine();
}
private static void sendMessage()
{
Thread.Sleep(1000);
var connectionFactory = new ConnectionFactory();
IConnection connection = connectionFactory.CreateConnection();
IModel channel = connection.CreateModel();
channel.ExchangeDeclare("direct-exchange-example", ExchangeType.Direct);
string value = DoSomethingInteresting();
string logMessage = string.Format("{0}: {1}", TraceEventType.Information, value);
byte[] message = Encoding.UTF8.GetBytes(logMessage);
channel.BasicPublish("direct-exchange-example", "", null, message);
Console.WriteLine(string.Format("I am Sending {0}", logMessage));
channel.Close();
connection.Close();
Console.WriteLine("Press Any key to send Next Message....");
Console.ReadLine();
}
static string DoSomethingInteresting()
{
return Guid.NewGuid().ToString();
}
}
}