Skip to content

Commit 71ad5a7

Browse files
Merge branch 'stable'
2 parents 874e05a + a840f58 commit 71ad5a7

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

projects/client/RabbitMQ.Client/src/client/api/ConnectionFactory.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,9 @@ public IConnection CreateConnection(IList<string> hostnames, String clientProvid
419419
else
420420
{
421421
IProtocol protocol = Protocols.DefaultProtocol;
422-
conn = protocol.CreateConnection(this, false, CreateFrameHandler(), clientProvidedName);
422+
var selectedHost = this.HostnameSelector.NextFrom(hostnames);
423+
var endPoint = AmqpTcpEndpoint.Parse(selectedHost);
424+
conn = protocol.CreateConnection(this, false, CreateFrameHandler(endPoint), clientProvidedName);
423425
}
424426
}
425427
catch (Exception e)
@@ -445,9 +447,12 @@ public IFrameHandler CreateFrameHandler(AmqpTcpEndpoint endpoint)
445447

446448
public IFrameHandler CreateFrameHandlerForHostname(string hostname)
447449
{
448-
return CreateFrameHandler(this.Endpoint.CloneWithHostname(hostname));
450+
var ep = AmqpTcpEndpoint.Parse(hostname);
451+
ep.Ssl = this.Endpoint.Ssl;
452+
return CreateFrameHandler(ep);
449453
}
450454

455+
451456
private IFrameHandler ConfigureFrameHandler(IFrameHandler fh)
452457
{
453458
// make sure socket timeouts are higher than heartbeat

projects/client/RabbitMQ.Client/src/client/impl/AutorecoveringConnection.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,11 @@ public void Init(IList<string> hostnames)
562562
{
563563
try
564564
{
565-
fh = m_factory.CreateFrameHandler(m_factory.Endpoint.CloneWithHostname(h));
565+
var endPoint = CloneEndpointWithHostname(h);
566+
fh = m_factory.CreateFrameHandler(endPoint);
566567
reachableHostname = h;
567-
} catch (Exception caught)
568+
}
569+
catch (Exception caught)
568570
{
569571
e = caught;
570572
}
@@ -796,7 +798,8 @@ protected void RecoverConnectionDelegate()
796798
try
797799
{
798800
var nextHostname = m_factory.HostnameSelector.NextFrom(this.hostnames);
799-
var fh = m_factory.CreateFrameHandler(m_factory.Endpoint.CloneWithHostname(nextHostname));
801+
var endPoint = CloneEndpointWithHostname(nextHostname);
802+
var fh = m_factory.CreateFrameHandler(endPoint);
800803
m_delegate = new Connection(m_factory, false, fh, this.ClientProvidedName);
801804
recovering = false;
802805
}
@@ -1003,5 +1006,12 @@ protected bool ShouldTriggerConnectionRecovery(ShutdownEventArgs args)
10031006
// connectivity loss or abrupt shutdown
10041007
args.Initiator == ShutdownInitiator.Library);
10051008
}
1009+
1010+
private AmqpTcpEndpoint CloneEndpointWithHostname(string hostname)
1011+
{
1012+
var ate = AmqpTcpEndpoint.Parse(hostname);
1013+
ate.Ssl = m_factory.Endpoint.Ssl;
1014+
return ate;
1015+
}
10061016
}
10071017
}

projects/client/Unit/src/unit/TestConnectionFactory.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,35 @@ public void TestProperties()
6868
Assert.AreEqual(cf.HostName, h);
6969
Assert.AreEqual(cf.Port, p);
7070
}
71+
72+
[Test]
73+
public void TestCreateConnectionParsesHostNameWithPort()
74+
{
75+
var cf = new ConnectionFactory();
76+
cf.AutomaticRecoveryEnabled = true;
77+
cf.HostName = "not_localhost";
78+
cf.Port = 1234;
79+
var conn = cf.CreateConnection(new System.Collections.Generic.List<string> { "localhost:5672" }, "oregano");
80+
conn.Close();
81+
conn.Dispose();
82+
Assert.AreEqual("not_localhost", cf.HostName);
83+
Assert.AreEqual(1234, cf.Port);
84+
Assert.AreEqual("localhost", conn.Endpoint.HostName);
85+
Assert.AreEqual(5672, conn.Endpoint.Port);
86+
}
87+
88+
[Test]
89+
public void TestCreateConnectionWithoutAutoRecoverySelectsAHostFromTheList()
90+
{
91+
var cf = new ConnectionFactory();
92+
cf.AutomaticRecoveryEnabled = false;
93+
cf.HostName = "not_localhost";
94+
cf.Port = 1234;
95+
var conn = cf.CreateConnection(new System.Collections.Generic.List<string> { "localhost" }, "oregano");
96+
conn.Close();
97+
conn.Dispose();
98+
Assert.AreEqual("not_localhost", cf.HostName);
99+
Assert.AreEqual("localhost", conn.Endpoint.HostName);
100+
}
71101
}
72102
}

0 commit comments

Comments
 (0)