Skip to content

Commit

Permalink
Tutorial 4: Routing
Browse files Browse the repository at this point in the history
  • Loading branch information
ahcantarim committed Jul 8, 2021
1 parent 65aabc6 commit e682564
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 4 deletions.
41 changes: 41 additions & 0 deletions src/Tutorial.RabbitMQ.Console.EmitLogDirect/EmitLogDirect.cs
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();
}
}
}
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>
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}'");
}
}
}
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>
24 changes: 20 additions & 4 deletions src/Tutorial.RabbitMQ.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tutorial.RabbitMQ.Console.R
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "1. Tutorial 1 - Basic Producer and Consumer", "1. Tutorial 1 - Basic Producer and Consumer", "{0CEB372D-E82E-43EB-9386-C340A3EBD2B2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tutorial.RabbitMQ.Console.NewTask", "Tutorial.RabbitMQ.Console.NewTask\Tutorial.RabbitMQ.Console.NewTask.csproj", "{E1D63EAD-60E9-4FE5-B11B-A32995992AFC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tutorial.RabbitMQ.Console.NewTask", "Tutorial.RabbitMQ.Console.NewTask\Tutorial.RabbitMQ.Console.NewTask.csproj", "{E1D63EAD-60E9-4FE5-B11B-A32995992AFC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "2. Tutorial 2 - Work Queues", "2. Tutorial 2 - Work Queues", "{C6745A90-0523-45D5-8D40-8406CBBE2D5A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tutorial.RabbitMQ.Console.Worker", "Tutorial.RabbitMQ.Console.Worker\Tutorial.RabbitMQ.Console.Worker.csproj", "{95234244-9F35-48B5-84B5-421D2BE20264}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tutorial.RabbitMQ.Console.Worker", "Tutorial.RabbitMQ.Console.Worker\Tutorial.RabbitMQ.Console.Worker.csproj", "{95234244-9F35-48B5-84B5-421D2BE20264}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "3. Tutorial 3 - Publish and Subscribe", "3. Tutorial 3 - Publish and Subscribe", "{ECFD14A2-DC0A-4F2E-A12B-5F9C2D57CE11}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tutorial.RabbitMQ.Console.EmitLog", "Tutorial.RabbitMQ.Console.EmitLog\Tutorial.RabbitMQ.Console.EmitLog.csproj", "{33D24547-D67D-4FEB-8C26-E7DFFA3527A4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tutorial.RabbitMQ.Console.EmitLog", "Tutorial.RabbitMQ.Console.EmitLog\Tutorial.RabbitMQ.Console.EmitLog.csproj", "{33D24547-D67D-4FEB-8C26-E7DFFA3527A4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tutorial.RabbitMQ.Console.ReceiveLogs", "Tutorial.RabbitMQ.Console.ReceiveLogs\Tutorial.RabbitMQ.Console.ReceiveLogs.csproj", "{BB96A1AD-69F5-4D7A-B305-4E5D7E87A2BF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tutorial.RabbitMQ.Console.ReceiveLogs", "Tutorial.RabbitMQ.Console.ReceiveLogs\Tutorial.RabbitMQ.Console.ReceiveLogs.csproj", "{BB96A1AD-69F5-4D7A-B305-4E5D7E87A2BF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "4. Tutorial 4 - Routing", "4. Tutorial 4 - Routing", "{C487ED4B-21F0-4969-86BB-863B088BD7B0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tutorial.RabbitMQ.Console.EmitLogDirect", "Tutorial.RabbitMQ.Console.EmitLogDirect\Tutorial.RabbitMQ.Console.EmitLogDirect.csproj", "{36029E5A-A2EF-445F-ABB0-5BCBD25E44FD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tutorial.RabbitMQ.Console.ReceiveLogsDirect", "Tutorial.RabbitMQ.Console.ReceiveLogsDirect\Tutorial.RabbitMQ.Console.ReceiveLogsDirect.csproj", "{9F3ECD8E-BEB4-459D-89BD-0705E87FED73}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -51,6 +57,14 @@ Global
{BB96A1AD-69F5-4D7A-B305-4E5D7E87A2BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB96A1AD-69F5-4D7A-B305-4E5D7E87A2BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB96A1AD-69F5-4D7A-B305-4E5D7E87A2BF}.Release|Any CPU.Build.0 = Release|Any CPU
{36029E5A-A2EF-445F-ABB0-5BCBD25E44FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{36029E5A-A2EF-445F-ABB0-5BCBD25E44FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{36029E5A-A2EF-445F-ABB0-5BCBD25E44FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{36029E5A-A2EF-445F-ABB0-5BCBD25E44FD}.Release|Any CPU.Build.0 = Release|Any CPU
{9F3ECD8E-BEB4-459D-89BD-0705E87FED73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F3ECD8E-BEB4-459D-89BD-0705E87FED73}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F3ECD8E-BEB4-459D-89BD-0705E87FED73}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F3ECD8E-BEB4-459D-89BD-0705E87FED73}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -62,6 +76,8 @@ Global
{95234244-9F35-48B5-84B5-421D2BE20264} = {C6745A90-0523-45D5-8D40-8406CBBE2D5A}
{33D24547-D67D-4FEB-8C26-E7DFFA3527A4} = {ECFD14A2-DC0A-4F2E-A12B-5F9C2D57CE11}
{BB96A1AD-69F5-4D7A-B305-4E5D7E87A2BF} = {ECFD14A2-DC0A-4F2E-A12B-5F9C2D57CE11}
{36029E5A-A2EF-445F-ABB0-5BCBD25E44FD} = {C487ED4B-21F0-4969-86BB-863B088BD7B0}
{9F3ECD8E-BEB4-459D-89BD-0705E87FED73} = {C487ED4B-21F0-4969-86BB-863B088BD7B0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F38BDFBB-F9A8-4372-973D-39FB03E21EA4}
Expand Down

0 comments on commit e682564

Please sign in to comment.