Ejemplo n.º 1
0
        public void LinkSubscriber(Session session, HostItemSubscriber subscriber,
                                   ISubscriptionSettings subscriptionSettings)
        {
            var topicAttribute = (TopicAttribute)subscriber.HostItemAttribute;

            // Determine if the host application has provided a specific subscription
            // that should be used for the name specified in code.

            string hostName = subscriber.HostAttribute.HostName;

            var mapping = new SubscriptionMapping(hostName, topicAttribute.TopicName,
                                                  topicAttribute.SubscriptionName);

            string subscriptionName = subscriptionSettings.GetMappedSubscription(mapping)
                                      ?? topicAttribute.SubscriptionName;

            var logger = LoggerFactory.CreateLogger <TopicSubscriberLinker>();

            logger.LogInformation(
                "Subscribing to topic {topicName} as subscription {subscriptionName}",
                topicAttribute.TopicName,
                subscriptionName);

            // Create a AMQP receiver link used to register handler method:
            var receiverLink = new ReceiverLink(session, Guid.NewGuid().ToString(),
                                                $"{topicAttribute.TopicName}/Subscriptions/{subscriptionName}");

            ReceiveMessages(subscriber, receiverLink);
        }
Ejemplo n.º 2
0
        public void LinkSubscriber(Session session, HostItemSubscriber subscriber,
                                   ISubscriptionSettings subscriptionSettings)
        {
            var queueAttribute = (QueueAttribute)subscriber.HostItemAttribute;

            // Create a AMQP receiver link used to register handler method:
            var receiverLink = new ReceiverLink(session,
                                                Guid.NewGuid().ToString(),
                                                queueAttribute.QueueName);

            var logger = LoggerFactory.CreateLogger <QueueSubscriberLinker>();

            logger.LogInformation("Subscribing to queue {named}", queueAttribute.QueueName);

            ReceiveMessages(subscriber, receiverLink);
        }
Ejemplo n.º 3
0
        private void InvokeHandler(
            IReceiverLink receiverLink,
            Message amqpMessage,
            IMessage message,
            HostItemSubscriber subscriber)
        {
            try
            {
                DispatchModule.InvokeDispatcherInNewLifetimeScopeAsync(
                    subscriber.DispatchInfo,
                    message).GetAwaiter().GetResult();

                receiverLink.Accept(amqpMessage);
            }
            catch (Exception ex)
            {
                LogMessageReceiveEx(ex, message, subscriber.DispatchInfo);
                receiverLink.Reject(amqpMessage);
            }
        }
Ejemplo n.º 4
0
        protected void ReceiveMessages(HostItemSubscriber subscriber, IReceiverLink receiverLink)
        {
            subscriber.SetReceiverLink(receiverLink);

            receiverLink.Closed += (sender, error) => { LogItemClosed(error); };

            // Start message pump that will be called when a message
            // is published to the host item.
            receiverLink.Start(subscriber.HostItemAttribute.LinkCredit, (receiver, amqpMessage) =>
            {
                // Deserialize the message body into the the .NET types associated
                // with the handler to which the message should be dispatched.
                Type messageType = subscriber.DispatchInfo.MessageType;
                IMessage message = DeserializeMessage(amqpMessage, messageType);

                SetMessageApplicationProperties(amqpMessage, message);

                // Invoke the handler associated with the message.
                InvokeHandler(receiver, amqpMessage, message, subscriber);
            });
        }