-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathProgram.cs
More file actions
58 lines (51 loc) · 1.82 KB
/
Program.cs
File metadata and controls
58 lines (51 loc) · 1.82 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
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";
string message = args.Length < 1 ? "info: Hello World!" : string.Join(" ", args);
ConnectionSettings settings = ConnectionSettingsBuilder.Create()
.Uri(new Uri(brokerUri))
.ContainerId("tutorial-emitlog")
.Build();
IEnvironment environment = AmqpEnvironment.Create(settings);
IConnection connection = await environment.CreateConnectionAsync();
try
{
IManagement management = connection.Management();
IExchangeSpecification exchangeSpec = management.Exchange(exchangeName).Type("fanout");
await exchangeSpec.DeclareAsync();
IPublisher publisher = await connection.PublisherBuilder().Exchange(exchangeName).BuildAsync();
try
{
var amqpMessage = new AmqpMessage(Encoding.UTF8.GetBytes(message));
PublishResult pr = await publisher.PublishAsync(amqpMessage);
switch (pr.Outcome.State)
{
case OutcomeState.Accepted:
break;
case OutcomeState.Released:
Console.Error.WriteLine($"Released message: {pr.Message.BodyAsString()}");
Environment.Exit(1);
break;
case OutcomeState.Rejected:
Console.Error.WriteLine($"[Publisher] Message: {pr.Message.BodyAsString()} rejected with error: {pr.Outcome.Error}");
Environment.Exit(1);
break;
default:
Console.Error.WriteLine($"Unexpected publish outcome: {pr.Outcome.State}");
Environment.Exit(1);
break;
}
Console.WriteLine($" [x] Sent '{message}'");
}
finally
{
await publisher.CloseAsync();
}
}
finally
{
await connection.CloseAsync();
await environment.CloseAsync();
}