-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathProgram.cs
More file actions
46 lines (39 loc) · 1.36 KB
/
Program.cs
File metadata and controls
46 lines (39 loc) · 1.36 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
using System.Text;
using RabbitMQ.AMQP.Client;
using RabbitMQ.AMQP.Client.Impl;
const string brokerUri = "amqp://guest:guest@localhost:5672/%2f";
const string taskQueueName = "task_queue";
string message = args.Length > 0 ? string.Join(" ", args) : "Hello World!";
ConnectionSettings settings = ConnectionSettingsBuilder.Create()
.Uri(new Uri(brokerUri))
.ContainerId("tutorial-newtask")
.Build();
IEnvironment environment = AmqpEnvironment.Create(settings);
IConnection connection = await environment.CreateConnectionAsync();
try
{
IManagement management = connection.Management();
IQueueSpecification queueSpec = management.Queue(taskQueueName).Type(QueueType.QUORUM);
await queueSpec.DeclareAsync();
IPublisher publisher = await connection.PublisherBuilder().Queue(taskQueueName).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 '{message}'");
}
finally
{
await publisher.CloseAsync();
}
}
finally
{
await connection.CloseAsync();
await environment.CloseAsync();
}