Skip to content

Commit 377dbe0

Browse files
committed
* Allow passing hostname to GH-1749 program as the first arg.
1 parent 1b1225c commit 377dbe0

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

projects/Applications/GH-1749/Program.cs

+41-7
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,28 @@ protected override Task OnCancelAsync(string[] consumerTags, CancellationToken c
5252

5353
static class Program
5454
{
55+
const string DefaultHostName = "localhost";
5556
const string ConnectionClientProvidedName = "GH_1749";
56-
57-
static readonly Util s_util = new();
5857
static readonly CancellationTokenSource s_cancellationTokenSource = new();
5958
static readonly CancellationToken s_cancellationToken = s_cancellationTokenSource.Token;
6059

60+
static Util? s_util;
61+
6162
static async Task Main(string[] args)
6263
{
64+
string hostname = DefaultHostName;
65+
if (args.Length > 0)
66+
{
67+
hostname = args[0];
68+
}
69+
70+
s_util = new Util(hostname, "guest", "guest");
71+
6372
AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
6473

6574
ConnectionFactory connectionFactory = new()
6675
{
76+
HostName = hostname,
6777
AutomaticRecoveryEnabled = true,
6878
UserName = "guest",
6979
Password = "guest",
@@ -87,9 +97,10 @@ static async Task Main(string[] args)
8797

8898
connection.ConnectionRecoveryErrorAsync += Connection_ConnectionRecoveryErrorAsync;
8999

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

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

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

113+
channel.CallbackExceptionAsync += Channel_CallbackExceptionAsync;
114+
channel.ChannelShutdownAsync += Channel_ChannelShutdownAsync;
115+
102116
QueueDeclareOk queue = await channel.QueueDeclareAsync();
103117

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

113127
static async Task CloseConnectionAsync()
114128
{
129+
if (s_util is null)
130+
{
131+
throw new NullReferenceException("s_util");
132+
}
133+
115134
try
116135
{
117136
Console.WriteLine("{0} [INFO] start closing connection: {1}", Now, ConnectionClientProvidedName);
@@ -126,16 +145,31 @@ static async Task CloseConnectionAsync()
126145

127146
private static string Now => Util.Now;
128147

129-
private static void CurrentDomain_FirstChanceException(object? sender, FirstChanceExceptionEventArgs e)
148+
private static Task Channel_CallbackExceptionAsync(object sender, CallbackExceptionEventArgs ea)
149+
{
150+
Console.WriteLine("{0} [INFO] channel saw CallbackExceptionAsync, event: {1}", Now, ea);
151+
Console.WriteLine("{0} [INFO] channel CallbackExceptionAsync, exception: {1}", Now, ea.Exception);
152+
return Task.CompletedTask;
153+
}
154+
155+
private static Task Channel_ChannelShutdownAsync(object sender, ShutdownEventArgs ea)
130156
{
131-
Console.WriteLine("{0} [INFO] saw FirstChanceException, exception: {1}", Now, e.Exception);
157+
Console.WriteLine("{0} [INFO] saw ChannelShutdownAsync, event: {1}", Now, ea);
158+
return Task.CompletedTask;
132159
}
133160

161+
private static void CurrentDomain_FirstChanceException(object? sender, FirstChanceExceptionEventArgs e)
162+
{
163+
if (e.Exception is ObjectDisposedException)
164+
{
165+
Console.WriteLine("{0} [INFO] saw FirstChanceException, exception: {1}", Now, e.Exception);
166+
}
167+
}
134168

135169
private static Task Connection_CallbackExceptionAsync(object sender, CallbackExceptionEventArgs ea)
136170
{
137-
Console.WriteLine("{0} [INFO] saw CallbackExceptionAsync, event: {1}", Now, ea);
138-
Console.WriteLine("{0} [INFO] CallbackExceptionAsync, exception: {1}", Now, ea.Exception);
171+
Console.WriteLine("{0} [INFO] connection saw CallbackExceptionAsync, event: {1}", Now, ea);
172+
Console.WriteLine("{0} [INFO] connection CallbackExceptionAsync, exception: {1}", Now, ea.Exception);
139173
return Task.CompletedTask;
140174
}
141175

projects/Applications/GH-1749/Util.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public class Util : IDisposable
4242
private readonly ManagementClient _managementClient;
4343
private static readonly bool s_isWindows = false;
4444

45-
public Util() : this("guest", "guest")
45+
public Util() : this("localhost", "guest", "guest")
4646
{
4747
}
4848

49-
public Util(string managementUsername, string managementPassword)
49+
public Util(string hostname, string managementUsername, string managementPassword)
5050
{
5151
if (string.IsNullOrEmpty(managementUsername))
5252
{
@@ -58,7 +58,7 @@ public Util(string managementUsername, string managementPassword)
5858
throw new ArgumentNullException(nameof(managementPassword));
5959
}
6060

61-
var managementUri = new Uri("http://localhost:15672");
61+
var managementUri = new Uri($"http://{hostname}:15672");
6262
_managementClient = new ManagementClient(managementUri, managementUsername, managementPassword);
6363
}
6464

0 commit comments

Comments
 (0)