public ArraySegment <byte> ConvertViaMemoryStream() { using var messageStream = amqpMessage.ToStream(); using var memStream = new MemoryStream(StreamBufferSizeInBytes); messageStream.CopyTo(memStream, StreamBufferSizeInBytes); return(new ArraySegment <byte>(memStream.ToArray())); }
protected override async Task OnSendEventAsync(IEnumerable <Message> messages) { // List to hold messages in Amqp friendly format var messageList = new List <Data>(); foreach (var message in messages) { using (AmqpMessage amqpMessage = message.ToAmqpMessage()) { var data = new Data() { Value = MessageConverter.ReadStream(amqpMessage.ToStream()) }; messageList.Add(data); } } Outcome outcome; using (var amqpMessage = AmqpMessage.Create(messageList)) { amqpMessage.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat; outcome = await this.SendAmqpMessageAsync(amqpMessage); } if (outcome.DescriptorCode != Accepted.Code) { throw AmqpErrorMapper.GetExceptionFromOutcome(outcome); } }
static AmqpMessage GetBatchedMessage(IEnumerable <string> contents) { var messageList = new List <Data>(); int ctr = 0; foreach (string msgContent in contents) { byte[] bytes = Encoding.UTF8.GetBytes(msgContent); using (AmqpMessage msg = AmqpMessage.Create(new MemoryStream(bytes), false)) { msg.Properties.MessageId = $"{ctr}"; msg.ApplicationProperties = new ApplicationProperties(); msg.ApplicationProperties.Map["MsgCnt"] = $"{ctr++}"; msg.ApplicationProperties.Map["MsgData"] = msgContent; var data = new Data { Value = ReadStream(msg.ToStream()) }; messageList.Add(data); } } AmqpMessage amqpMessage = AmqpMessage.Create(messageList); amqpMessage.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat; return(amqpMessage); }
public override async Task SendEventAsync(IEnumerable <Message> messages, CancellationToken cancellationToken) { await this.HandleTimeoutCancellation(async() => { // List to hold messages in Amqp friendly format var messageList = new List <Data>(); foreach (Message message in messages) { using (AmqpMessage amqpMessage = message.ToAmqpMessage()) { var data = new Data() { Value = MessageConverter.ReadStream(amqpMessage.ToStream()) }; messageList.Add(data); } } Outcome outcome; using (AmqpMessage amqpMessage = AmqpMessage.Create(messageList)) { amqpMessage.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat; outcome = await this.SendAmqpMessageAsync(amqpMessage, cancellationToken).ConfigureAwait(false); } if (outcome.DescriptorCode != Accepted.Code) { throw AmqpErrorMapper.GetExceptionFromOutcome(outcome); } }, cancellationToken).ConfigureAwait(false); }
public BrokerMessage(AmqpMessage message) { this.stream = (BufferListStream)message.ToStream(); foreach (var buffer in message.RawByteBuffers) { buffer.Clone(); } this.RawByteBuffers = message.RawByteBuffers; }
public static AmqpMessage EventDatasToAmqpMessage(IEnumerable <EventData> eventDatas, string partitionKey, bool batchable) { if (eventDatas == null) { throw new ArgumentNullException(nameof(eventDatas)); } AmqpMessage returnMessage = null; var dataCount = eventDatas.Count(); if (dataCount > 1) { IList <Data> bodyList = new List <Data>(); EventData firstEvent = null; foreach (EventData data in eventDatas) { if (firstEvent == null) { //this.ProcessFaultInjectionInfo(data); firstEvent = data; } AmqpMessage amqpMessage = EventDataToAmqpMessage(data, partitionKey); amqpMessage.Batchable = batchable; if ((amqpMessage.Sections & ClientAmqpPropsSetOnSendToEventHub) == 0 && (data.Body.Array == null || data.Body.Count == 0)) { throw new InvalidOperationException(Resources.CannotSendAnEmptyEvent.FormatForUser(data.GetType().Name)); } ArraySegment <byte> buffer = StreamToBytes(amqpMessage.ToStream()); bodyList.Add(new Data { Value = buffer }); } returnMessage = AmqpMessage.Create(bodyList); returnMessage.Batchable = true; returnMessage.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat; UpdateAmqpMessageHeadersAndProperties(returnMessage, null, partitionKey, firstEvent, copyUserProperties: false); } else if (dataCount == 1) // ??? can't be null { var data = eventDatas.First(); //this.ProcessFaultInjectionInfo(data); returnMessage = EventDataToAmqpMessage(data, partitionKey); returnMessage.Batchable = batchable; if ((returnMessage.Sections & ClientAmqpPropsSetOnSendToEventHub) == 0 && (data.Body.Array == null || data.Body.Count == 0)) { throw new InvalidOperationException(Resources.CannotSendAnEmptyEvent.FormatForUser(data.GetType().Name)); } } return(returnMessage); }
public override async Task SendEventAsync(IEnumerable <Message> messages, CancellationToken cancellationToken) { if (Logging.IsEnabled) { Logging.Enter(this, messages, cancellationToken, $"{nameof(SendEventAsync)}"); } try { cancellationToken.ThrowIfCancellationRequested(); // List to hold messages in Amqp friendly format var messageList = new List <Data>(); foreach (Message message in messages) { using (AmqpMessage amqpMessage = message.ToAmqpMessage()) { var data = new Data() { Value = MessageConverter.ReadStream(amqpMessage.ToStream()) }; messageList.Add(data); } } Outcome outcome; using (AmqpMessage amqpMessage = AmqpMessage.Create(messageList)) { amqpMessage.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat; outcome = await _amqpUnit.SendEventAsync(amqpMessage, _operationTimeout).ConfigureAwait(false); } if (outcome != null) { if (outcome.DescriptorCode != Accepted.Code) { throw AmqpErrorMapper.GetExceptionFromOutcome(outcome); } } } finally { if (Logging.IsEnabled) { Logging.Exit(this, messages, cancellationToken, $"{nameof(SendEventAsync)}"); } } }
internal async Task <AmqpIoTOutcome> SendMessagesAsync(IEnumerable <Message> messages, TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(this, $"{nameof(SendMessagesAsync)}"); } // List to hold messages in Amqp friendly format var messageList = new List <Data>(); foreach (Message message in messages) { using (AmqpMessage amqpMessage = AmqpIoTMessageConverter.MessageToAmqpMessage(message)) { var data = new Data() { Value = AmqpIoTMessageConverter.ReadStream(amqpMessage.ToStream()) }; messageList.Add(data); } } Outcome outcome; using (AmqpMessage amqpMessage = AmqpMessage.Create(messageList)) { amqpMessage.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat; outcome = await SendAmqpMessageAsync(amqpMessage, timeout).ConfigureAwait(false); } AmqpIoTOutcome amqpIoTOutcome = new AmqpIoTOutcome(outcome); if (amqpIoTOutcome != null) { amqpIoTOutcome.ThrowIfNotAccepted(); } if (Logging.IsEnabled) { Logging.Exit(this, $"{nameof(SendMessagesAsync)}"); } return(amqpIoTOutcome); }
internal async Task <AmqpIotOutcome> SendMessagesAsync(IEnumerable <Message> messages, CancellationToken cancellationToken) { if (Logging.IsEnabled) { Logging.Enter(this, nameof(SendMessagesAsync)); } cancellationToken.ThrowIfCancellationRequested(); // List to hold messages in AMQP friendly format var messageList = new List <Data>(messages.Count()); foreach (Message message in messages) { using AmqpMessage amqpMessage = AmqpIotMessageConverter.MessageToAmqpMessage(message); var data = new Data { Value = AmqpIotMessageConverter.ReadStream(amqpMessage.ToStream()), }; messageList.Add(data); } using var batchMessage = AmqpMessage.Create(messageList); batchMessage.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat; Outcome outcome = await SendAmqpMessageAsync(batchMessage, cancellationToken).ConfigureAwait(false); var amqpIotOutcome = new AmqpIotOutcome(outcome); amqpIotOutcome.ThrowIfNotAccepted(); if (Logging.IsEnabled) { Logging.Exit(this, nameof(SendMessagesAsync)); } return(amqpIotOutcome); }
public void AmqpMessageSerializationTest() { // empty message AmqpMessage message = AmqpMessage.Create(); AddSection(message, SectionFlag.Properties); RunSerializationTest(message); // data message message = AmqpMessage.Create(new Data() { Value = new ArraySegment <byte>(new byte[60]) }); RunSerializationTest(message); message = AmqpMessage.Create(new Data[] { new Data() { Value = new ArraySegment <byte>(new byte[60]) }, new Data() { Value = new ArraySegment <byte>(new byte[44]) } }); AddSection(message, SectionFlag.Header | SectionFlag.ApplicationProperties); RunSerializationTest(message); // value message message = AmqpMessage.Create(new AmqpValue() { Value = new AmqpSymbol("symbol value") }); AddSection(message, SectionFlag.Header | SectionFlag.DeliveryAnnotations | SectionFlag.ApplicationProperties); RunSerializationTest(message); // sequence message message = AmqpMessage.Create(new AmqpSequence[] { new AmqpSequence(new List <object>() { "string1", 1234 }), new AmqpSequence(new List <object>() { DateTime.Parse("2012-01-01 12:00:00").ToUniversalTime() }) }); AddSection(message, SectionFlag.MessageAnnotations | SectionFlag.Properties | SectionFlag.ApplicationProperties | SectionFlag.Footer); RunSerializationTest(message); // data message - binary message = AmqpUtils.CreateMessage(new byte[888]); AddSection(message, SectionFlag.DeliveryAnnotations | SectionFlag.ApplicationProperties | SectionFlag.Footer); RunSerializationTest(message); // body stream message message = AmqpMessage.Create(new MemoryStream(new byte[679]), true); AddSection(message, SectionFlag.Header | SectionFlag.MessageAnnotations | SectionFlag.Footer); RunSerializationTest(message); // the following simulates a message's round trip from client to broker to client // message -serialize-> buffers -> input stream message -> output stream message -deserialize> message message = AmqpMessage.Create(new AmqpValue() { Value = new AmqpSymbol("symbol value") }); AddSection(message, SectionFlag.MessageAnnotations | SectionFlag.Properties | SectionFlag.ApplicationProperties); // serialize - send the message on client side ArraySegment <byte>[] buffers = ReadMessagePayLoad(message, 71); // input stream message - received on broker side AmqpMessage message2 = AmqpMessage.CreateAmqpStreamMessage(new BufferListStream(buffers)); // output stream message - send out on broker side message2 = AmqpMessage.CreateOutputMessage((BufferListStream)message2.ToStream(), true); AddSection(message2, SectionFlag.Header | SectionFlag.DeliveryAnnotations); message2.MessageAnnotations.Map["delivery-count"] = 5; // input stream message - received on client side buffers = ReadMessagePayLoad(message2, 71); message2 = AmqpMessage.CreateAmqpStreamMessage(new BufferListStream(buffers)); // update the original message to match the updated message AddSection(message, SectionFlag.Header | SectionFlag.DeliveryAnnotations); message.MessageAnnotations.Map["delivery-count"] = 5; ValidateMessage(message, message2); }
public static AmqpMessage EventDatasToAmqpMessage(IEnumerable <EventData> eventDatas, string partitionKey) { Guard.ArgumentNotNull(nameof(eventDatas), eventDatas); AmqpMessage returnMessage = null; var dataCount = eventDatas.Count(); if (dataCount > 1) { IList <Data> bodyList = new List <Data>(); EventData firstEvent = null; foreach (EventData data in eventDatas) { if (firstEvent == null) { firstEvent = data; } // Create AMQP message if not created yet. We might have created AmqpMessage while building the EventDataBatch. AmqpMessage amqpMessage = data.AmqpMessage; data.AmqpMessage = null; // Retry on the same message should create a new AmqpMessage if (amqpMessage == null) { amqpMessage = EventDataToAmqpMessage(data); } UpdateAmqpMessagePartitionKey(amqpMessage, partitionKey); amqpMessage.Batchable = true; if ((amqpMessage.Sections & ClientAmqpPropsSetOnSendToEventHub) == 0 && data.Body.Array == null) { throw new InvalidOperationException(Resources.CannotSendAnEmptyEvent.FormatForUser(data.GetType().Name)); } ArraySegment <byte> buffer = StreamToBytes(amqpMessage.ToStream()); bodyList.Add(new Data { Value = buffer }); } returnMessage = AmqpMessage.Create(bodyList); returnMessage.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat; UpdateAmqpMessageHeadersAndProperties(returnMessage, null, firstEvent, copyUserProperties: false); } else if (dataCount == 1) // ??? can't be null { var data = eventDatas.First(); // Create AMQP message if not created yet. We might have created AmqpMessage while building the EventDataBatch. returnMessage = data.AmqpMessage; data.AmqpMessage = null; // Retry on the same message should create a new AmqpMessage if (returnMessage == null) { returnMessage = EventDataToAmqpMessage(data); } if ((returnMessage.Sections & ClientAmqpPropsSetOnSendToEventHub) == 0 && data.Body.Array == null) { throw new InvalidOperationException(Resources.CannotSendAnEmptyEvent.FormatForUser(data.GetType().Name)); } } returnMessage.Batchable = true; UpdateAmqpMessagePartitionKey(returnMessage, partitionKey); return(returnMessage); }
public BrokerMessage(AmqpMessage message) { this.stream = (BufferListStream)message.ToStream(); foreach (var buffer in message.RawByteBuffers) buffer.Clone(); this.RawByteBuffers = message.RawByteBuffers; }