示例#1
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="settings">The message queue cluster settings.</param>
        /// <param name="username">Optional username (overrides <see cref="HiveMQSettings.Username"/>).</param>
        /// <param name="password">Optional password (overrides <see cref="HiveMQSettings.Password"/>).</param>
        /// <param name="virtualHost">Optional target virtual host (overrides <see cref="HiveMQSettings.VirtualHost"/>).</param>
        public HiveBus(HiveMQSettings settings, string username = null, string password = null, string virtualHost = null)
        {
            Covenant.Requires <ArgumentNullException>(settings != null);

            var busSettings = new EasyBusSettings()
            {
                Client = clientVersion
            };

            this.EasyBus = settings.ConnectEasyNetQ(username, password, virtualHost, busSettings);
        }
示例#2
0
        /// <summary>
        /// Returns an <see cref="HiveBus"/> instance that provides more advanced
        /// capabilites over the very simple <b>EasyNetQ</b> capabilities returned by
        /// <see cref="ConnectEasyNetQ(string, string, string, EasyBusSettings, Action{IServiceRegister})"/>
        /// while still being very easy to use.
        /// </summary>
        /// <param name="username">Optional username (overrides <see cref="Username"/>).</param>
        /// <param name="password">Optional password (overrides <see cref="Password"/>).</param>
        /// <param name="virtualHost">Optional target virtual host (overrides <see cref="VirtualHost"/>).</param>
        /// <param name="settings">Optional message bus client settings.</param>
        /// <returns></returns>
        public HiveBus ConnectHiveBus(
            string username          = null,
            string password          = null,
            string virtualHost       = null,
            EasyBusSettings settings = null)
        {
            username    = username ?? Username;
            password    = password ?? Password;
            virtualHost = virtualHost ?? VirtualHost;

            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException($"[{nameof(username)}] is required.");
            }

            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException($"[{nameof(password)}] is required.");
            }

            return(new HiveBus(this, username, password, virtualHost));
        }
示例#3
0
        /// <summary>
        /// Returns an EasyNetQ <see cref="IBus"/> connection to a RabbitMQ cluster.
        /// </summary>
        /// <param name="username">Optional username (overrides <see cref="Username"/>).</param>
        /// <param name="password">Optional password (overrides <see cref="Password"/>).</param>
        /// <param name="virtualHost">Optional target virtual host (defaults to <b>"/"</b>).</param>
        /// <param name="busSettings">Optional message bus client settings.</param>
        /// <param name="customServiceAction">
        /// Optionally specifies an action that overrides the default the EasyNetQ
        /// client configuration via dependency injection.
        /// </param>
        /// <returns>The connected <see cref="IBus"/>.</returns>
        public IBus ConnectEasyNetQ(
            string username             = null,
            string password             = null,
            string virtualHost          = "/",
            EasyBusSettings busSettings = null,
            Action <IServiceRegister> customServiceAction = null)
        {
            Covenant.Requires <NotImplementedException>(!TlsEnabled, "$todo(jeff.lill): We don't support RabbitMQ TLS yet.");

            var config = new ConnectionConfiguration()
            {
                Port        = (ushort)AmqpPort,
                UserName    = username ?? Username,
                Password    = password ?? Password,
                VirtualHost = virtualHost
            };

            if (busSettings != null)
            {
                busSettings.ApplyTo(config);
            }

            if (string.IsNullOrEmpty(config.UserName))
            {
                throw new ArgumentNullException($"[{nameof(username)}] is required.");
            }

            if (string.IsNullOrEmpty(config.Password))
            {
                throw new ArgumentNullException($"[{nameof(password)}] is required.");
            }

            var hostConfigs = new List <HostConfiguration>();

            foreach (var host in AmqpHosts)
            {
                hostConfigs.Add(new HostConfiguration()
                {
                    Host = host, Port = (ushort)AmqpPort
                });
            }

            config.Hosts = hostConfigs;

            // Enable Neon based logging if requested (which is the default).

            if (NeonLog)
            {
                // Generate a reasonable [sourceModule] setting.

                var sourceModule = "EasyNetQ";
                var product      = busSettings?.Product ?? string.Empty;
                var appName      = busSettings?.Name ?? string.Empty;

                if (!string.IsNullOrEmpty(product) || !string.IsNullOrEmpty(appName))
                {
                    if (string.IsNullOrEmpty(product))
                    {
                        sourceModule = appName;
                    }
                    else if (string.IsNullOrEmpty(appName))
                    {
                        sourceModule = product;
                    }
                    else if (product != appName)
                    {
                        sourceModule = $"{product}/{appName}";
                    }
                    else
                    {
                        sourceModule = appName;
                    }
                }
                else
                {
                    // We're going to try to default to the current executable name
                    // for the source module.  Note that it's possible that we can't
                    // obtain this name in some situations, e.g. when running
                    // on Integration Services Package (SSIS).  In those cases,
                    // this will default to "EasyNetQ".

                    var appPath = Environment.GetCommandLineArgs()[0];

                    if (!string.IsNullOrWhiteSpace(appPath))
                    {
                        sourceModule = Path.GetFileNameWithoutExtension(appPath);
                    }
                }

                var neonLogger      = LogManager.Default.GetLogger(sourceModule);
                var neonLogProvider = new HiveEasyMQLogProvider(neonLogger);

                LogProvider.SetCurrentLogProvider(neonLogProvider);
            }

            if (customServiceAction == null)
            {
                // Use a NOP service action.

                customServiceAction = r => { };
            }

            var bus = RabbitHutch.CreateBus(config, customServiceAction);

            if (!bus.IsConnected)
            {
                bus.Dispose();

                throw new Exception("Cannot to connect to RabbitMQ.");
            }

            return(bus);
        }