Skip to content

Commit

Permalink
Merge pull request #35 from tiggerite/connectionpool
Browse files Browse the repository at this point in the history
Allow setting of individual properties from Uri in ConnectionFactory
  • Loading branch information
houseofcat committed Dec 13, 2021
2 parents 36ba079 + e87fd3f commit 590d481
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/HouseofCat.RabbitMQ/Options/FactoryOptions.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
using System;
using RabbitMQ.Client;

namespace HouseofCat.RabbitMQ
{
public class FactoryOptions
{
/// <summary>
/// ConnectionFactory (RabbitMQ) Uri connection string.
/// ConnectionFactory (RabbitMQ) Uri connection string. Set to null to use individual properties.
/// <para>amqp(s)://guest:guest@localhost:5672/vhost</para>
/// </summary>
public Uri Uri { get; set; } = new Uri("amqp://guest:guest@localhost:5672/");

/// <summary>
/// ConnectionFactory (RabbitMQ) virtual host property. Use in lieu of Uri connection string.
/// </summary>
public string VirtualHost { get; set; } = "";

/// <summary>
/// ConnectionFactory (RabbitMQ) username property. Use in lieu of Uri connection string.
/// </summary>
public string UserName { get; set; } = "guest";

/// <summary>
/// ConnectionFactory (RabbitMQ) password property. Use in lieu of Uri connection string.
/// </summary>
public string Password { get; set; } = "guest";

/// <summary>
/// ConnectionFactory (RabbitMQ) host name property. Use in lieu of Uri connection string.
/// </summary>
public string HostName { get; set; } = "localhost";

/// <summary>
/// ConnectionFactory (RabbitMQ) port property. Use in lieu of Uri connection string.
/// </summary>
public int Port { get; set; } = AmqpTcpEndpoint.UseDefaultPort;

/// <summary>
/// ConnectionFactory (RabbitMQ) max connection property.
/// </summary>
Expand Down
17 changes: 16 additions & 1 deletion src/HouseofCat.RabbitMQ/Pools/ConnectionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ private ConnectionFactory CreateConnectionFactory()
{
var cf = new ConnectionFactory
{
Uri = Options.FactoryOptions.Uri,
AutomaticRecoveryEnabled = true,
TopologyRecoveryEnabled = Options.FactoryOptions.TopologyRecovery,
NetworkRecoveryInterval = TimeSpan.FromSeconds(Options.FactoryOptions.NetRecoveryTimeout),
Expand All @@ -60,6 +59,22 @@ private ConnectionFactory CreateConnectionFactory()
DispatchConsumersAsync = Options.FactoryOptions.EnableDispatchConsumersAsync,
};

if (Options.FactoryOptions.Uri != null)
{
cf.Uri = Options.FactoryOptions.Uri;
}
else
{
cf.VirtualHost = Options.FactoryOptions.VirtualHost;
cf.HostName = Options.FactoryOptions.HostName;
cf.UserName = Options.FactoryOptions.UserName;
cf.Password = Options.FactoryOptions.Password;
if (Options.FactoryOptions.Port != AmqpTcpEndpoint.UseDefaultPort)
{
cf.Port = Options.FactoryOptions.Port;
}
}

if (Options.FactoryOptions.SslOptions.EnableSsl)
{
cf.Ssl = new SslOption
Expand Down

0 comments on commit 590d481

Please sign in to comment.