-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65aabc6
commit e682564
Showing
5 changed files
with
149 additions
and
4 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/Tutorial.RabbitMQ.Console.EmitLogDirect/EmitLogDirect.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using RabbitMQ.Client; | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Tutorial.RabbitMQ.Console.EmitLogDirect | ||
{ | ||
class EmitLogDirect | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var factory = new ConnectionFactory() { HostName = "localhost" }; | ||
var exchangeName = "direct_logs"; | ||
|
||
using (var connection = factory.CreateConnection()) | ||
using (var channel = connection.CreateModel()) | ||
{ | ||
channel.ExchangeDeclare(exchange: exchangeName, | ||
type: ExchangeType.Direct); | ||
|
||
var severity = (args.Length > 0) ? args[0] : "info"; | ||
|
||
var message = (args.Length > 1) | ||
? string.Join(" ", args.Skip(1).ToArray()) | ||
: "Hello World!"; | ||
|
||
var body = Encoding.UTF8.GetBytes(message); | ||
|
||
channel.BasicPublish(exchange: exchangeName, | ||
routingKey: severity, | ||
basicProperties: null, | ||
body: body); | ||
|
||
System.Console.WriteLine($"{DateTime.Now}: Sent '{severity}':'{message}'"); | ||
} | ||
|
||
System.Console.WriteLine($"{DateTime.Now}: Press [enter] to exit."); | ||
System.Console.ReadLine(); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Tutorial.RabbitMQ.Console.EmitLogDirect/Tutorial.RabbitMQ.Console.EmitLogDirect.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="RabbitMQ.Client" Version="6.2.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
64 changes: 64 additions & 0 deletions
64
src/Tutorial.RabbitMQ.Console.ReceiveLogsDirect/ReceiveLogsDirect.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using RabbitMQ.Client; | ||
using RabbitMQ.Client.Events; | ||
using System; | ||
using System.Text; | ||
|
||
namespace Tutorial.RabbitMQ.Console.ReceiveLogsDirect | ||
{ | ||
class ReceiveLogsDirect | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var factory = new ConnectionFactory() { HostName = "localhost" }; | ||
var exchangeName = "direct_logs"; | ||
|
||
using (var connection = factory.CreateConnection()) | ||
using (var channel = connection.CreateModel()) | ||
{ | ||
channel.ExchangeDeclare(exchange: exchangeName, | ||
type: ExchangeType.Direct); | ||
|
||
var queueName = channel.QueueDeclare().QueueName; | ||
|
||
if (args.Length < 1) | ||
{ | ||
System.Console.Error.WriteLine($"{DateTime.Now}: Usage: {Environment.GetCommandLineArgs()[0]} [info] [warning] [error]."); | ||
|
||
System.Console.WriteLine($"{DateTime.Now}: Press [enter] to exit."); | ||
System.Console.ReadLine(); | ||
|
||
Environment.ExitCode = 1; | ||
return; | ||
} | ||
|
||
foreach (var severity in args) | ||
{ | ||
channel.QueueBind(queue: queueName, | ||
exchange: exchangeName, | ||
routingKey: severity); | ||
} | ||
|
||
System.Console.WriteLine($"{DateTime.Now}: Waiting for messages."); | ||
|
||
var consumer = new EventingBasicConsumer(channel); | ||
consumer.Received += Consumer_Received; | ||
|
||
channel.BasicConsume(queue: queueName, | ||
autoAck: true, | ||
consumer: consumer); | ||
|
||
System.Console.WriteLine($"{DateTime.Now}: Press [enter] to exit."); | ||
System.Console.ReadLine(); | ||
} | ||
} | ||
|
||
private static void Consumer_Received(object sender, BasicDeliverEventArgs e) | ||
{ | ||
var body = e.Body.ToArray(); | ||
var message = Encoding.UTF8.GetString(body); | ||
var routingKey = e.RoutingKey; | ||
|
||
System.Console.WriteLine($"{DateTime.Now}: Received '{routingKey}':'{message}'"); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ial.RabbitMQ.Console.ReceiveLogsDirect/Tutorial.RabbitMQ.Console.ReceiveLogsDirect.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="RabbitMQ.Client" Version="6.2.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters