bool CheckQueueExistence(FullyQualifiedRoutingKey routingKey, IModel model)
        {
            var queueName = routingKey.RoutingKey;

            try
            {
                // Checks if queue exists, throws if the queue doesn't exist
                model.QueueDeclarePassive(queueName);
            }
            catch (Exception e)
            {
                throw new RebusApplicationException(e, $"Queue '{queueName}' does not exist");
            }

            var exchange = routingKey.ExchangeName ?? _directExchangeName;

            try
            {
                model.ExchangeDeclarePassive(exchange);
            }
            catch (Exception e)
            {
                throw new RebusApplicationException(e, $"Exchange '{exchange}' does not exist");
            }

            // since we can't check whether a binding exists, we need
            // to proactively declare it, so we don't risk sending a message
            // "into the void":
            model.QueueBind(queueName, exchange, routingKey.RoutingKey);

            return(true);
        }
        async Task SendOutgoingMessages(ITransactionContext context, ConcurrentQueue <OutgoingMessage> outgoingMessages)
        {
            var model = GetModel(context);

            foreach (var outgoingMessage in outgoingMessages)
            {
                var destinationAddress = outgoingMessage.DestinationAddress;
                var message            = outgoingMessage.TransportMessage;
                var props = CreateBasicProperties(model, message.Headers);

                var mandatory = message.Headers.ContainsKey(RabbitMqHeaders.Mandatory);
                if (mandatory && !_callbackOptions.HasMandatoryCallback)
                {
                    throw new MandatoryDeliveryException("Mandatory delivery is not allowed without registrering a handler for BasicReturn in RabbitMqOptions.");
                }

                var routingKey = new FullyQualifiedRoutingKey(destinationAddress);
                var exchange   = routingKey.ExchangeName ?? _directExchangeName;

                // when we're sending point-to-point, we want to be sure that we are never sending the message out into nowhere
                if (exchange == _directExchangeName && !_callbackOptions.HasMandatoryCallback)
                {
                    try
                    {
                        EnsureQueueExists(destinationAddress, model);
                    }
                    catch (Exception e)
                    {
                        throw new RebusApplicationException(e, $"Queue '{destinationAddress}' does not exists.");
                    }
                }

                model.BasicPublish(exchange, routingKey.RoutingKey, mandatory, props, message.Body);
            }
        }
Example #3
0
        async Task SendOutgoingMessages(ITransactionContext context, ConcurrentQueue <OutgoingMessage> outgoingMessages)
        {
            var model = GetModel(context);

            foreach (var outgoingMessage in outgoingMessages)
            {
                var destinationAddress = outgoingMessage.DestinationAddress;
                var message            = outgoingMessage.TransportMessage;
                var props             = model.CreateBasicProperties();
                var headers           = message.Headers;
                var timeToBeDelivered = GetTimeToBeReceivedOrNull(message);

                props.Headers = headers
                                .ToDictionary(kvp => kvp.Key, kvp => (object)HeaderValueEncoding.GetBytes(kvp.Value));

                if (timeToBeDelivered.HasValue)
                {
                    props.Expiration = timeToBeDelivered.Value.TotalMilliseconds.ToString("0");
                }

                var express = headers.ContainsKey(Headers.Express);

                props.Persistent = !express;

                var routingKey = new FullyQualifiedRoutingKey(destinationAddress);

                model.BasicPublish(routingKey.ExchangeName, routingKey.RoutingKey, props, message.Body);
            }
        }
Example #4
0
        public void CanDetermineEquality()
        {
            const string destinationAddress = "wherever@whatever";

            var routingKey1 = new FullyQualifiedRoutingKey(destinationAddress);
            var routingKey2 = new FullyQualifiedRoutingKey(destinationAddress);

            Assert.That(routingKey1, Is.EqualTo(routingKey2));
        }
Example #5
0
        async Task SendOutgoingMessages(ITransactionContext context, ConcurrentQueue <OutgoingMessage> outgoingMessages)
        {
            var model = GetModel(context);

            if (_publisherConfirmsEnabled)
            {
                model.ConfirmSelect();
            }

            foreach (var outgoingMessage in outgoingMessages)
            {
                var destinationAddress = outgoingMessage.DestinationAddress;
                var message            = outgoingMessage.TransportMessage;
                var props = CreateBasicProperties(model, message.Headers);

                var mandatory = message.Headers.ContainsKey(RabbitMqHeaders.Mandatory);
                if (mandatory && !_callbackOptions.HasMandatoryCallback)
                {
                    throw new MandatoryDeliveryException("Mandatory delivery is not allowed without registering a handler for BasicReturn in RabbitMqOptions.");
                }

                var routingKey = new FullyQualifiedRoutingKey(destinationAddress);
                var exchange   = routingKey.ExchangeName ?? _directExchangeName;

                var isPointToPoint = message.Headers.TryGetValue(Headers.Intent, out var intent) &&
                                     intent == Headers.IntentOptions.PointToPoint;

                // when we're sending point-to-point, we want to be sure that we are never sending the message out into nowhere
                if (isPointToPoint && !_callbackOptions.HasMandatoryCallback)
                {
                    EnsureQueueExists(routingKey, model);
                }

                model.BasicPublish(
                    exchange: exchange,
                    routingKey: routingKey.RoutingKey,
                    mandatory: mandatory,
                    basicProperties: props,
                    body: message.Body
                    );
            }

            if (_publisherConfirmsEnabled)
            {
                model.WaitForConfirmsOrDie();
            }
        }
        async Task SendOutgoingMessages(ITransactionContext context, ConcurrentQueue <OutgoingMessage> outgoingMessages)
        {
            var model = GetModel(context);

            foreach (var outgoingMessage in outgoingMessages)
            {
                var destinationAddress = outgoingMessage.DestinationAddress;
                var message            = outgoingMessage.TransportMessage;
                var props             = model.CreateBasicProperties();
                var headers           = message.Headers;
                var timeToBeDelivered = GetTimeToBeReceivedOrNull(message);

                props.Headers = headers
                                .ToDictionary(kvp => kvp.Key, kvp => (object)HeaderValueEncoding.GetBytes(kvp.Value));

                if (timeToBeDelivered.HasValue)
                {
                    props.Expiration = timeToBeDelivered.Value.TotalMilliseconds.ToString("0");
                }

                var express = headers.ContainsKey(Headers.Express);

                props.Persistent = !express;

                var routingKey = new FullyQualifiedRoutingKey(destinationAddress);
                var exchange   = routingKey.ExchangeName ?? _directExchangeName;

                // when we're sending point-to-point, we want to be sure that we are never sending the message out into nowhere
                if (exchange == _directExchangeName)
                {
                    EnsureQueueIsInitialized(destinationAddress, model);
                }

                model.BasicPublish(exchange, routingKey.RoutingKey, props, message.Body);
            }
        }
 void EnsureQueueExists(FullyQualifiedRoutingKey routingKey, IModel model)
 {
     _verifiedQueues
     .GetOrAdd(routingKey, _ => CheckQueueExistence(routingKey, model));
 }
Example #8
0
        async Task SendOutgoingMessages(ITransactionContext context, ConcurrentQueue<OutgoingMessage> outgoingMessages)
        {
            var model = GetModel(context);

            foreach (var outgoingMessage in outgoingMessages)
            {
                var destinationAddress = outgoingMessage.DestinationAddress;
                var message = outgoingMessage.TransportMessage;
                var props = model.CreateBasicProperties();
                var headers = message.Headers;
                var timeToBeDelivered = GetTimeToBeReceivedOrNull(message);

                props.Headers = headers
                    .ToDictionary(kvp => kvp.Key, kvp => (object)HeaderValueEncoding.GetBytes(kvp.Value));

                if (timeToBeDelivered.HasValue)
                {
                    props.Expiration = timeToBeDelivered.Value.TotalMilliseconds.ToString("0");
                }

                var express = headers.ContainsKey(Headers.Express);

                props.Persistent = !express;

                var routingKey = new FullyQualifiedRoutingKey(destinationAddress);
                var exchange = routingKey.ExchangeName ?? _directExchangeName;

                // when we're sending point-to-point, we want to be sure that we are never sending the message out into nowhere
                if (exchange == _directExchangeName)
                {
                    EnsureQueueIsInitialized(destinationAddress, model);
                }

                model.BasicPublish(exchange, routingKey.RoutingKey, props, message.Body);
            }
        }
Example #9
0
        async Task SendOutgoingMessages(ITransactionContext context, ConcurrentQueue<OutgoingMessage> outgoingMessages)
        {
            var model = GetModel(context);

            foreach (var outgoingMessage in outgoingMessages)
            {
                var destinationAddress = outgoingMessage.DestinationAddress;
                var message = outgoingMessage.TransportMessage;
                var props = model.CreateBasicProperties();
                var headers = message.Headers;
                var timeToBeDelivered = GetTimeToBeReceivedOrNull(message);

                props.Headers = headers
                    .ToDictionary(kvp => kvp.Key, kvp => (object)HeaderValueEncoding.GetBytes(kvp.Value));

                if (timeToBeDelivered.HasValue)
                {
                    props.Expiration = timeToBeDelivered.Value.TotalMilliseconds.ToString("0");
                }

                var express = headers.ContainsKey(Headers.Express);

                props.Persistent = !express;

                var routingKey = new FullyQualifiedRoutingKey(destinationAddress);

                model.BasicPublish(routingKey.ExchangeName, routingKey.RoutingKey, props, message.Body);
            }
        }