public override async Task <Message> ReceiveAsync(TimeSpan timeout, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); Message message = null; AmqpMessage amqpMessage; try { ReceivingAmqpLink deviceBoundReceivingLink = await this.GetDeviceBoundReceivingLinkAsync(cancellationToken).ConfigureAwait(false); amqpMessage = await deviceBoundReceivingLink.ReceiveMessageAsync(timeout).ConfigureAwait(false); } catch (Exception exception) when(!exception.IsFatal() && !(exception is OperationCanceledException)) { throw AmqpClientHelper.ToIotHubClientContract(exception); } if (amqpMessage != null) { message = new Message(amqpMessage) { LockToken = new Guid(amqpMessage.DeliveryTag.Array).ToString() }; } else { message = null; } return(message); }
public override async Task <FileNotification> ReceiveAsync(TimeSpan timeout) { try { ReceivingAmqpLink receivingLink = await this.faultTolerantReceivingLink.GetReceivingLinkAsync(); AmqpMessage amqpMessage = await receivingLink.ReceiveMessageAsync(timeout); if (amqpMessage != null) { using (amqpMessage) { AmqpClientHelper.ValidateContentType(amqpMessage, CommonConstants.FileNotificationContentType); var fileNotification = await AmqpClientHelper.GetObjectFromAmqpMessageAsync <FileNotification>(amqpMessage); fileNotification.LockToken = new Guid(amqpMessage.DeliveryTag.Array).ToString(); return(fileNotification); } } return(null); } catch (Exception exception) { if (exception.IsFatal()) { throw; } throw AmqpClientHelper.ToIotHubClientContract(exception); } }
private static void StartReceiver() { Task.Run(async() => { var tfactory = new AmqpConnectionFactory(); var connection = await tfactory.OpenConnectionAsync("amqp://localhost:5672", TimeSpan.FromSeconds(10)); var session = connection.CreateSession(new AmqpSessionSettings()); var receicerSettings = new AmqpLinkSettings { LinkName = $"receiver-{DateTime.UtcNow.Ticks}", Role = true, TotalLinkCredit = 300, Source = new Source { Address = QueueName, }, Target = new Target() }; var receiver = new ReceivingAmqpLink(session, receicerSettings); while (true) { AmqpMessage message = await receiver.ReceiveMessageAsync(TimeSpan.FromSeconds(20)); if (message != null) { receiver.DisposeDelivery(message, true, AmqpConstants.AcceptedOutcome); DisplayMessage(new StreamReader(message.BodyStream).ReadToEnd()); } } }); }
public void AmqpWebSocketTransportTest() { string address = "ws://localhost:28088"; var broker = new TestAmqpBroker(new string[] { address }, null, null, null); try { broker.Start(); string queue = "AmqpWebSocketTransportTest"; broker.AddQueue(queue); AmqpConnection connection = AmqpConnection.Factory.OpenConnectionAsync(address).GetAwaiter().GetResult(); AmqpSession session = connection.CreateSession(new AmqpSessionSettings()); session.Open(); SendingAmqpLink sLink = new SendingAmqpLink(session, AmqpUtils.GetLinkSettings(true, queue, SettleMode.SettleOnSend)); sLink.Open(); int messageCount = 100; for (int i = 0; i < messageCount; i++) { AmqpMessage message = AmqpMessage.Create(new AmqpValue() { Value = "message" + i }); sLink.SendMessageAsync(message, AmqpConstants.EmptyBinary, AmqpConstants.NullBinary, TimeSpan.FromSeconds(10)).Wait(); } sLink.Close(); ReceivingAmqpLink rLink = new ReceivingAmqpLink(session, AmqpUtils.GetLinkSettings(false, queue, SettleMode.SettleOnReceive, 100)); rLink.Open(); for (int i = 0; i < messageCount; i++) { AmqpMessage message2 = rLink.ReceiveMessageAsync(TimeSpan.FromSeconds(60)).GetAwaiter().GetResult(); Assert.NotNull(message2); rLink.AcceptMessage(message2, false); message2.Dispose(); } rLink.Close(); connection.Close(); } finally { broker.Stop(); } }
public override async Task <FeedbackBatch> ReceiveAsync(CancellationToken cancellationToken) { Logging.Enter(this, nameof(ReceiveAsync)); try { cancellationToken.ThrowIfCancellationRequested(); ReceivingAmqpLink receivingLink = await _faultTolerantReceivingLink.GetReceivingLinkAsync().ConfigureAwait(false); AmqpMessage amqpMessage = await receivingLink.ReceiveMessageAsync(cancellationToken).ConfigureAwait(false); Logging.Info(this, $"Message received is [{amqpMessage}]", nameof(ReceiveAsync)); if (amqpMessage != null) { using (amqpMessage) { AmqpClientHelper.ValidateContentType(amqpMessage, CommonConstants.BatchedFeedbackContentType); IEnumerable <FeedbackRecord> records = await AmqpClientHelper .GetObjectFromAmqpMessageAsync <IEnumerable <FeedbackRecord> >(amqpMessage).ConfigureAwait(false); return(new FeedbackBatch { EnqueuedTime = (DateTime)amqpMessage.MessageAnnotations.Map[MessageSystemPropertyNames.EnqueuedTime], LockToken = new Guid(amqpMessage.DeliveryTag.Array).ToString(), Records = records, UserId = Encoding.UTF8.GetString(amqpMessage.Properties.UserId.Array, amqpMessage.Properties.UserId.Offset, amqpMessage.Properties.UserId.Count) }); } } return(null); } catch (Exception exception) { Logging.Error(this, exception, nameof(ReceiveAsync)); if (exception.IsFatal()) { throw; } throw AmqpClientHelper.ToIotHubClientContract(exception); } finally { Logging.Exit(this, nameof(ReceiveAsync)); } }
public void AmqpWebSocketTransportTest() { string queue = "AmqpWebSocketTransportTest"; broker.AddQueue(queue); AmqpConnection connection = AmqpConnection.Factory.OpenConnectionAsync( TestAmqpBrokerFixture.WsAddress.OriginalString).GetAwaiter().GetResult(); AmqpSession session = connection.CreateSession(new AmqpSessionSettings()); session.Open(); SendingAmqpLink sLink = new SendingAmqpLink(session, AmqpUtils.GetLinkSettings(true, queue, SettleMode.SettleOnSend)); sLink.Open(); int messageCount = 1800; for (int i = 0; i < messageCount; i++) { AmqpMessage message = AmqpMessage.Create(new AmqpValue() { Value = "message" + i }); sLink.SendMessageAsync(message, EmptyBinary, NullBinary, TimeSpan.FromSeconds(10)).Wait(); } sLink.Close(); ReceivingAmqpLink rLink = new ReceivingAmqpLink(session, AmqpUtils.GetLinkSettings(false, queue, SettleMode.SettleOnReceive, 100)); rLink.Open(); for (int i = 0; i < messageCount; i++) { AmqpMessage message2 = rLink.ReceiveMessageAsync(TimeSpan.FromSeconds(60)).GetAwaiter().GetResult(); Assert.NotNull(message2); rLink.AcceptMessage(message2); message2.Dispose(); } rLink.Close(); connection.Close(); }
public override async Task <FeedbackBatch> ReceiveAsync(TimeSpan timeout) { try { ReceivingAmqpLink receivingLink = await this.GetReceivingLinkAsync(); AmqpMessage amqpMessage = await receivingLink.ReceiveMessageAsync(timeout); if (amqpMessage != null) { using (amqpMessage) { string contentType = amqpMessage.Properties.ContentType.ToString(); if (!string.Equals(contentType, CommonConstants.BatchedFeedbackContentType, StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("Unsupported content type: {0}".FormatInvariant(contentType)); } using (var reader = new StreamReader(amqpMessage.BodyStream, Encoding.UTF8)) { string jsonString = await reader.ReadToEndAsync(); var records = JsonConvert.DeserializeObject <IEnumerable <FeedbackRecord> >(jsonString); return(new FeedbackBatch { EnqueuedTime = (DateTime)amqpMessage.MessageAnnotations.Map[MessageSystemPropertyNames.EnqueuedTime], LockToken = new Guid(amqpMessage.DeliveryTag.Array).ToString(), Records = records, UserId = Encoding.UTF8.GetString(amqpMessage.Properties.UserId.Array, amqpMessage.Properties.UserId.Offset, amqpMessage.Properties.UserId.Count) }); } } } return(null); } catch (Exception exception) { if (exception.IsFatal()) { throw; } throw AmqpClientHelper.ToIotHubClientContract(exception); } }
public override async Task <FileNotification> ReceiveAsync(CancellationToken cancellationToken) { Logging.Enter(this, nameof(ReceiveAsync)); try { cancellationToken.ThrowIfCancellationRequested(); ReceivingAmqpLink receivingLink = await _faultTolerantReceivingLink.GetReceivingLinkAsync().ConfigureAwait(false); AmqpMessage amqpMessage = await receivingLink.ReceiveMessageAsync(cancellationToken).ConfigureAwait(false); Logging.Info(this, $"Message received is [{amqpMessage}]", nameof(ReceiveAsync)); if (amqpMessage != null) { using (amqpMessage) { AmqpClientHelper.ValidateContentType(amqpMessage, CommonConstants.FileNotificationContentType); FileNotification fileNotification = await AmqpClientHelper.GetObjectFromAmqpMessageAsync <FileNotification>(amqpMessage).ConfigureAwait(false); fileNotification.LockToken = new Guid(amqpMessage.DeliveryTag.Array).ToString(); return(fileNotification); } } return(null); } catch (Exception exception) { Logging.Error(this, exception, nameof(ReceiveAsync)); if (exception.IsFatal()) { throw; } throw AmqpClientHelper.ToIotHubClientContract(exception); } finally { Logging.Exit(this, nameof(ReceiveAsync)); } }
internal async Task <Message> ReceiveAmqpMessageAsync(TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(this, $"{nameof(ReceiveAmqpMessageAsync)}"); } try { AmqpMessage amqpMessage = await _receivingAmqpLink.ReceiveMessageAsync(timeout).ConfigureAwait(false); Message message = null; if (amqpMessage != null) { message = AmqpIoTMessageConverter.AmqpMessageToMessage(amqpMessage); message.LockToken = new Guid(amqpMessage.DeliveryTag.Array).ToString(); } return(message); } catch (Exception e) when(!e.IsFatal()) { Exception ex = AmqpIoTExceptionAdapter.ConvertToIoTHubException(e, _receivingAmqpLink); if (ReferenceEquals(e, ex)) { throw; } else { if (ex is AmqpIoTResourceException) { _receivingAmqpLink.SafeClose(); throw new IotHubCommunicationException(ex.Message, ex); } throw ex; } } finally { if (Logging.IsEnabled) { Logging.Exit(this, $"{nameof(ReceiveAmqpMessageAsync)}"); } } }
internal static async Task <AmqpMessage> ReceiveAmqpMessageAsync(ReceivingAmqpLink receivingAmqpLink, TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(receivingAmqpLink, timeout, $"{nameof(DisposeMessageAsync)}"); } AmqpMessage amqpMessage = null; if (receivingAmqpLink != null) { amqpMessage = await receivingAmqpLink.ReceiveMessageAsync(timeout).ConfigureAwait(false); } if (Logging.IsEnabled) { Logging.Exit(receivingAmqpLink, timeout, $"{nameof(DisposeMessageAsync)}"); } return(amqpMessage); }
private void StartReceiver() { Task.Run(async() => { var tfactory = new AmqpConnectionFactory(); var connection = await tfactory.OpenConnectionAsync("amqp://localhost:5672", TimeSpan.FromSeconds(10)); var session = connection.CreateSession(CreateSessionSettings()); var receicerSettings = new AmqpLinkSettings { LinkName = $"receiver-{DateTime.UtcNow.Ticks}", Role = true, Source = new Source { Address = QueueName, //DynamicNodeProperties = CreateProperties(), //Value = "sourceValue", Durable = 1 }, Properties = CreateProperties() }; var receiver = new ReceivingAmqpLink(session, receicerSettings); await Task.WhenAll( session.OpenAsync(session.DefaultOpenTimeout), receiver.OpenAsync(receiver.DefaultOpenTimeout)); while (true) { AmqpMessage message = await receiver.ReceiveMessageAsync(TimeSpan.FromSeconds(20)); if (message != null) { receiver.DisposeDelivery(message, true, AmqpConstants.AcceptedOutcome); ReceiveMessage(message); } } }); }
async Task RunClientAsync(string address) { AmqpConnectionFactory factory = new AmqpConnectionFactory(); factory.Settings.TransportProviders.Add( new TlsTransportProvider( new TlsTransportSettings() { CertificateValidationCallback = (a, b, c, d) => true }, AmqpVersion.V100)); AmqpConnection connection = await factory.OpenConnectionAsync(address); AmqpSession session = connection.CreateSession(new AmqpSessionSettings()); await session.OpenAsync(TimeSpan.FromSeconds(20)); SendingAmqpLink sLink = new SendingAmqpLink(session, AmqpUtils.GetLinkSettings(true, queue, SettleMode.SettleOnSend)); await sLink.OpenAsync(TimeSpan.FromSeconds(20)); AmqpMessage message = AmqpMessage.Create(new AmqpValue() { Value = "AmqpConnectionFactoryTest" }); Outcome outcome = await sLink.SendMessageAsync(message, EmptyBinary, NullBinary, TimeSpan.FromSeconds(10)); Assert.Equal(Accepted.Code, outcome.DescriptorCode); ReceivingAmqpLink rLink = new ReceivingAmqpLink(session, AmqpUtils.GetLinkSettings(false, queue, SettleMode.SettleOnDispose, 10)); await rLink.OpenAsync(TimeSpan.FromSeconds(20)); var receivedMessage = await rLink.ReceiveMessageAsync(TimeSpan.FromSeconds(20)); Assert.NotNull(receivedMessage); outcome = await rLink.DisposeMessageAsync(receivedMessage.DeliveryTag, new Accepted(), false, TimeSpan.FromSeconds(20)); Assert.Equal(Accepted.Code, outcome.DescriptorCode); await connection.CloseAsync(TimeSpan.FromSeconds(20)); }
public override async Task <Message> ReceiveAsync(TimeSpan timeout, CancellationToken cancellationToken) { Message message = null; await this.HandleTimeoutCancellation(async() => { AmqpMessage amqpMessage; try { ReceivingAmqpLink deviceBoundReceivingLink = await this.GetDeviceBoundReceivingLinkAsync(cancellationToken); amqpMessage = await deviceBoundReceivingLink.ReceiveMessageAsync(timeout); } catch (Exception exception) { if (exception.IsFatal()) { throw; } throw AmqpClientHelper.ToIotHubClientContract(exception); } if (amqpMessage != null) { message = new Message(amqpMessage) { LockToken = new Guid(amqpMessage.DeliveryTag.Array).ToString() }; } else { message = null; } }, cancellationToken); return(message); }
public override async Task <FeedbackBatch> ReceiveAsync(TimeSpan timeout) { try { ReceivingAmqpLink receivingLink = await faultTolerantReceivingLink.GetReceivingLinkAsync().ConfigureAwait(false); AmqpMessage amqpMessage = await receivingLink.ReceiveMessageAsync(timeout).ConfigureAwait(false); if (amqpMessage != null) { using (amqpMessage) { AmqpClientHelper.ValidateContentType(amqpMessage, CommonConstants.BatchedFeedbackContentType); var records = await AmqpClientHelper.GetObjectFromAmqpMessageAsync <IEnumerable <FeedbackRecord> >(amqpMessage).ConfigureAwait(false); return(new FeedbackBatch { EnqueuedTime = (DateTime)amqpMessage.MessageAnnotations.Map[MessageSystemPropertyNames.EnqueuedTime], LockToken = new Guid(amqpMessage.DeliveryTag.Array).ToString(), Records = records, UserId = Encoding.UTF8.GetString(amqpMessage.Properties.UserId.Array, amqpMessage.Properties.UserId.Offset, amqpMessage.Properties.UserId.Count) }); } } return(null); } catch (Exception exception) { if (exception.IsFatal()) { throw; } throw AmqpClientHelper.ToIotHubClientContract(exception); } }
private static void TestLocalReceiver() { var tfactory = new AmqpConnectionFactory(); var connection = tfactory.OpenConnectionAsync("amqp://localhost:5672", TimeSpan.FromSeconds(10)).Result; var session = connection.CreateSession(new AmqpSessionSettings()); var receicerSettings = new AmqpLinkSettings { LinkName = $"receiver-{DateTime.UtcNow.Ticks}", Role = true, TotalLinkCredit = 300, Source = new Source { Address = QueueName }, Target = new Target() }; var receiver = new ReceivingAmqpLink(session, receicerSettings); Task.WhenAll( session.OpenAsync(TimeSpan.FromSeconds(30)), receiver.OpenAsync(TimeSpan.FromSeconds(30))).Wait(); Console.WriteLine("Listening"); while (true) { AmqpMessage message = receiver.ReceiveMessageAsync(receiver.DefaultOpenTimeout).Result; if (message != null) { receiver.DisposeDelivery(message, true, AmqpConstants.AcceptedOutcome); DisplayMessage(new StreamReader(message.BodyStream).ReadToEnd()); } } }
protected async override Task <Message> OnReceiveAsync(TimeSpan timeout) { AmqpMessage amqpMessage; try { ReceivingAmqpLink deviceBoundReceivingLink = await this.GetDeviceBoundReceivingLinkAsync(); amqpMessage = await deviceBoundReceivingLink.ReceiveMessageAsync(timeout); } catch (Exception exception) { if (exception.IsFatal()) { throw; } throw AmqpClientHelper.ToIotHubClientContract(exception); } Message message; if (amqpMessage != null) { message = new Message(amqpMessage) { LockToken = new Guid(amqpMessage.DeliveryTag.Array).ToString() }; } else { message = null; } return(message); }
internal async Task <Message> ReceiveAmqpMessageAsync(TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(this, $"{nameof(ReceiveAmqpMessageAsync)}"); } var amqpMessage = await _receivingAmqpLink.ReceiveMessageAsync(timeout).ConfigureAwait(false); Message message = null; if (amqpMessage != null) { message = AmqpIoTMessageConverter.AmqpMessageToMessage(amqpMessage); message.LockToken = new Guid(amqpMessage.DeliveryTag.Array).ToString(); } if (Logging.IsEnabled) { Logging.Exit(this, $"{nameof(ReceiveAmqpMessageAsync)}"); } return(message); }