コード例 #1
0
        public void SetUp()
        {
            routingTopology = new ConventionalRoutingTopology();
            receivedMessages = new BlockingCollection<TransportMessage>();

            var config = new ConnectionConfiguration();
            config.ParseHosts("localhost:5672");

            var selectionStrategy = new DefaultClusterHostSelectionStrategy<ConnectionFactoryInfo>();
            var connectionFactory = new ConnectionFactoryWrapper(config, selectionStrategy);
            connectionManager = new RabbitMqConnectionManager(connectionFactory, config);

            unitOfWork = new RabbitMqUnitOfWork
            {
                ConnectionManager = connectionManager,
                UsePublisherConfirms = true,
                MaxWaitTimeForConfirms = TimeSpan.FromSeconds(10)
            };

            sender = new RabbitMqMessageSender
            {
                UnitOfWork = unitOfWork,
                RoutingTopology = routingTopology
            };


            dequeueStrategy = new RabbitMqDequeueStrategy
            {
                ConnectionManager = connectionManager,
                PurgeOnStartup = true
            };

            MakeSureQueueAndExchangeExists(ReceiverQueue);


            MessagePublisher = new RabbitMqMessagePublisher
            {
                UnitOfWork = unitOfWork,
                RoutingTopology = routingTopology
            };
            subscriptionManager = new RabbitMqSubscriptionManager
            {
                ConnectionManager = connectionManager,
                EndpointQueueName = ReceiverQueue,
                RoutingTopology = routingTopology
            };

            dequeueStrategy.Init(Address.Parse(ReceiverQueue), TransactionSettings.Default, m =>
            {
                receivedMessages.Add(m);
                return true;
            }, (s, exception) => { });

            dequeueStrategy.Start(MaximumConcurrency);
        }
コード例 #2
0
        public ConnectionFactoryWrapper(ConnectionConfiguration connectionConfiguration, IClusterHostSelectionStrategy<ConnectionFactoryInfo> clusterHostSelectionStrategy)
        {
            this.clusterHostSelectionStrategy = clusterHostSelectionStrategy;

            Preconditions.CheckNotNull(connectionConfiguration, "connectionConfiguration");
            Preconditions.CheckAny(connectionConfiguration.Hosts, "connectionConfiguration", "At least one host must be defined in connectionConfiguration");

            Configuration = connectionConfiguration;

            foreach (var hostConfiguration in Configuration.Hosts)
            {
                var connectionFactory = new ConnectionFactory
                {
                    UseBackgroundThreadsForIO = connectionConfiguration.UseBackgroundThreads,
                    AutomaticRecoveryEnabled = false,
                    TopologyRecoveryEnabled = false
                };

                if (connectionConfiguration.AMQPConnectionString != null)
                {
                    connectionFactory.uri = connectionConfiguration.AMQPConnectionString;
                }

                connectionFactory.HostName = hostConfiguration.Host;

                if(connectionFactory.VirtualHost == "/")
                    connectionFactory.VirtualHost = Configuration.VirtualHost;

                if(connectionFactory.UserName == "guest")
                    connectionFactory.UserName = Configuration.UserName;

                if(connectionFactory.Password == "guest")
                    connectionFactory.Password = Configuration.Password;

                if (connectionFactory.Port == -1)
                    connectionFactory.Port = hostConfiguration.Port;

                if (hostConfiguration.Ssl.Enabled)
                    connectionFactory.Ssl = hostConfiguration.Ssl;

                //Prefer SSL configurations per each host but fall back to ConnectionConfiguration's SSL configuration for backwards compatibility
                else if (Configuration.Ssl.Enabled)
                    connectionFactory.Ssl = Configuration.Ssl;

                connectionFactory.RequestedHeartbeat = Configuration.RequestedHeartbeat;
                connectionFactory.ClientProperties = Configuration.ClientProperties;
                connectionFactory.AuthMechanisms = Configuration.AuthMechanisms;
                clusterHostSelectionStrategy.Add(new ConnectionFactoryInfo(connectionFactory, hostConfiguration));
            }
        }
コード例 #3
0
ファイル: RabbitHutch.cs プロジェクト: richard-green/EasyNetQ
        /// <summary>
        /// Creates a new instance of RabbitBus
        /// </summary>
        /// <param name="hostName">
        /// The RabbitMQ broker.
        /// </param>
        /// <param name="hostPort">
        /// The RabbitMQ broker port.
        /// </param>
        /// <param name="virtualHost">
        /// The RabbitMQ virtualHost.
        /// </param>
        /// <param name="username">
        /// The username to use to connect to the RabbitMQ broker.
        /// </param>
        /// <param name="password">
        /// The password to use to connect to the RabbitMQ broker.
        /// </param>
        /// <param name="requestedHeartbeat">
        /// The initially requested heartbeat interval, in seconds; zero for none.
        /// </param>
        /// <param name="registerServices">
        /// Override default services. For example, to override the default IEasyNetQLogger:
        /// RabbitHutch.CreateBus("host=localhost", x => x.Register&lt;IEasyNetQLogger&gt;(_ => myLogger));
        /// </param>
        /// <returns>
        /// A new RabbitBus instance.
        /// </returns>
        public static IBus CreateBus(
            string hostName, 
            ushort hostPort, 
            string virtualHost, 
            string username, 
            string password,
            ushort requestedHeartbeat, 
            Action<IServiceRegister> registerServices)
        {
            Preconditions.CheckNotNull(hostName, "hostName");
            Preconditions.CheckNotNull(virtualHost, "virtualHost");
            Preconditions.CheckNotNull(username, "username");
            Preconditions.CheckNotNull(password, "password");
            Preconditions.CheckNotNull(registerServices, "registerServices");

            var connectionConfiguration = new ConnectionConfiguration
            {
                Hosts = new List<IHostConfiguration>
                    {
                        new HostConfiguration { Host = hostName, Port = hostPort }
                    },
                Port = hostPort,
                VirtualHost = virtualHost,
                UserName = username,
                Password = password,
                RequestedHeartbeat = requestedHeartbeat
            };

            return CreateBus(connectionConfiguration, registerServices);
        }
コード例 #4
0
 public MessageDeliveryModeStrategy(ConnectionConfiguration connectionConfiguration)
 {
     Preconditions.CheckNotNull(connectionConfiguration, "connectionConfiguration");
     this.connectionConfiguration = connectionConfiguration;
 }
コード例 #5
0
        public RabbitAdvancedBus(
            IConnectionFactory connectionFactory,
            IConsumerFactory consumerFactory,
            IClientCommandDispatcherFactory clientCommandDispatcherFactory,
            IPublishConfirmationListener confirmationListener,
            IEventBus eventBus,
            IHandlerCollectionFactory handlerCollectionFactory,
            IServiceResolver container,
            ConnectionConfiguration connectionConfiguration,
            IProduceConsumeInterceptor produceConsumeInterceptor,
            IMessageSerializationStrategy messageSerializationStrategy,
            IConventions conventions,
            AdvancedBusEventHandlers advancedBusEventHandlers,
            IPersistentConnectionFactory persistentConnectionFactory)
        {
            Preconditions.CheckNotNull(connectionFactory, "connectionFactory");
            Preconditions.CheckNotNull(consumerFactory, "consumerFactory");
            Preconditions.CheckNotNull(eventBus, "eventBus");
            Preconditions.CheckNotNull(handlerCollectionFactory, "handlerCollectionFactory");
            Preconditions.CheckNotNull(container, "container");
            Preconditions.CheckNotNull(messageSerializationStrategy, "messageSerializationStrategy");
            Preconditions.CheckNotNull(connectionConfiguration, "connectionConfiguration");
            Preconditions.CheckNotNull(produceConsumeInterceptor, "produceConsumeInterceptor");
            Preconditions.CheckNotNull(conventions, "conventions");
            Preconditions.CheckNotNull(advancedBusEventHandlers, "advancedBusEventHandlers");
            Preconditions.CheckNotNull(persistentConnectionFactory, "persistentConnectionFactory");

            this.consumerFactory              = consumerFactory;
            this.confirmationListener         = confirmationListener;
            this.eventBus                     = eventBus;
            this.handlerCollectionFactory     = handlerCollectionFactory;
            this.Container                    = container;
            this.connectionConfiguration      = connectionConfiguration;
            this.produceConsumeInterceptor    = produceConsumeInterceptor;
            this.messageSerializationStrategy = messageSerializationStrategy;
            this.Conventions                  = conventions;

            this.eventBus.Subscribe <ConnectionCreatedEvent>(e => OnConnected());
            if (advancedBusEventHandlers.Connected != null)
            {
                Connected += advancedBusEventHandlers.Connected;
            }
            this.eventBus.Subscribe <ConnectionDisconnectedEvent>(e => OnDisconnected());
            if (advancedBusEventHandlers.Disconnected != null)
            {
                Disconnected += advancedBusEventHandlers.Disconnected;
            }
            this.eventBus.Subscribe <ConnectionBlockedEvent>(OnBlocked);
            if (advancedBusEventHandlers.Blocked != null)
            {
                Blocked += advancedBusEventHandlers.Blocked;
            }
            this.eventBus.Subscribe <ConnectionUnblockedEvent>(e => OnUnblocked());
            if (advancedBusEventHandlers.Unblocked != null)
            {
                Unblocked += advancedBusEventHandlers.Unblocked;
            }
            this.eventBus.Subscribe <ReturnedMessageEvent>(OnMessageReturned);
            if (advancedBusEventHandlers.MessageReturned != null)
            {
                MessageReturned += advancedBusEventHandlers.MessageReturned;
            }

            connection = persistentConnectionFactory.CreateConnection();
            clientCommandDispatcher = clientCommandDispatcherFactory.GetClientCommandDispatcher(connection);
            connection.Initialize();
        }
コード例 #6
0
 public MessageDeliveryModeStrategy(ConnectionConfiguration connectionConfiguration)
 {
     Preconditions.CheckNotNull(connectionConfiguration, "connectionConfiguration");
     this.connectionConfiguration = connectionConfiguration;
 }
コード例 #7
0
        /// <summary>
        /// Creates a new instance of <see cref="RabbitBus"/>.
        /// </summary>
        /// <param name="connectionConfiguration">
        /// An <see cref="ConnectionConfiguration"/> instance.
        /// </param>
        /// <param name="registerServices">
        /// Override default services. For example, to override the default <see cref="ISerializer"/>:
        /// RabbitHutch.CreateBus("host=localhost", x => x.Register{ISerializer}(mySerializer));
        /// </param>
        /// <returns>
        /// A new <see cref="RabbitBus"/> instance.
        /// </returns>
        public static IBus CreateBus(ConnectionConfiguration connectionConfiguration, Action <IServiceRegister> registerServices)
        {
            Preconditions.CheckNotNull(connectionConfiguration, "connectionConfiguration");

            return(CreateBus(_ => connectionConfiguration, registerServices));
        }
コード例 #8
0
ファイル: IConnectionFactory.cs プロジェクト: nurhat/EasyNetQ
        public ConnectionFactoryWrapper(ConnectionConfiguration connectionConfiguration, IClusterHostSelectionStrategy <ConnectionFactoryInfo> clusterHostSelectionStrategy)
        {
            this.clusterHostSelectionStrategy = clusterHostSelectionStrategy;

            Preconditions.CheckNotNull(connectionConfiguration, "connectionConfiguration");
            Preconditions.CheckAny(connectionConfiguration.Hosts, "connectionConfiguration", "At least one host must be defined in connectionConfiguration");

            Configuration = connectionConfiguration;

            foreach (var hostConfiguration in Configuration.Hosts)
            {
                var connectionFactory = new ConnectionFactory
                {
                    UseBackgroundThreadsForIO = false,
                    AutomaticRecoveryEnabled  = false,
                    TopologyRecoveryEnabled   = false
                };

                if (connectionConfiguration.AMQPConnectionString != null)
                {
                    connectionFactory.uri = connectionConfiguration.AMQPConnectionString;
                }

                connectionFactory.HostName = hostConfiguration.Host;

                if (connectionFactory.VirtualHost == "/")
                {
                    connectionFactory.VirtualHost = Configuration.VirtualHost;
                }

                if (connectionFactory.UserName == "guest")
                {
                    connectionFactory.UserName = Configuration.UserName;
                }

                if (connectionFactory.Password == "guest")
                {
                    connectionFactory.Password = Configuration.Password;
                }

                if (connectionFactory.Port == -1)
                {
                    connectionFactory.Port = hostConfiguration.Port;
                }

                if (hostConfiguration.Ssl.Enabled)
                {
                    connectionFactory.Ssl = hostConfiguration.Ssl;
                }

                //Prefer SSL configurations per each host but fall back to ConnectionConfiguration's SSL configuration for backwards compatibility
                else if (Configuration.Ssl.Enabled)
                {
                    connectionFactory.Ssl = Configuration.Ssl;
                }

                connectionFactory.RequestedHeartbeat = Configuration.RequestedHeartbeat;
                connectionFactory.ClientProperties   = Configuration.ClientProperties;
                clusterHostSelectionStrategy.Add(new ConnectionFactoryInfo(connectionFactory, hostConfiguration));
            }
        }
コード例 #9
0
ファイル: RabbitHutch.cs プロジェクト: radicalgeek/EasyNetQ
        /// <summary>
        /// Creates a new instance of <see cref="RabbitBus"/>.
        /// </summary>
        /// <param name="connectionConfiguration">
        /// An <see cref="ConnectionConfiguration"/> instance.
        /// </param>
        /// <param name="advancedBusEventHandlers">
        /// An <see cref="AdvancedBusEventHandlers"/> instance which is used to add handlers
        /// to the events of the newly created <see cref="IBus.Advanced"/>.
        /// As <see cref="RabbitAdvancedBus"/> attempts to connect during instantiation, specifying a <see cref="AdvancedBusEventHandlers"/>
        /// before instantiation is the only way to catch the first <see cref="AdvancedBusEventHandlers.Connected"/> event.
        /// </param>
        /// <param name="registerServices">
        /// Override default services. For example, to override the default <see cref="IEasyNetQLogger"/>:
        /// RabbitHutch.CreateBus("host=localhost", x => x.Register{IEasyNetQLogger}(_ => myLogger));
        /// </param>
        /// <returns>
        /// A new <see cref="RabbitBus"/> instance.
        /// </returns>
        public static IBus CreateBus(ConnectionConfiguration connectionConfiguration, AdvancedBusEventHandlers advancedBusEventHandlers, Action<IServiceRegister> registerServices)
        {
            Preconditions.CheckNotNull(connectionConfiguration, "connectionConfiguration");
            Preconditions.CheckNotNull(advancedBusEventHandlers, "advancedBusEventHandlers");
            Preconditions.CheckNotNull(registerServices, "registerServices");

            var container = createContainerInternal();
            if (container == null)
            {
                throw new EasyNetQException("Could not create container. " +
                    "Have you called SetContainerFactory(...) with a function that returns null?");
            }

            connectionConfiguration.Validate();
            container.Register(_ => connectionConfiguration);
            container.Register(_ => advancedBusEventHandlers);
            registerServices(container);
            ComponentRegistration.RegisterServices(container);

            return container.Resolve<IBus>();
        }
コード例 #10
0
ファイル: RabbitHutch.cs プロジェクト: radicalgeek/EasyNetQ
        /// <summary>
        /// Creates a new instance of <see cref="RabbitBus"/>.
        /// </summary>
        /// <param name="connectionConfiguration">
        /// An <see cref="ConnectionConfiguration"/> instance.
        /// </param>
        /// <param name="registerServices">
        /// Override default services. For example, to override the default <see cref="IEasyNetQLogger"/>:
        /// RabbitHutch.CreateBus("host=localhost", x => x.Register{IEasyNetQLogger}(_ => myLogger));
        /// </param>
        /// <returns>
        /// A new <see cref="RabbitBus"/> instance.
        /// </returns>
        public static IBus CreateBus(ConnectionConfiguration connectionConfiguration, Action<IServiceRegister> registerServices)
        {
            Preconditions.CheckNotNull(connectionConfiguration, "connectionConfiguration");
            Preconditions.CheckNotNull(registerServices, "registerServices");

            return CreateBus(connectionConfiguration, AdvancedBusEventHandlers.Default, registerServices);
        }
コード例 #11
0
 public TimeoutStrategy(ConnectionConfiguration connectionConfiguration)
 {
     Preconditions.CheckNotNull(connectionConfiguration, "connectionConfiguration");
     this.connectionConfiguration = connectionConfiguration;
 }
コード例 #12
0
ファイル: ITimeoutStrategy.cs プロジェクト: hippasus/EasyNetQ
 public TimeoutStrategy(ConnectionConfiguration connectionConfiguration)
 {
     Preconditions.CheckNotNull(connectionConfiguration, "connectionConfiguration");
     this.connectionConfiguration = connectionConfiguration;
 }
コード例 #13
0
        public static void SetDefaultProperties(this ConnectionConfiguration configuration)
        {
            Preconditions.CheckNotNull(configuration, "configuration");

            if (
                configuration.AmqpConnectionString != null &&
                configuration.Hosts.All(h => h.Host != configuration.AmqpConnectionString.Host)
                )
            {
                if (configuration.Port == ConnectionConfiguration.DefaultPort)
                {
                    if (configuration.AmqpConnectionString.Port > 0)
                    {
                        configuration.Port = (ushort)configuration.AmqpConnectionString.Port;
                    }
                    else if (
                        configuration.AmqpConnectionString.Scheme.Equals("amqps", StringComparison.OrdinalIgnoreCase)
                        )
                    {
                        configuration.Port = ConnectionConfiguration.DefaultAmqpsPort;
                    }
                }

                if (configuration.AmqpConnectionString.Segments.Length > 1)
                {
                    configuration.VirtualHost = configuration.AmqpConnectionString.Segments.Last();
                }

                configuration.Hosts.Add(new HostConfiguration {
                    Host = configuration.AmqpConnectionString.Host
                });
            }

            if (configuration.Hosts.Count == 0)
            {
                throw new EasyNetQException(
                          "Invalid connection string. 'host' value must be supplied. e.g: \"host=myserver\""
                          );
            }

            foreach (var hostConfiguration in configuration.Hosts)
            {
                if (hostConfiguration.Port == 0)
                {
                    hostConfiguration.Port = configuration.Port;
                }
            }

#if !NETFX
            var version = typeof(ConnectionConfigurationExtensions).GetTypeInfo().Assembly.GetName().Version.ToString();
#else
            var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
#endif
            var applicationNameAndPath = Environment.GetCommandLineArgs()[0];

            var applicationName = "unknown";
            var applicationPath = "unknown";
            if (!string.IsNullOrWhiteSpace(applicationNameAndPath))
            {
                try
                {
                    // Will only throw an exception if the applicationName contains invalid characters, is empty, or too long
                    // Silently catch the exception, as we will just leave the application name and path to "unknown"
                    applicationName = Path.GetFileName(applicationNameAndPath);
                    applicationPath = Path.GetDirectoryName(applicationNameAndPath);
                }
                catch (ArgumentException)
                {
                }
            }
コード例 #14
0
 public static TimeSpan GetTimeout(this ConnectionConfiguration configuration)
 {
     return(configuration.Timeout == 0 ? Timeout.InfiniteTimeSpan : TimeSpan.FromSeconds(configuration.Timeout));
 }
コード例 #15
0
ファイル: RabbitHutch.cs プロジェクト: nslowes/EasyNetQ
        /// <summary>
        /// Creates a new instance of RabbitBus
        /// </summary>
        /// <param name="hostName">
        /// The RabbitMQ broker.
        /// </param>
        /// <param name="hostPort">
        /// The RabbitMQ broker port.
        /// </param>
        /// <param name="virtualHost">
        /// The RabbitMQ virtualHost.
        /// </param>
        /// <param name="username">
        /// The username to use to connect to the RabbitMQ broker.
        /// </param>
        /// <param name="password">
        /// The password to use to connect to the RabbitMQ broker.
        /// </param>
        /// <param name="requestedHeartbeat">
        /// The initially requested heartbeat interval, in seconds; zero for none.
        /// </param>
        /// <param name="registerServices">
        /// Override default services. For example, to override the default IEasyNetQLogger:
        /// RabbitHutch.CreateBus("host=localhost", x => x.Register&lt;IEasyNetQLogger&gt;(_ => myLogger));
        /// </param>
        /// <returns>
        /// A new RabbitBus instance.
        /// </returns>
        public static IBus CreateBus(
            string hostName, 
            ushort hostPort, 
            string virtualHost, 
            string username, 
            string password,
            ushort requestedHeartbeat, 
            Action<IServiceRegister> registerServices)
        {
            if(hostName == null)
            {
                throw new ArgumentNullException("hostName");
            }
            if(virtualHost == null)
            {
                throw new ArgumentNullException("virtualHost");
            }
            if(username == null)
            {
                throw new ArgumentNullException("username");
            }
            if(password == null)
            {
                throw new ArgumentNullException("password");
            }
            if(registerServices == null)
            {
                throw new ArgumentNullException("registerServices");
            }

            var connectionConfiguration = new ConnectionConfiguration
            {
                Hosts = new List<IHostConfiguration>
                    {
                        new HostConfiguration { Host = hostName, Port = hostPort }
                    },
                Port = hostPort,
                VirtualHost = virtualHost,
                UserName = username,
                Password = password,
                RequestedHeartbeat = requestedHeartbeat
            };

            return CreateBus(connectionConfiguration, registerServices);
        }