public void Handle <T>()
        {
            ulong deliveryTag = 100000;

            try
            {
                var consumer = new EventingBasicConsumer(_rabbitMqchannel);
                consumer.Received += (model, ea) =>
                {
                    Logger.WriteInfo("retrieving the message from queue");
                    deliveryTag = ea.DeliveryTag;
                    var body        = ea.Body;
                    var message     = Encoding.UTF8.GetString(body);
                    var basecommand = Serializer.DeserializeObject <T>(message);
                    ProcessMessage(basecommand);
                    Logger.WriteInfo("message processed");
                    _rabbitMqchannel.BasicAck(ea.DeliveryTag, false);
                    Logger.WriteInfo("Acknowledgement sent");
                };
                var messageProperty = MessagePropertyCollection.GetMessageProperty(_key);
                _rabbitMqchannel.BasicConsume(queue: messageProperty.QueueName,
                                              noAck: false,
                                              consumer: consumer);
            }
            catch (Exception ex)
            {
                Logger.WriteError(ex.ToString());
                _rabbitMqchannel.BasicNack(deliveryTag, false, false);
                throw;
            }
        }
Ejemplo n.º 2
0
        public void Publish <T>(T command)
        {
            Logger.WriteInfo("trying to publish the message");
            var    commandMetaDataAttribute = command.GetType().GetCustomAttribute <CommandMetaDataAttribute>();
            var    messageProperty          = MessagePropertyCollection.GetMessageProperty(commandMetaDataAttribute.Key);
            string strcommand = Serializer.SerializeObject(command);
            var    body       = Encoding.UTF8.GetBytes(strcommand);
            var    properties = _rabbitMqchannel.CreateBasicProperties();

            properties.Persistent   = true;
            properties.DeliveryMode = 2;
            _rabbitMqchannel.BasicPublish(messageProperty.ExchangeName, messageProperty.RoutingKey, true, properties, body);
            Logger.WriteInfo("Message published");
        }