Skip to content

Commit

Permalink
* Allow passing hostname to GH-1749 program as the first arg.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebakken committed Dec 19, 2024
1 parent 1b1225c commit 377dbe0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
48 changes: 41 additions & 7 deletions projects/Applications/GH-1749/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,28 @@ protected override Task OnCancelAsync(string[] consumerTags, CancellationToken c

static class Program
{
const string DefaultHostName = "localhost";
const string ConnectionClientProvidedName = "GH_1749";

static readonly Util s_util = new();
static readonly CancellationTokenSource s_cancellationTokenSource = new();
static readonly CancellationToken s_cancellationToken = s_cancellationTokenSource.Token;

static Util? s_util;

static async Task Main(string[] args)
{
string hostname = DefaultHostName;
if (args.Length > 0)
{
hostname = args[0];
}

s_util = new Util(hostname, "guest", "guest");

AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;

ConnectionFactory connectionFactory = new()
{
HostName = hostname,
AutomaticRecoveryEnabled = true,
UserName = "guest",
Password = "guest",
Expand All @@ -87,9 +97,10 @@ static async Task Main(string[] args)

connection.ConnectionRecoveryErrorAsync += Connection_ConnectionRecoveryErrorAsync;

connection.ConnectionShutdownAsync += async (object sender, ShutdownEventArgs ea) =>
connection.ConnectionShutdownAsync += (object sender, ShutdownEventArgs ea) =>
{
Console.WriteLine("{0} [INFO] saw ConnectionShutdownAsync, event: {1}", Now, ea);
return Task.CompletedTask;
};

connection.ConsumerTagChangeAfterRecoveryAsync += Connection_ConsumerTagChangeAfterRecoveryAsync;
Expand All @@ -99,6 +110,9 @@ static async Task Main(string[] args)

await using var channel = await connection.CreateChannelAsync(options: channelOptions);

channel.CallbackExceptionAsync += Channel_CallbackExceptionAsync;
channel.ChannelShutdownAsync += Channel_ChannelShutdownAsync;

QueueDeclareOk queue = await channel.QueueDeclareAsync();

var consumer = new GH1749Consumer(channel);
Expand All @@ -112,6 +126,11 @@ static async Task Main(string[] args)

static async Task CloseConnectionAsync()
{
if (s_util is null)
{
throw new NullReferenceException("s_util");
}

try
{
Console.WriteLine("{0} [INFO] start closing connection: {1}", Now, ConnectionClientProvidedName);
Expand All @@ -126,16 +145,31 @@ static async Task CloseConnectionAsync()

private static string Now => Util.Now;

private static void CurrentDomain_FirstChanceException(object? sender, FirstChanceExceptionEventArgs e)
private static Task Channel_CallbackExceptionAsync(object sender, CallbackExceptionEventArgs ea)
{
Console.WriteLine("{0} [INFO] channel saw CallbackExceptionAsync, event: {1}", Now, ea);
Console.WriteLine("{0} [INFO] channel CallbackExceptionAsync, exception: {1}", Now, ea.Exception);
return Task.CompletedTask;
}

private static Task Channel_ChannelShutdownAsync(object sender, ShutdownEventArgs ea)
{
Console.WriteLine("{0} [INFO] saw FirstChanceException, exception: {1}", Now, e.Exception);
Console.WriteLine("{0} [INFO] saw ChannelShutdownAsync, event: {1}", Now, ea);
return Task.CompletedTask;
}

private static void CurrentDomain_FirstChanceException(object? sender, FirstChanceExceptionEventArgs e)
{
if (e.Exception is ObjectDisposedException)
{
Console.WriteLine("{0} [INFO] saw FirstChanceException, exception: {1}", Now, e.Exception);
}
}

private static Task Connection_CallbackExceptionAsync(object sender, CallbackExceptionEventArgs ea)
{
Console.WriteLine("{0} [INFO] saw CallbackExceptionAsync, event: {1}", Now, ea);
Console.WriteLine("{0} [INFO] CallbackExceptionAsync, exception: {1}", Now, ea.Exception);
Console.WriteLine("{0} [INFO] connection saw CallbackExceptionAsync, event: {1}", Now, ea);
Console.WriteLine("{0} [INFO] connection CallbackExceptionAsync, exception: {1}", Now, ea.Exception);
return Task.CompletedTask;
}

Expand Down
6 changes: 3 additions & 3 deletions projects/Applications/GH-1749/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public class Util : IDisposable
private readonly ManagementClient _managementClient;
private static readonly bool s_isWindows = false;

public Util() : this("guest", "guest")
public Util() : this("localhost", "guest", "guest")
{
}

public Util(string managementUsername, string managementPassword)
public Util(string hostname, string managementUsername, string managementPassword)
{
if (string.IsNullOrEmpty(managementUsername))
{
Expand All @@ -58,7 +58,7 @@ public Util(string managementUsername, string managementPassword)
throw new ArgumentNullException(nameof(managementPassword));
}

var managementUri = new Uri("http://localhost:15672");
var managementUri = new Uri($"http://{hostname}:15672");
_managementClient = new ManagementClient(managementUri, managementUsername, managementPassword);
}

Expand Down

0 comments on commit 377dbe0

Please sign in to comment.