public Task Attach() { Source source = CreateSource(); Target target = CreateTarget(); Attach frame = new Attach { Source = source, Target = target, SndSettleMode = SenderSettleMode.Unsettled, IncompleteUnsettled = false, InitialDeliveryCount = 0 }; string linkName = info.Id + ":" + target.Address; var taskCompletionSource = new TaskCompletionSource <bool>(); senderLink = new SenderLink(session.UnderlyingSession, linkName, frame, HandleOpened(taskCompletionSource)); senderLink.AddClosedCallback((sender, error) => { NMSException exception = ExceptionSupport.GetException(error, "Received Amqp link detach with Error for link {0}", info.Id); if (!taskCompletionSource.TrySetException(exception)) { session.RemoveProducer(info.Id); // If session is not closed it means that the link was remotely detached if (!senderLink.Session.IsClosed) { session.Connection.Provider.FireResourceClosed(info, exception); } } }); return(taskCompletionSource.Task); }
private async Task <SenderLink> CreateSenderLink(string address, CancellationToken cancellationToken) { var tcs = TaskUtil.CreateTaskCompletionSource <bool>(cancellationToken); var senderLink = new SenderLink(_session, Guid.NewGuid().ToString(), new Target { Address = address }, OnAttached); senderLink.AddClosedCallback(OnClosed); await tcs.Task.ConfigureAwait(false); senderLink.Closed -= OnClosed; return(senderLink); void OnAttached(ILink link, Attach attach) { if (attach != null) { tcs.TrySetResult(true); } } void OnClosed(IAmqpObject sender, Error error) { if (error != null) { tcs.TrySetException(new CreateRpcClientException(error.Description, error.Condition)); } } }
static void Main(string[] args) { string address = "amqp://*****:*****@localhost:5672"; Connection connection = new Connection(new Address(address)); Session session = new Session(connection); SenderLink producerLink = new SenderLink(session, "test-sender", "amqp1"); producerLink.AddClosedCallback((sender, error) => { if (sender.IsClosed) { System.Console.WriteLine(error.Description); } }); Message message1 = new Message("Hello AMQP!"); message1.MessageAnnotations = new Amqp.Framing.MessageAnnotations(); message1.MessageAnnotations.Map.Add("metadata", "meatadata"); message1.Header = new Amqp.Framing.Header(); message1.Header.DeliveryCount = 0; //senderLink.Send(message1); //expiring message sending producerLink.Send(message1, TimeSpan.FromMinutes(5)); Console.WriteLine("Hello World!"); producerLink.Close(); Thread.Sleep(5000); }
public async Task <TransactionCoordinator> CreateAsync(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); cancellationToken.Register(() => _tcs.TrySetCanceled()); var attach = new Attach { Target = new Coordinator { Capabilities = new[] { TxnCapabilities.LocalTransactions } }, Source = new Source(), SndSettleMode = SenderSettleMode.Unsettled, RcvSettleMode = ReceiverSettleMode.First }; var senderLink = new SenderLink(_session, GetName(), attach, OnAttached); senderLink.AddClosedCallback(OnClosed); await _tcs.Task.ConfigureAwait(false); var transactionCoordinator = new TransactionCoordinator(senderLink); senderLink.Closed -= OnClosed; return(transactionCoordinator); }
public async Task <IProducer> CreateAsync(ProducerConfiguration configuration, CancellationToken cancellationToken) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (string.IsNullOrWhiteSpace(configuration.Address)) { throw new ArgumentNullException(nameof(configuration.Address), "The address cannot be empty."); } cancellationToken.ThrowIfCancellationRequested(); cancellationToken.Register(() => _tcs.TrySetCanceled()); var target = new Target { Address = configuration.Address, Capabilities = GetCapabilities(configuration) }; var senderLink = new SenderLink(_session, Guid.NewGuid().ToString(), target, OnAttached); senderLink.AddClosedCallback(OnClosed); await _tcs.Task.ConfigureAwait(false); configuration.MessageIdPolicy ??= _messageIdPolicyFactory(); var producer = new Producer(_loggerFactory, senderLink, _transactionsManager, configuration); senderLink.Closed -= OnClosed; return(producer); }
public async Task <IAnonymousProducer> CreateAsync(AnonymousProducerConfiguration configuration, CancellationToken cancellationToken) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } cancellationToken.ThrowIfCancellationRequested(); cancellationToken.Register(() => _tcs.TrySetCanceled()); var target = new Target { Address = null, }; var senderLink = new SenderLink(_session, Guid.NewGuid().ToString(), target, OnAttached); senderLink.AddClosedCallback(OnClosed); await _tcs.Task.ConfigureAwait(false); var producer = new AnonymousProducer(_loggerFactory, senderLink, _transactionsManager, configuration); senderLink.Closed -= OnClosed; return(producer); }