Skip to content

Commit 05799f6

Browse files
Merge pull request #38 from sopra-steria-norge/dotnet-2-eventdriven-oppsett
Fasit / oppsett av basic consumer & producer
2 parents c90296a + 05ee105 commit 05799f6

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

2-EventDriven/dotnet-consumer/Consumer/Program.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,31 @@
1616
// - Create a channel
1717
// - Declare a queue
1818
// - Create a consumer and subscribe to the queue
19-
// - Handle incoming messages by deserializing the JSON and printing the content to the console
19+
// - Handle incoming messages by deserializing the JSON and printing the content to the console
20+
21+
22+
23+
// Create a channel and declare the queue
24+
using var channel = await connection.CreateChannelAsync();
25+
await channel.QueueDeclareAsync(queue: "idem-events", durable: true, exclusive: false, autoDelete: false, arguments: null);
26+
27+
// Set up a consumer to listen for messages
28+
var consumer = new AsyncEventingBasicConsumer(channel);
29+
30+
// Handle received messages
31+
consumer.ReceivedAsync += async (sender, eventArgs) =>
32+
{
33+
var body = eventArgs.Body.ToArray();
34+
var message = JsonSerializer.Deserialize<JsonNode>(Encoding.UTF8.GetString(body));
35+
36+
Console.WriteLine($"Received message: {message}");
37+
38+
// Simulate processing time
39+
await Task.Delay(1000);
40+
41+
// Acknowledge the message
42+
await channel.BasicAckAsync(eventArgs.DeliveryTag, multiple: false);
43+
};
44+
// Start consuming messages
45+
await channel.BasicConsumeAsync(queue: "idem-events", autoAck: false, consumerTag: "", noLocal: false, exclusive: false, arguments: null, consumer: consumer);
46+
Console.ReadLine(); // Keep the application running to listen for messages

2-EventDriven/dotnet-consumer/Producer/Program.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,25 @@
1212
// Start with:
1313
// - Create a channel
1414
// - Declare a queue
15-
// - Publish a message to the queue (you can use a simple JSON string as the message body)
15+
// - Publish a message to the queue (you can use a simple JSON string as the message body)
16+
17+
18+
// Create a channel and declare the queue
19+
using var channel = await connection.CreateChannelAsync();
20+
await channel.QueueDeclareAsync(queue: "idem-events", durable: true, exclusive: false, autoDelete: false, arguments: null);
21+
22+
// Publish messages to the queue with for loop to simulate multiple events
23+
for (int i = 0; i < 10; i++)
24+
{
25+
var message = $"Idems Event {i + 1} at {DateTime.Now}";
26+
27+
var messageBody = JsonSerializer.Serialize(message);
28+
var body = Encoding.UTF8.GetBytes(messageBody);
29+
30+
await channel.BasicPublishAsync(exchange: string.Empty, routingKey: "idem-events", mandatory: true, basicProperties: new BasicProperties { Persistent = true }, body: body);
31+
Console.WriteLine($"Published event: {message}");
32+
33+
await Task.Delay(2000);
34+
}
35+
36+
Console.WriteLine("Producer finished.");

0 commit comments

Comments
 (0)