Example #1
0
        protected virtual async Task <IList <Message> > OnPeekAsync(long fromSequenceNumber, int messageCount = 1)
        {
            try
            {
                AmqpRequestMessage requestMessage =
                    AmqpRequestMessage.CreateRequest(
                        ManagementConstants.Operations.PeekMessageOperation,
                        this.OperationTimeout,
                        null);

                requestMessage.Map[ManagementConstants.Properties.FromSequenceNumber] = fromSequenceNumber;
                requestMessage.Map[ManagementConstants.Properties.MessageCount]       = messageCount;

                if (!string.IsNullOrWhiteSpace(this.SessionId))
                {
                    requestMessage.Map[ManagementConstants.Properties.SessionId] = this.SessionId;
                }

                List <Message> messages = new List <Message>();

                AmqpResponseMessage response = await this.ExecuteRequestResponseAsync(requestMessage).ConfigureAwait(false);

                if (response.StatusCode == AmqpResponseStatusCode.OK)
                {
                    Message message     = null;
                    var     messageList = response.GetListValue <AmqpMap>(ManagementConstants.Properties.Messages);
                    foreach (AmqpMap entry in messageList)
                    {
                        var         payload     = (ArraySegment <byte>)entry[ManagementConstants.Properties.Message];
                        AmqpMessage amqpMessage =
                            AmqpMessage.CreateAmqpStreamMessage(new BufferListStream(new[] { payload }), true);
                        message = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage);
                        messages.Add(message);
                    }

                    if (message != null)
                    {
                        this.LastPeekedSequenceNumber = message.SystemProperties.SequenceNumber;
                    }

                    return(messages);
                }
                else if (response.StatusCode == AmqpResponseStatusCode.NoContent ||
                         (response.StatusCode == AmqpResponseStatusCode.NotFound && Equals(AmqpClientConstants.MessageNotFoundError, response.GetResponseErrorCondition())))
                {
                    return(messages);
                }
                else
                {
                    throw response.ToMessagingContractException();
                }
            }
            catch (Exception exception)
            {
                throw AmqpExceptionHelper.GetClientException(exception);
            }
        }
Example #2
0
        public void CanParseMaxAbsoluteExpiryTime()
        {
            var data        = new Data();
            var amqpMessage = AmqpMessage.Create(data);

            amqpMessage.Properties.AbsoluteExpiryTime = DateTime.MaxValue;

            var convertedSbMessage = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage);

            Assert.AreEqual(DateTimeOffset.MaxValue, convertedSbMessage.ExpiresAt);
        }
        void Convert_Amqp_message_with_data_value_byte_array_to_SB_message()
        {
            var messageBody = Encoding.UTF8.GetBytes("message1");

            var data = new Data();

            data.Value = messageBody;
            var amqpMessage = AmqpMessage.Create(data);

            var sbMessage = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage);

            Assert.Equal(messageBody, sbMessage.Body);
        }
        void Convert_Amqp_message_with_Amqp_value_byte_array_to_SB_message()
        {
            var messageBody = Encoding.UTF8.GetBytes("message1");

            var amqpValue = new AmqpValue();

            amqpValue.Value = messageBody;
            var amqpMessage = AmqpMessage.Create(amqpValue);

            var sbMessage = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage);

            Assert.Equal(messageBody, sbMessage.GetBody <byte[]>(null));
        }
Example #5
0
        void Convert_Amqp_message_with_Amqp_value_array_segment_to_SB_message()
        {
            var messageBody = Encoding.UTF8.GetBytes("message1");

            var amqpValue = new AmqpValue();

            amqpValue.Value = new ArraySegment <byte>(messageBody);
            var amqpMessage = AmqpMessage.Create(amqpValue);

            var sbMessage = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage);

            Assert.Equal(messageBody, sbMessage.Body);
        }
Example #6
0
        public void ConvertAmqpMessageToSBMessage()
        {
            var messageBody = Encoding.UTF8.GetBytes("message1");

            var data = new Data();

            data.Value = messageBody;
            var amqpMessage = AmqpMessage.Create(data);

            var sbMessage = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage);

            Assert.AreEqual(messageBody, sbMessage.Body.ToArray());
        }
        void Convert_SB_message_to_Amqp_message_and_back()
        {
            var messageBody     = Encoding.UTF8.GetBytes("hello");
            var messageId       = Guid.NewGuid().ToString();
            var partitionKey    = Guid.NewGuid().ToString();
            var viaPartitionKey = Guid.NewGuid().ToString();
            var sessionId       = Guid.NewGuid().ToString();
            var correlationId   = Guid.NewGuid().ToString();
            var label           = Guid.NewGuid().ToString();
            var to               = Guid.NewGuid().ToString();
            var contentType      = Guid.NewGuid().ToString();
            var replyTo          = Guid.NewGuid().ToString();
            var replyToSessionId = Guid.NewGuid().ToString();
            var publisher        = Guid.NewGuid().ToString();
            var timeToLive       = TimeSpan.FromDays(5);

            var sbMessage = new Message(messageBody)
            {
                MessageId       = messageId,
                PartitionKey    = partitionKey,
                ViaPartitionKey = viaPartitionKey,
                SessionId       = sessionId,
                CorrelationId   = correlationId,
                Label           = label,
                To               = to,
                ContentType      = contentType,
                ReplyTo          = replyTo,
                ReplyToSessionId = replyToSessionId,
                TimeToLive       = timeToLive
            };

            sbMessage.UserProperties.Add("UserProperty", "SomeUserProperty");

            var amqpMessage        = AmqpMessageConverter.SBMessageToAmqpMessage(sbMessage);
            var convertedSbMessage = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage);

            Assert.Equal("SomeUserProperty", convertedSbMessage.UserProperties["UserProperty"]);
            Assert.Equal(messageBody, convertedSbMessage.Body);
            Assert.Equal(messageId, convertedSbMessage.MessageId);
            Assert.Equal(partitionKey, convertedSbMessage.PartitionKey);
            Assert.Equal(viaPartitionKey, convertedSbMessage.ViaPartitionKey);
            Assert.Equal(sessionId, convertedSbMessage.SessionId);
            Assert.Equal(correlationId, convertedSbMessage.CorrelationId);
            Assert.Equal(label, convertedSbMessage.Label);
            Assert.Equal(to, convertedSbMessage.To);
            Assert.Equal(contentType, convertedSbMessage.ContentType);
            Assert.Equal(replyTo, convertedSbMessage.ReplyTo);
            Assert.Equal(replyToSessionId, convertedSbMessage.ReplyToSessionId);
            Assert.Equal(timeToLive, convertedSbMessage.TimeToLive);
        }
Example #8
0
        public void ConvertSBMessageToAmqpMessageAndBack()
        {
            var messageBody     = Encoding.UTF8.GetBytes("hello");
            var messageId       = Guid.NewGuid().ToString();
            var partitionKey    = Guid.NewGuid().ToString();
            var viaPartitionKey = Guid.NewGuid().ToString();
            var sessionId       = partitionKey;
            var correlationId   = Guid.NewGuid().ToString();
            var label           = Guid.NewGuid().ToString();
            var to               = Guid.NewGuid().ToString();
            var contentType      = Guid.NewGuid().ToString();
            var replyTo          = Guid.NewGuid().ToString();
            var replyToSessionId = Guid.NewGuid().ToString();
            var timeToLive       = TimeSpan.FromDays(5);

            var sbMessage = new ServiceBusMessage(messageBody)
            {
                MessageId               = messageId,
                PartitionKey            = partitionKey,
                TransactionPartitionKey = viaPartitionKey,
                SessionId               = sessionId,
                CorrelationId           = correlationId,
                Subject          = label,
                To               = to,
                ContentType      = contentType,
                ReplyTo          = replyTo,
                ReplyToSessionId = replyToSessionId,
                TimeToLive       = timeToLive,
            };

            sbMessage.ApplicationProperties.Add("UserProperty", "SomeUserProperty");

            var amqpMessage        = AmqpMessageConverter.SBMessageToAmqpMessage(sbMessage);
            var convertedSbMessage = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage);

            Assert.AreEqual("SomeUserProperty", convertedSbMessage.ApplicationProperties["UserProperty"]);
            Assert.AreEqual(messageBody, convertedSbMessage.Body.ToArray());
            Assert.AreEqual(messageId, convertedSbMessage.MessageId);
            Assert.AreEqual(partitionKey, convertedSbMessage.PartitionKey);
            Assert.AreEqual(viaPartitionKey, convertedSbMessage.TransactionPartitionKey);
            Assert.AreEqual(sessionId, convertedSbMessage.SessionId);
            Assert.AreEqual(correlationId, convertedSbMessage.CorrelationId);
            Assert.AreEqual(label, convertedSbMessage.Subject);
            Assert.AreEqual(to, convertedSbMessage.To);
            Assert.AreEqual(contentType, convertedSbMessage.ContentType);
            Assert.AreEqual(replyTo, convertedSbMessage.ReplyTo);
            Assert.AreEqual(replyToSessionId, convertedSbMessage.ReplyToSessionId);
            Assert.AreEqual(timeToLive, convertedSbMessage.TimeToLive);
        }
Example #9
0
        void Convert_SB_message_to_Amqp_message_and_back()
        {
            var messageBody      = Encoding.UTF8.GetBytes("hello");
            var messageId        = Guid.NewGuid().ToString();
            var partitionKey     = Guid.NewGuid().ToString();
            var sessionId        = Guid.NewGuid().ToString();
            var correlationId    = Guid.NewGuid().ToString();
            var label            = Guid.NewGuid().ToString();
            var to               = Guid.NewGuid().ToString();
            var contentType      = Guid.NewGuid().ToString();
            var replyTo          = Guid.NewGuid().ToString();
            var replyToSessionId = Guid.NewGuid().ToString();
            var publisher        = Guid.NewGuid().ToString();
            var deadLetterSource = Guid.NewGuid().ToString();

            var sbMessage = new Message(messageBody)
            {
                MessageId        = messageId,
                PartitionKey     = partitionKey,
                SessionId        = sessionId,
                CorrelationId    = correlationId,
                Label            = label,
                To               = to,
                ContentType      = contentType,
                ReplyTo          = replyTo,
                ReplyToSessionId = replyToSessionId,
                Publisher        = publisher,
                DeadLetterSource = deadLetterSource,
            };

            sbMessage.UserProperties.Add("UserProperty", "SomeUserProperty");

            var amqpMessage = AmqpMessageConverter.SBMessageToAmqpMessage(sbMessage);
            var convertedBrokeredMessage = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage);

            Assert.Equal("SomeUserProperty", convertedBrokeredMessage.UserProperties["UserProperty"]);
            Assert.Equal(messageBody, convertedBrokeredMessage.Body);
            Assert.Equal(messageId, convertedBrokeredMessage.MessageId);
            Assert.Equal(partitionKey, convertedBrokeredMessage.PartitionKey);
            Assert.Equal(sessionId, convertedBrokeredMessage.SessionId);
            Assert.Equal(correlationId, convertedBrokeredMessage.CorrelationId);
            Assert.Equal(label, convertedBrokeredMessage.Label);
            Assert.Equal(to, convertedBrokeredMessage.To);
            Assert.Equal(contentType, convertedBrokeredMessage.ContentType);
            Assert.Equal(replyTo, convertedBrokeredMessage.ReplyTo);
            Assert.Equal(replyToSessionId, convertedBrokeredMessage.ReplyToSessionId);
            Assert.Equal(publisher, convertedBrokeredMessage.Publisher);
            Assert.Equal(deadLetterSource, convertedBrokeredMessage.DeadLetterSource);
        }
Example #10
0
        protected virtual async Task <IList <Message> > OnReceiveAsync(int maxMessageCount, TimeSpan serverWaitTime)
        {
            ReceivingAmqpLink receiveLink = null;

            try
            {
                TimeoutHelper timeoutHelper = new TimeoutHelper(serverWaitTime, true);
                receiveLink = await this.ReceiveLinkManager.GetOrCreateAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false);

                IEnumerable <AmqpMessage> amqpMessages = null;
                bool hasMessages = await Task.Factory.FromAsync(
                    (c, s) => receiveLink.BeginReceiveRemoteMessages(maxMessageCount, DefaultBatchFlushInterval, timeoutHelper.RemainingTime(), c, s),
                    a => receiveLink.EndReceiveMessages(a, out amqpMessages),
                    this).ConfigureAwait(false);

                if (receiveLink.TerminalException != null)
                {
                    throw receiveLink.TerminalException;
                }

                if (hasMessages && amqpMessages != null)
                {
                    IList <Message> brokeredMessages = null;
                    foreach (var amqpMessage in amqpMessages)
                    {
                        if (brokeredMessages == null)
                        {
                            brokeredMessages = new List <Message>();
                        }

                        if (this.ReceiveMode == ReceiveMode.ReceiveAndDelete)
                        {
                            receiveLink.DisposeDelivery(amqpMessage, true, AmqpConstants.AcceptedOutcome);
                        }

                        Message message = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage);
                        brokeredMessages.Add(message);
                    }

                    return(brokeredMessages);
                }

                return(null);
            }
            catch (Exception exception)
            {
                throw AmqpExceptionHelper.GetClientException(exception, receiveLink?.GetTrackingId());
            }
        }
Example #11
0
        public void PeekedMessageShouldNotIncrementDeliveryCount()
        {
            var messageBody = Encoding.UTF8.GetBytes("message1");

            var amqpValue = new AmqpValue();

            amqpValue.Value = new ArraySegment <byte>(messageBody);
            var amqpMessage = AmqpMessage.Create(amqpValue);

            amqpMessage.Header.DeliveryCount = 2;

            var sbMessage = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage, isPeeked: true);

            sbMessage.SequenceNumber = 1L;

            Assert.AreEqual(2, sbMessage.DeliveryCount);
        }
        void When_message_is_received_should_have_delivery_count_increased()
        {
            var messageBody = Encoding.UTF8.GetBytes("message1");

            var amqpValue = new AmqpValue();

            amqpValue.Value = new ArraySegment <byte>(messageBody);
            var amqpMessage = AmqpMessage.Create(amqpValue);

            amqpMessage.Header.DeliveryCount = 2;

            var sbMessage = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage, isPeeked: false);

            sbMessage.SystemProperties.SequenceNumber = 1L;

            Assert.Equal(3, sbMessage.SystemProperties.DeliveryCount);
        }
Example #13
0
        protected virtual async Task <IList <Message> > OnReceiveBySequenceNumberAsync(IEnumerable <long> sequenceNumbers)
        {
            List <Message> messages = new List <Message>();

            try
            {
                AmqpRequestMessage requestMessage = AmqpRequestMessage.CreateRequest(ManagementConstants.Operations.ReceiveBySequenceNumberOperation, this.OperationTimeout, null);
                requestMessage.Map[ManagementConstants.Properties.SequenceNumbers]    = sequenceNumbers.ToArray();
                requestMessage.Map[ManagementConstants.Properties.ReceiverSettleMode] = (uint)(this.ReceiveMode == ReceiveMode.ReceiveAndDelete ? 0 : 1);

                AmqpResponseMessage response = await this.ExecuteRequestResponseAsync(requestMessage).ConfigureAwait(false);

                if (response.StatusCode == AmqpResponseStatusCode.OK)
                {
                    IEnumerable <AmqpMap> amqpMapList = response.GetListValue <AmqpMap>(ManagementConstants.Properties.Messages);
                    foreach (AmqpMap entry in amqpMapList)
                    {
                        ArraySegment <byte> payload     = (ArraySegment <byte>)entry[ManagementConstants.Properties.Message];
                        AmqpMessage         amqpMessage = AmqpMessage.CreateAmqpStreamMessage(new BufferListStream(new[] { payload }), true);
                        Message             message     = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage);
                        Guid lockToken;
                        if (entry.TryGetValue(ManagementConstants.Properties.LockToken, out lockToken))
                        {
                            message.SystemProperties.LockTokenGuid = lockToken;
                            this.requestResponseLockedMessages.AddOrUpdate(lockToken, message.SystemProperties.LockedUntilUtc);
                        }

                        messages.Add(message);
                    }
                }
                else
                {
                    throw response.ToMessagingContractException();
                }
            }
            catch (Exception exception)
            {
                throw AmqpExceptionHelper.GetClientException(exception);
            }

            return(messages);
        }
Example #14
0
        public void CanParseDictionaryValueSection()
        {
            var amqpMessage = AmqpMessage.Create(new AmqpValue {
                Value = new Dictionary <string, string> {
                    { "key", "value" }
                }
            });
            var sbMessage = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage);
            var body      = sbMessage.GetRawAmqpMessage().Body;

            Assert.IsTrue(body.TryGetValue(out object val));
            Assert.AreEqual("value", ((Dictionary <string, string>)val)["key"]);

            amqpMessage = AmqpMessage.Create(new AmqpValue {
                Value = new AmqpMap {
                    { new MapKey("key"), "value" }
                }
            });
            sbMessage = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage);
            body      = sbMessage.GetRawAmqpMessage().Body;
            Assert.IsTrue(body.TryGetValue(out val));
            Assert.AreEqual("value", ((Dictionary <string, object>)val)["key"]);
        }