Ejemplo n.º 1
0
        public void TestVerifyPopulatedChecks()
        {
            var producerOptions = new ProducerOptions();

            Assert.False(producerOptions.VerifyPopulated());

            producerOptions.ExchangeName = "";
            Assert.False(producerOptions.VerifyPopulated());

            producerOptions.ExchangeName = "Test.ExchangeName";
            Assert.True(producerOptions.VerifyPopulated());

            var consumerOptions = new ConsumerOptions();

            Assert.False(consumerOptions.VerifyPopulated());

            consumerOptions.QueueName = "Test.QueueName";
            Assert.False(consumerOptions.VerifyPopulated());

            consumerOptions.QoSPrefetchCount = 1234;
            Assert.True(consumerOptions.VerifyPopulated());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Setup a <see cref="IProducerModel"/> to send messages with.
        /// </summary>
        /// <param name="producerOptions">The producer options class to setup which must include the exchange name.</param>
        /// <param name="isBatch"></param>
        /// <returns>Object which can send messages to a RabbitMQ exchange.</returns>
        public IProducerModel SetupProducer(ProducerOptions producerOptions, bool isBatch = false)
        {
            if (ShutdownCalled)
            {
                throw new ApplicationException("Adapter has been shut down");
            }

            if (!producerOptions.VerifyPopulated())
            {
                throw new ArgumentException("The given producer options have invalid values");
            }

            //TODO Should maybe refactor this so we have 1 IConnection per service
            //NOTE: IConnection objects are thread safe
            IConnection connection = _factory.CreateConnection(string.Format("{0}::Producer::{1}", _hostId, producerOptions.ExchangeName));

            //NOTE: IModel objects are /not/ thread safe
            IModel model = connection.CreateModel();

            model.ConfirmSelect();

            try
            {
                // Passively declare the exchange (equivalent to checking the exchange exists)
                model.ExchangeDeclarePassive(producerOptions.ExchangeName);
            }
            catch (OperationInterruptedException e)
            {
                model.Close(200, "SetupProducer - Exchange missing");
                connection.Close(200, "SetupProducer - Exchange missing");

                throw new ApplicationException($"Expected exchange \"{producerOptions.ExchangeName}\" to exist", e);
            }

            IBasicProperties props = model.CreateBasicProperties();

            props.ContentEncoding = "UTF-8";
            props.ContentType     = "application/json";
            props.Persistent      = true;

            IProducerModel producerModel;

            try
            {
                producerModel = isBatch ?
                                new BatchProducerModel(producerOptions.ExchangeName, model, props, producerOptions.MaxConfirmAttempts) :
                                new ProducerModel(producerOptions.ExchangeName, model, props, producerOptions.MaxConfirmAttempts);
            }
            catch (Exception)
            {
                model.Close(200, "SetupProducer - Couldn't create ProducerModel");
                connection.Close(200, "SetupProducer - Couldn't create ProducerModel");

                throw;
            }

            var resources = new ProducerResources
            {
                Connection    = connection,
                Model         = model,
                ProducerModel = producerModel,
            };

            lock (_oResourceLock)
            {
                _rabbitResources.Add(Guid.NewGuid(), resources);
            }

            producerModel.OnFatal += (s, ra) =>
            {
                resources.Dispose();
                _hostFatalHandler.Invoke(s, new FatalErrorEventArgs(ra));
            };

            return(producerModel);
        }