Esempio n. 1
0
        private IConnection OpenConnection(RabbitMQUri uri, IProtocol protocol)
        {
            int port = uri.Port.HasValue ? uri.Port.Value : protocol.DefaultPort;

            ConnectionFactory connFactory = new ConnectionFactory
                {
                    AutomaticRecoveryEnabled = true,
                    HostName = uri.Host,
                    Port = port,
                    Protocol = protocol
                };

            if (uri.Username != null)
            {
                connFactory.UserName = uri.Username;
            }
            if (uri.Password != null)
            {
                connFactory.Password = uri.Password;
            }
            if (uri.VirtualHost != null)
            {
                connFactory.VirtualHost = uri.VirtualHost;
            }

            return connFactory.CreateConnection();
        }
Esempio n. 2
0
        private IConnection OpenConnection(RabbitMQUri uri, IProtocol protocol)
        {
            int port = uri.Port.HasValue ? uri.Port.Value : protocol.DefaultPort;

            ConnectionFactory connFactory = new ConnectionFactory
            {
                AutomaticRecoveryEnabled = true,
                HostName = uri.Host,
                Port     = port,
                Protocol = protocol
            };

            if (uri.Username != null)
            {
                connFactory.UserName = uri.Username;
            }
            if (uri.Password != null)
            {
                connFactory.Password = uri.Password;
            }
            if (uri.VirtualHost != null)
            {
                connFactory.VirtualHost = uri.VirtualHost;
            }

            return(connFactory.CreateConnection());
        }
Esempio n. 3
0
        public override void Send(Message message, TimeSpan timeout)
        {
            if (State == CommunicationState.Opened && !message.IsFault)
            {
                byte[] body;

                IBasicProperties basicProperties = _model.CreateBasicProperties();

                // Set message properties
                SetMessageProperties(basicProperties);

                // Read custom headers and put it into the message headers
                SetMessageHeaders(message, basicProperties);

                // Set ReplyTo address if specified
                if (_bindingElement.ReplyToExchange != null)
                {
                    message.Headers.ReplyTo = new EndpointAddress(_bindingElement.ReplyToExchange);
                }

#if VERBOSE
                DebugHelper.Start();
#endif
                // Serialize the message to stream
                using (MemoryStream str = new MemoryStream())
                {
                    _encoder.WriteMessage(message, str);
                    body = str.ToArray();
                }

#if VERBOSE
                DebugHelper.Stop(" #### Message.Serialize {{\n\tAction={2}, \n\tBytes={1}, \n\tTime={0}ms}}.",
                                 body.Length,
                                 message.Headers.Action);
#endif

                // Build AMQP uri
                RabbitMQUri uri = new RabbitMQUri(RemoteAddress.Uri);

                // Enlist operation into transaction
                EnlistTransaction();

#if VERBOSE
                DebugHelper.Start();
#endif
                // Publish AMQP message
                _model.BasicPublish(uri.Endpoint,
                                    uri.RoutingKey,
                                    _bindingElement.Mandatory,
                                    _bindingElement.Immediate,
                                    basicProperties,
                                    body);

#if VERBOSE
                DebugHelper.Stop(" #### Message.Publish {{\n\tAction={2}, \n\tBytes={1}, \n\tTime={0}ms}}.",
                                 body.Length,
                                 message.Headers.Action);
#endif
            }
        }
Esempio n. 4
0
        public IModel OpenModel(RabbitMQUri uri, IProtocol protocol, TimeSpan timeout)
        {
            IConnection connection = OpenConnection(uri, protocol);

            IModel model = connection.CreateModel();

            connection.AutoClose = true;

            return model;
        }
Esempio n. 5
0
        public IModel OpenModel(RabbitMQUri uri, IProtocol protocol, TimeSpan timeout)
        {
            IConnection connection = OpenConnection(uri, protocol);

            IModel model = connection.CreateModel();

            connection.AutoClose = true;

            return(model);
        }
        public override void Open(TimeSpan timeout)
        {
            if (State != CommunicationState.Created && State != CommunicationState.Closed)
            {
                throw new InvalidOperationException(string.Format("Cannot open the channel from the {0} state.", State));
            }

            OnOpening();
#if VERBOSE
            DebugHelper.Start();
#endif
            RabbitMQUri uri = new RabbitMQUri(LocalAddress.Uri);

            _model = ConnectionManager.Instance.OpenModel(uri, _bindingElement.BrokerProtocol, timeout);

            _queueName = uri.Endpoint;

            IDictionary <String, Object> args = new Dictionary <String, Object>();

            int ttl;
            if (!string.IsNullOrEmpty(_bindingElement.TTL) && int.TryParse(_bindingElement.TTL, out ttl))
            {
                args.Add("x-message-ttl", ttl);
            }

            //Create a queue for messages destined to this service, bind it to the service URI routing key
            bool autoDelete = string.IsNullOrEmpty(_queueName) || _bindingElement.AutoDelete;

            _queueName = _model.QueueDeclare(_queueName ?? "", true, autoDelete, autoDelete, args);

            if (!string.IsNullOrEmpty(_bindToExchange))
            {
                _model.QueueBind(_queueName, _bindToExchange, uri.RoutingKey);
            }

            // Create receiver
            if (_bindingElement.TransactedReceiveEnabled)
            {
                _messageReceiver = new TransactionalMessageReceiver(_model, _queueName);
            }
            else
            {
                _messageReceiver = new NoAckMessageReceiver(_model, _queueName);
            }

#if VERBOSE
            DebugHelper.Stop(" ## In.Channel.Open {{\n\tAddress={1}, \n\tTime={0}ms}}.", LocalAddress.Uri.PathAndQuery);
#endif
            OnOpened();
        }
Esempio n. 7
0
        public override IChannelListener <TChannel> BuildChannelListener <TChannel>(BindingContext context)
        {
            string bindToExchange = AutoBindExchange;

            if (context.ListenUriBaseAddress == null)
            {
                if (ReplyToExchange == null)
                {
                    return(null);
                }

                RabbitMQUri uri = new RabbitMQUri(ReplyToExchange);

                context.ListenUriMode = ListenUriMode.Explicit;

                context.ListenUriRelativeAddress = ReplyToQueue;

                if (ReplyToQueue == null)
                {
                    bindToExchange = uri.Endpoint;
                }

                if (uri.Port.HasValue)
                {
                    context.ListenUriBaseAddress = new Uri(string.Format("{0}://{1}:{2}/", Scheme, uri.Host, uri.Port));
                }
                else
                {
                    context.ListenUriBaseAddress = new Uri(string.Format("{0}://{1}/", Scheme, uri.Host));
                }
            }

            Uri listenUri = new Uri(context.ListenUriBaseAddress, context.ListenUriRelativeAddress ?? "");

            return(new RabbitMQTransportChannelListener <TChannel>(context, listenUri, bindToExchange));
        }
        public override void Send(Message message, TimeSpan timeout)
        {
            if (State == CommunicationState.Opened && !message.IsFault)
            {
                byte[] body;

                IBasicProperties basicProperties = _model.CreateBasicProperties();

                // Set message properties
                SetMessageProperties(basicProperties);

                // Read custom headers and put it into the message headers
                SetMessageHeaders(message, basicProperties);

                // Set ReplyTo address if specified
                if (_bindingElement.ReplyToExchange != null)
                {
                    message.Headers.ReplyTo = new EndpointAddress(_bindingElement.ReplyToExchange);
                }

            #if VERBOSE
                DebugHelper.Start();
            #endif
                // Serialize the message to stream
                using (MemoryStream str = new MemoryStream())
                {
                    _encoder.WriteMessage(message, str);
                    body = str.ToArray();
                }

            #if VERBOSE
                DebugHelper.Stop(" #### Message.Serialize {{\n\tAction={2}, \n\tBytes={1}, \n\tTime={0}ms}}.",
                    body.Length,
                    message.Headers.Action);
            #endif

                // Build AMQP uri
                RabbitMQUri uri = new RabbitMQUri(RemoteAddress.Uri);

                // Enlist operation into transaction
                EnlistTransaction();

            #if VERBOSE
                DebugHelper.Start();
            #endif
                // Publish AMQP message
                _model.BasicPublish(uri.Endpoint,
                                     uri.RoutingKey,
                                     _bindingElement.Mandatory,
                                     basicProperties,
                                     body);

            #if VERBOSE
                DebugHelper.Stop(" #### Message.Publish {{\n\tAction={2}, \n\tBytes={1}, \n\tTime={0}ms}}.",
                    body.Length,
                    message.Headers.Action);
            #endif
            }
        }
        public override void Open(TimeSpan timeout)
        {
            if (State != CommunicationState.Created && State != CommunicationState.Closed)
                throw new InvalidOperationException(string.Format("Cannot open the channel from the {0} state.", State));

            OnOpening();
            #if VERBOSE
            DebugHelper.Start();
            #endif
            RabbitMQUri uri = new RabbitMQUri(LocalAddress.Uri);

            _model = ConnectionManager.Instance.OpenModel(uri, _bindingElement.BrokerProtocol, timeout);

            _queueName = uri.Endpoint;

            IDictionary<String, Object> args = new Dictionary<String, Object>();

            int ttl;
            if (!string.IsNullOrEmpty(_bindingElement.TTL) && int.TryParse(_bindingElement.TTL, out ttl))
            {
                args.Add("x-message-ttl", ttl);
            }

            //Create a queue for messages destined to this service, bind it to the service URI routing key
            bool autoDelete = string.IsNullOrEmpty(_queueName) || _bindingElement.AutoDelete;

            _queueName = _model.QueueDeclare(_queueName ?? "", true, autoDelete, autoDelete, args);

            if (!string.IsNullOrEmpty(_bindToExchange))
            {
                _model.QueueBind(_queueName, _bindToExchange, uri.RoutingKey);
            }

            // Create receiver
            if (_bindingElement.TransactedReceiveEnabled)
            {
                _messageReceiver = new TransactionalMessageReceiver(_model, _queueName);
            }
            else
            {
                _messageReceiver = new NoAckMessageReceiver(_model, _queueName);
            }

            #if VERBOSE
            DebugHelper.Stop(" ## In.Channel.Open {{\n\tAddress={1}, \n\tTime={0}ms}}.", LocalAddress.Uri.PathAndQuery);
            #endif
            OnOpened();
        }