Example #1
0
        public void Unsubscribe(string topicName, string subscriberReference)
        {
            SystemWideLogging.LogServiceClient.LogEvent("MessageBus :: VideoMessageBus\\SubscriptionService.cs :: public void Unsubscribe(string topicName, string subscriberReference)", "Unsubscription Request for " + topicName + " received");

            ConsoleHelper.WriteLine(ConsoleColor.Yellow, "Unsubscription for " + topicName + " received");
            SubscriptionRegistry.RemoveSubscriber(topicName, subscriberReference);
        }
        public void Publish(VideoMessage message)
        {
            if (message is DeliveryRequestMessage)
            {
                SystemWideLogging.LogServiceClient.LogEvent("MessageBus :: VideoMessageBus\\PublisherService.cs :: public void Publish(VideoMessage message)", "Received DeliveryRequestMessage Publish Request from VideoStore ( message.TopicName=" + message.TopicName + " )");


                var deliveryMessage = message as DeliveryRequestMessage;

                // Find the scubscriber who matches with region of delivery address
                string keyword = deliveryMessage.TopicName + "/" + deliveryMessage.RegionName;

                IEnumerable <string> subscribers = SubscriptionRegistry.GetSubscribers(keyword);

                if (subscribers == null ||
                    ((IList <string>)subscribers).Count == 0)
                {
                    // Save the message

                    SystemWideLogging.LogServiceClient.LogEvent("MessageBus :: VideoMessageBus\\PublisherService.cs :: public void Publish(VideoMessage message)", "Can't find any subscribers, storing message in queue ( message.TopicName=" + message.TopicName + " )");


                    PutMessageIntoQueue(message);
                    return;
                }


                foreach (string subscriberAddress in subscribers)
                {
                    var service = ServiceFactory.GetService <ISubscriberService>(subscriberAddress);

                    if (service == null)
                    {
                        SystemWideLogging.LogServiceClient.LogEvent("MessageBus :: VideoMessageBus\\PublisherService.cs :: public void Publish(VideoMessage message)", "Following service was unsubscribed because no such address is available: " + subscriberAddress);


                        PutMessageIntoQueue(message);

                        // It must be a dead service so unsubscribe it
                        SubscriptionRegistry.RemoveSubscriber(message.TopicName, subscriberAddress);

                        ConsoleHelper.WriteLine(ConsoleColor.Yellow,
                                                "Following service was unsubscribed because no such address is available: " +
                                                subscriberAddress);
                        return;
                    }

                    try
                    {
                        SystemWideLogging.LogServiceClient.LogEvent("MessageBus :: VideoMessageBus\\PublisherService.cs :: public void Publish(VideoMessage message)", "Forwarding message to subscriber ( subscriberAddress=" + subscriberAddress + " , message.TopicName=" + message.TopicName + " )");


                        service.PublishToSubscriber(message);

                        Console.WriteLine("Request sent to " + subscriberAddress);
                    }
                    catch
                    {
                        // It must be a dead service so unsubscribe it
                        SubscriptionRegistry.RemoveSubscriber(message.TopicName, subscriberAddress);

                        ConsoleHelper.WriteLine(ConsoleColor.Red,
                                                "An failue has occurred to publish the message to " + subscriberAddress +
                                                " so the service was unsubscribed!");
                    }
                }
            }
        }