/// <summary> /// Dequeue a message /// </summary> /// <param name="queueConfiguration">(sic)</param> /// <param name="handler">QueueMessageHandler</param> public void SetupDequeueEvent(Models.RabbitMqInstanceConfiguration queueConfiguration, QueueMessageHandler handler) { var factory = RabbitMqUtility.RabbitMQMakeConnectionFactory(this._engineConfiguration.Host, this._engineConfiguration.Port, this._engineConfiguration.Username, this._engineConfiguration.Password); using (var connection = factory.CreateConnection()) { using (var model = connection.CreateModel()) { RabbitMqUtility.SetupDurableQueue(model, this._engineConfiguration, queueConfiguration); var consumer = new EventingBasicConsumer(model); consumer.Received += (_, ea) => { handler(this, this._logger, model, ea); }; model.BasicConsume(queue: queueConfiguration.QueueName, autoAck: false, consumer: consumer); while (this.KeepListening) { } } } }
/// <summary> /// Enqueue message /// </summary> /// <typeparam name="T">T</typeparam> /// <param name="message">Message of T</param> /// <param name="queueConfiguration">RabbitMqInstanceConfiguration</param> public void Enqueue <T>(T message, Models.RabbitMqInstanceConfiguration queueConfiguration) { var factory = RabbitMqUtility.RabbitMQMakeConnectionFactory(this._engineConfiguration.Host, this._engineConfiguration.Port, this._engineConfiguration.Username, this._engineConfiguration.Password); using (var connection = factory.CreateConnection()) { using (IModel model = connection.CreateModel()) { RabbitMqUtility.SetupDurableQueue(model, this._engineConfiguration, queueConfiguration); var messageProperties = RabbitMqUtility.MessageBasicPropertiesPersistant(model, this._engineConfiguration.MessageDeliveryMode, this._engineConfiguration.MessagePersistent, this._engineConfiguration.MessageExpiration); // Make message var json = JsonConvert.SerializeObject(message); var body = Encoding.UTF8.GetBytes(json); // Send message RabbitMqUtility.Publish(model, queueConfiguration.ExchangeName, queueConfiguration.RoutingKey, messageProperties, body); this._logger.LogInformation("Published: {0}", json); } } }