コード例 #1
0
        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;
            }
        }
コード例 #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");
        }
コード例 #3
0
        private void SetUpNormalExchanges(IModel channel)
        {
            Logger.WriteInfo("Normal exchange and queue setup starting");
            int ttl = 20; //default time in minute to live

            foreach (RabbitMqBusEndpointElement element in _rabbitMqsection.ConnectionManagerEndpoints)
            {
                if (!string.IsNullOrWhiteSpace(element.Name))
                {
                    MessagePropertyCollection.AddMessageProperty(element.Name, new MessageProperty()
                    {
                        ExchangeName = element.Exchange, ExchangeType = element.ExchangeType, Durable = bool.Parse(element.Durable), QueueName = element.Queue, RoutingKey = element.Routingkey
                    });
                    channel.ExchangeDeclare(element.Exchange, element.ExchangeType, true);
                    int timetolive;

                    if (int.TryParse(element.TTL, out timetolive))
                    {
                        ttl = timetolive * 60 * 1000; //convert to miliseconds
                    }
                    else
                    {
                        ttl = ttl * 60 * 1000; //convert to  miliseconds
                    }


                    var args = new Dictionary <string, object>();
                    if (ttl > 0)
                    {
                        args.Add("x-message-ttl", ttl);
                    }
                    args.Add("x-ha-policy", "all");
                    if (element.DeadletterRouting)
                    {
                        args.Add("x-dead-letter-exchange", _rabbitMqsection.DeadLetterExchange.Exchange);
                    }
                    channel.QueueDeclare(element.Queue, true, false, false, args);
                    channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
                    channel.QueueBind(element.Queue, element.Exchange, element.Routingkey);
                }
            }
            Logger.WriteInfo("Normal exchange and queue are set up");
        }