Exemplo n.º 1
0
        /// <summary>
        ///   Publish messsage to topic.
        /// </summary>
        /// <param name = "topic">The topic to publish the message to. The topic represents a channel.</param>
        /// <param name = "subject">The suject used when publishing to the topic.</param>
        /// <param name = "message">The body message used when publishing to the topic.</param>
        public void Publish(Topic topic, string subject, string message)
        {
            Validate.That(topic).IsNotNull();
            Validate.That(subject).IsNotNullOrEmpty();
            Validate.That(message).IsNotNullOrEmpty();

            amazonSnsFacade.PublishMessageToTopic(topic.TopicArn, subject, message);
        }
        /// <summary>
        /// Publish messsage to topic.
        /// </summary>
        /// <param name="topic">The topic to publish the message to. The topic represents a channel.</param>
        /// <param name="subject">The suject used when publishing to the topic.</param>
        /// <param name="message">The body message used when publishing to the topic.</param>
        public void Publish(Topic topic, string subject, string message)
        {
            Validate.That(topic).IsNotNull();
            Validate.That(subject).IsNotNullOrEmpty();
            Validate.That(message).IsNotNullOrEmpty();

            if (isDisposed)
            {
                throw new ObjectDisposedException(MethodBase.GetCurrentMethod().DeclaringType.Name, "instance has been disposed");
            }

            amazonSnsFacade.PublishMessageToTopic(topic.TopicArn, subject, message);
        }
        /// <summary>
        /// Indicates whether the subscriber is subscribed to the topic.
        /// </summary>
        /// <param name="topic">The topic to publish the message to. The topic represents a channel.</param>
        /// <param name="subscriber">The suject used when publishing to the topic.</param>
        /// <returns></returns>
        public bool IsSubscribed(Topic topic, Subscriber subscriber)
        {
            Validate.That(topic).IsNotNull();
            Validate.That(subscriber).IsNotNull();

            if (isDisposed)
            {
                throw new ObjectDisposedException(MethodBase.GetCurrentMethod().DeclaringType.Name, "instance has been disposed");
            }

            var key = GetKey(topic, subscriber);
            return subscriptions.ContainsKey(key);
        }
Exemplo n.º 4
0
        public Topic GetTopic(string channelName)
        {
            Validate.That(channelName).IsNotNullOrEmpty();

            var topicName = GetTopicName(channelName);
            var topicArn = amazonSnsFacade.CreateOrRetrieveTopic(topicName);
            if (topics.ContainsKey(channelName))
            {
                return topics[channelName];
            }
            var topic = new Topic(topicName, topicArn);
            topics[channelName] = topic;
            return topic;
        }
        /// <summary>
        /// Subscribe subscriber from topic.
        /// </summary>
        /// <param name="topic">The topic to be subscribed to.</param>
        /// <param name="subscriber">The subscriber to be unsubscribed from the topic.</param>
        /// <param name="messageHandler">The delegate for handling the messages.</param>
        /// <returns></returns>
        public SubscriptionInfo Subscribe(Topic topic, Subscriber subscriber, Action<Message> messageHandler)
        {
            Validate.That(topic).IsNotNull();
            Validate.That(subscriber).IsNotNull();
            Validate.That(messageHandler).IsNotNull();

            if (isDisposed)
            {
                throw new ObjectDisposedException(MethodBase.GetCurrentMethod().DeclaringType.Name, "instance has been disposed");
            }

            var key = GetKey(topic, subscriber);
            return subscriptions.AddOrUpdate(key, k => AddSubscription(topic, subscriber, messageHandler), (x,s) => UpdateSubscription(s, messageHandler));
        }
 private static string GetKey(Topic topic, Subscriber subscriber)
 {
     return string.Concat(topic.Name, "-", subscriber.Name);
 }
        private SubscriptionInfo AddSubscription(Topic topic, Subscriber subscriber, Action<Message> messageHandler)
        {
            var subscriptionInfo = new SubscriptionInfo
            {
                Topic=topic,
                Subscriber=subscriber
            };

            amazonSqsFacade.SetSqsPolicyForSnsPublish(subscriber.QueueUrl, subscriber.QueueArn, topic.TopicArn);
            subscriptionInfo.SubscriptionArn = amazonSnsFacade.SubscribeQueueToTopic(subscriptionInfo.Subscriber.QueueArn, subscriptionInfo.Topic.TopicArn);
            if (subscriptionInfo.Subscriber.LongLived)
            {
                resourceCounter.Increment(subscriptionInfo.Subscriber.Name);
            }
            subscriptionInfo.CancelToken = queuePoller.Start(subscriptionInfo, messageHandler);

            return subscriptionInfo;

        }
        /// <summary>
        /// Unsubscribe subscriber from topic.
        /// </summary>
        /// <param name="topic">The topic to be unsubscribed from.</param>
        /// <param name="subscriber">The subscriber to be unsubscribed from the topic.</param>
        /// <returns></returns>
        public SubscriptionInfo Unsubscribe(Topic topic, Subscriber subscriber)
        {
            Validate.That(topic).IsNotNull();
            Validate.That(subscriber).IsNotNull();

            if (isDisposed)
            {
                throw new ObjectDisposedException(MethodBase.GetCurrentMethod().DeclaringType.Name, "instance has been disposed");
            }

            var key = GetKey(topic, subscriber);
            SubscriptionInfo subscriptionInfo;
            if (subscriptions.TryRemove(key, out subscriptionInfo))
            {
                subscriptionInfo.CancelToken.Cancel();
                if (!subscriber.LongLived || resourceCounter.Decrement(subscriptionInfo.Subscriber.Name) == 0)
                {
                    amazonSnsFacade.UnsubscribeQueueFromTopic(subscriptionInfo.SubscriptionArn);
                }
                subscriptionInfo.CancelToken = null;
                subscriptionInfo.SubscriptionArn = null;
            }
            return subscriptionInfo;
        }