-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathProgram.cs
More file actions
59 lines (49 loc) · 1.65 KB
/
Program.cs
File metadata and controls
59 lines (49 loc) · 1.65 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
using System.Text;
using RabbitMQ.AMQP.Client;
using RabbitMQ.AMQP.Client.Impl;
const string brokerUri = "amqp://guest:guest@localhost:5672/%2f";
const string exchangeName = "logs_direct";
string severity = GetSeverity(args);
string message = GetMessage(args);
ConnectionSettings settings = ConnectionSettingsBuilder.Create()
.Uri(new Uri(brokerUri))
.ContainerId("tutorial-emitlogdirect")
.Build();
IEnvironment environment = AmqpEnvironment.Create(settings);
IConnection connection = await environment.CreateConnectionAsync();
try
{
IManagement management = connection.Management();
IExchangeSpecification exchangeSpec = management.Exchange(exchangeName).Type("direct");
await exchangeSpec.DeclareAsync();
IPublisher publisher = await connection.PublisherBuilder().Exchange(exchangeName).Key(severity).BuildAsync();
try
{
var amqpMessage = new AmqpMessage(Encoding.UTF8.GetBytes(message));
PublishResult pr = await publisher.PublishAsync(amqpMessage);
if (pr.Outcome.State != OutcomeState.Accepted)
{
Console.Error.WriteLine($"Unexpected publish outcome: {pr.Outcome.State}");
Environment.Exit(1);
}
Console.WriteLine($" [x] Sent '{severity}':'{message}'");
}
finally
{
await publisher.CloseAsync();
}
}
finally
{
await connection.CloseAsync();
await environment.CloseAsync();
}
static string GetSeverity(string[] strings) => strings.Length < 1 ? "info" : strings[0];
static string GetMessage(string[] strings)
{
if (strings.Length < 2)
{
return "Hello World!";
}
return string.Join(" ", strings.Skip(1));
}