Example #1
0
        private ClosedCallback HandleClosed(TaskCompletionSource <bool> tsc) => (sender, error) =>
        {
            NMSException exception = ExceptionSupport.GetException(error, "Received Amqp link detach with Error for link {0}", info.Id);
            if (!tsc.TrySetException(exception))
            {
                session.RemoveConsumer(info.Id);

                // If session is not closed it means that the link was remotely detached
                if (!receiverLink.Session.IsClosed)
                {
                    session.Connection.Provider.FireResourceClosed(info, exception);
                }
            }
        };
        public Task DestroyResource(ResourceInfo resourceInfo)
        {
            switch (resourceInfo)
            {
            case SessionInfo sessionInfo:
            {
                AmqpSession session = connection.GetSession(sessionInfo.Id);
                session.Close();
                return(Task.CompletedTask);
            }

            case ConsumerInfo consumerInfo:
            {
                AmqpSession  session  = connection.GetSession(consumerInfo.SessionId);
                AmqpConsumer consumer = session.GetConsumer(consumerInfo.Id);
                consumer.Close();
                session.RemoveConsumer(consumerInfo.Id);
                return(Task.CompletedTask);
            }

            case ProducerInfo producerInfo:
            {
                AmqpSession  session  = connection.GetSession(producerInfo.SessionId);
                AmqpProducer producer = session.GetProducer(producerInfo.Id);
                producer.Close();
                session.RemoveProducer(producerInfo.Id);
                return(Task.CompletedTask);
            }

            case NmsTemporaryDestination temporaryDestination:
            {
                AmqpTemporaryDestination amqpTemporaryDestination = connection.GetTemporaryDestination(temporaryDestination);
                if (amqpTemporaryDestination != null)
                {
                    amqpTemporaryDestination.Close();
                    connection.RemoveTemporaryDestination(temporaryDestination.Id);
                }
                else
                {
                    Tracer.Debug($"Could not find temporary destination {temporaryDestination.Id} to delete.");
                }

                return(Task.CompletedTask);
            }

            default:
                throw new ArgumentOutOfRangeException(nameof(resourceInfo), "Not supported resource type.");
            }
        }
        public Task Attach()
        {
            Target target = new Target();
            Source source = CreateSource();

            Attach attach = new Attach
            {
                Target        = target,
                Source        = source,
                RcvSettleMode = ReceiverSettleMode.First,
                SndSettleMode = (info.IsBrowser) ? SenderSettleMode.Settled : SenderSettleMode.Unsettled,
            };
            string name;

            if (info.IsDurable)
            {
                name = info.SubscriptionName;
            }
            else
            {
                string destinationAddress = source.Address ?? "";
                name = "nms:receiver:" + info.Id
                       + (destinationAddress.Length == 0 ? "" : (":" + destinationAddress));
            }

            var taskCompletionSource = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            link = new ReceiverLink(session.UnderlyingSession, name, attach, (link1, attach1) => { taskCompletionSource.SetResult(true); });

            link.AddClosedCallback((sender, error) =>
            {
                NMSException exception = ExceptionSupport.GetException(error, "Received Amqp link detach with Error for link {0}", info.Id);
                if (!taskCompletionSource.TrySetException(exception))
                {
                    session.RemoveConsumer(info.Id);

                    // If session is not closed it means that the link was remotely detached
                    if (!link.Session.IsClosed)
                    {
                        session.Connection.Provider.FireResourceClosed(info, exception);
                    }
                }
            });
            return(taskCompletionSource.Task);
        }