예제 #1
0
        public void Handler(object sender, BasicNackEventArgs e, IPublishedMessageStore publishedMessageStore)
        {
            using (var tx = publishedMessageStore.BeginTransaction())
            {
                RmqPublishMessage targetMessage = publishedMessageStore.GetByDeliveryTag(e.DeliveryTag);

                targetMessage.Status = MessageStatusConstants.Failed;

                publishedMessageStore.Commit(tx);
            }
        }
예제 #2
0
        public void Send <D>(D message, string routingKey) where D : IDomainEvent
        {
            var camelCaseSerializer = new JsonSerializer()
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            // set property of message
            var properties = _channel.CreateBasicProperties();

            properties.Persistent = true;

            // map message to RmqPublishMessage object
            RmqPublishMessage rmqMessage = _mapper.Map <RmqPublishMessage>(message);

            // set delivery tag
            rmqMessage.DeliveryTag = _channel.NextPublishSeqNo;

            using (var tx = _publishedMessageStore.BeginTransaction())
            {
                _publishedMessageStore.Create(rmqMessage);

                _publishedMessageStore.Commit(tx);
            }

            // RmqPublishMessage => JObject (message)
            JObject rmqMessageJObject = JObject.FromObject(rmqMessage, camelCaseSerializer);

            // JObject (message) => json
            string jsonMessage = JsonConvert.SerializeObject(rmqMessageJObject, Formatting.None, new JsonSerializerSettings()
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

            log.Debug("the message is about sent: ");
            log.Debug(jsonMessage);

            // json (message) => byte[];
            var body = Encoding.UTF8.GetBytes(jsonMessage);

            // send message
            _channel.BasicPublish(exchange: _publisher.ExchangeName,
                                  routingKey: routingKey,
                                  mandatory: true,
                                  basicProperties: properties,
                                  body: body);
        }
예제 #3
0
        public void Handler(object sender, BasicReturnEventArgs e, IPublishedMessageStore publishedMessageStore)
        {
            using (var tx = publishedMessageStore.BeginTransaction())
            {
                string body = Encoding.UTF8.GetString(e.Body);

                RmqPublishMessage returnedMessage = JsonConvert.DeserializeObject <RmqPublishMessage>(body);

                RmqPublishMessage foundMessage = publishedMessageStore.GetByMessageId(returnedMessage.MessageId);

                foundMessage.Status       = MessageStatusConstants.Unroutable;
                foundMessage.StatusReason = e.ReplyText;

                publishedMessageStore.Update(foundMessage);

                publishedMessageStore.Commit(tx);
            }
        }