Skip to content

Commit 336112d

Browse files
author
David R. MacIver
committed
fix API to provide parameterless CreateConnection
1 parent f11e641 commit 336112d

File tree

2 files changed

+38
-65
lines changed

2 files changed

+38
-65
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ namespace RabbitMQ.Client
7373
///</para>
7474
public class AmqpTcpEndpoint
7575
{
76+
///<summary>Indicates that the default port for the protocol should be used</summary>
77+
public const int UseDefaultPort = -1;
78+
7679
private IProtocol m_protocol;
7780
///<summary>Retrieve or set the IProtocol of this AmqpTcpEndpoint.</summary>
7881
public IProtocol Protocol
@@ -95,7 +98,7 @@ public string HostName
9598
///port number for the IProtocol to be used.</summary>
9699
public int Port
97100
{
98-
get { return (m_port == -1) ? m_protocol.DefaultPort : m_port; }
101+
get { return (m_port == UseDefaultPort) ? m_protocol.DefaultPort : m_port; }
99102
set { m_port = value; }
100103
}
101104

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

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,11 @@ namespace RabbitMQ.Client
7878
/// factory.UserName = ConnectionFactory.DefaultUser;
7979
/// factory.Password = ConnectionFactory.DefaultPass;
8080
/// factory.VirtualHost = ConnectionFactory.DefaultVHost;
81-
/// factory.Protocol = Protocols.DefaultProtocol;
81+
/// factory.Protocol = Protocols.FromEnvironment();
8282
/// factory.HostName = hostName;
83-
// factory.PortNumber = portNumber;
83+
// factory.PortNumber = AmqpTcpEndpoint.UseDefaultPort;
8484
/// //
85-
/// IProtocol protocol = Protocols.DefaultProtocol;
86-
/// IConnection conn = factory.CreateConnection(protocol, hostName, portNumber);
85+
/// IConnection conn = factory.CreateConnection();
8786
/// //
8887
/// IModel ch = conn.CreateModel();
8988
/// //
@@ -148,6 +147,31 @@ public class ConnectionFactory
148147
///<summary>Ssl options setting</summary>
149148
public SslOption Ssl = new SslOption();
150149

150+
///<summary>The host to connect to</summary>
151+
public String HostName = "localhost";
152+
153+
///<summary>The port to connect on. AmqpTcpEndpoint.UseDefaultPort indicates the
154+
/// default for the protocol should be used.</summary>
155+
public int Port = AmqpTcpEndpoint.UseDefaultPort;
156+
157+
///<summary>The AMQP protocol to be used</summary>
158+
public IProtocol Protocol = Protocols.FromEnvironment();
159+
160+
161+
public AmqpTcpEndpoint Endpoint
162+
{
163+
get
164+
{
165+
return new AmqpTcpEndpoint(Protocol, HostName, Port);
166+
}
167+
set
168+
{
169+
Protocol = value.Protocol;
170+
Port = value.Port;
171+
HostName = value.HostName;
172+
}
173+
}
174+
151175
///<summary>Construct a fresh instance, with all fields set to
152176
///their respective defaults.</summary>
153177
public ConnectionFactory() { }
@@ -262,79 +286,25 @@ protected virtual IConnection CreateConnection(int maxRedirects,
262286
///endpoint in the list provided. Up to a maximum of
263287
///maxRedirects broker-originated redirects are permitted for
264288
///each endpoint tried.</summary>
265-
public virtual IConnection CreateConnection(int maxRedirects,
266-
params AmqpTcpEndpoint[] endpoints)
289+
public virtual IConnection CreateConnection(int maxRedirects)
267290
{
268291
IDictionary connectionAttempts = new Hashtable();
269292
IDictionary connectionErrors = new Hashtable();
270293
IConnection conn = CreateConnection(maxRedirects,
271294
connectionAttempts,
272295
connectionErrors,
273-
endpoints);
296+
new AmqpTcpEndpoint[]{Endpoint});
274297
if (conn != null) {
275298
return conn;
276299
}
277300
throw new BrokerUnreachableException(connectionAttempts, connectionErrors);
278301
}
279302

280-
///<summary>Create a connection to the first available
281-
///endpoint in the list provided. No broker-originated
282-
///redirects are permitted.</summary>
283-
public virtual IConnection CreateConnection(params AmqpTcpEndpoint[] endpoints)
303+
///<summary>Create a connection to the specified endpoint
304+
///No broker-originated redirects are permitted.</summary>
305+
public virtual IConnection CreateConnection()
284306
{
285-
return CreateConnection(0, endpoints);
286-
}
287-
288-
///<summary>Create a connection to the endpoint specified.</summary>
289-
///<exception cref="ArgumentException"/>
290-
public IConnection CreateConnection(IProtocol version,
291-
string hostName,
292-
int portNumber)
293-
{
294-
return CreateConnection(new AmqpTcpEndpoint(version,
295-
hostName,
296-
portNumber,
297-
this.Ssl));
298-
}
299-
300-
///<summary>Create a connection to the endpoint specified. The
301-
///port used is the default for the protocol.</summary>
302-
///<exception cref="ArgumentException"/>
303-
public IConnection CreateConnection(IProtocol version, string hostName)
304-
{
305-
return CreateConnection(new AmqpTcpEndpoint(version, hostName));
306-
}
307-
308-
///<summary>Create a connection to the endpoint specified.</summary>
309-
///<remarks>
310-
/// Please see the class overview documentation for
311-
/// information about the Uri format in use.
312-
///</remarks>
313-
///<exception cref="ArgumentException"/>
314-
public IConnection CreateConnection(IProtocol version, Uri uri)
315-
{
316-
return CreateConnection(new AmqpTcpEndpoint(version, uri));
317-
}
318-
319-
///<summary>Create a connection to the endpoint specified,
320-
///with the IProtocol from
321-
///Protocols.FromEnvironment().</summary>
322-
///<remarks>
323-
/// Please see the class overview documentation for
324-
/// information about the Uri format in use.
325-
///</remarks>
326-
public IConnection CreateConnection(Uri uri)
327-
{
328-
return CreateConnection(new AmqpTcpEndpoint(uri));
329-
}
330-
331-
///<summary>Create a connection to the host (and optional
332-
///port) specified, with the IProtocol from
333-
///Protocols.FromEnvironment(). The format of the address
334-
///string is the same as that accepted by
335-
///AmqpTcpEndpoint.Parse().</summary>
336-
public IConnection CreateConnection(string address) {
337-
return CreateConnection(AmqpTcpEndpoint.Parse(Protocols.FromEnvironment(), address));
307+
return CreateConnection(0);
338308
}
339309
}
340310
}

0 commit comments

Comments
 (0)