Exemplo n.º 1
0
        static void Main(string[] args)
        {
            try
            {
                // Create the platform session.
                ISession session = GlobalSettings.Sessions.GetSession();

                // Open the session
                session.Open();

                // ********************************************************************************************
                // Headline Alert Endpoint URL.
                // ********************************************************************************************
                const string alertHeadlinesEndpoint = "https://api.refinitiv.com/alerts/v1/news-headlines-subscriptions";

                // Determine if we are using an existing queue or creating one.  The QueueManager will assist us here.
                IQueueManager manager = DeliveryFactory.CreateQueueManager(new QueueManager.Params().Session(session)
                                                                           .Endpoint(alertHeadlinesEndpoint)
                                                                           .OnError((qm, err) => Console.WriteLine(err)));

                // First, check to see if we have any news headline queues active in the cloud, if not, create one.
                List <IQueue> queues = manager.GetAllQueues();

                // Determine if we retrieved anything...create one if not.
                IQueue queue = (queues?.Count > 0 ? queues[0] : manager.CreateQueue());

                // Ensure our queue is created
                if (queue != null)
                {
                    Console.WriteLine($"{Environment.NewLine}{(queues.Count > 0 ? "Using existing" : "Created a new")} queue...");

                    // Start polling for news headline messages from the queue
                    // A QueueSubscriber provides the mechanisms to poll the queue and capture each headline alert via lambda expressions
                    IQueueSubscriber subscriber = DeliveryFactory.CreateQueueSubscriber(
                        new AWSQueueSubscriber.Params().Queue(queue)
                        .WithMessagePollingInterval(1)
                        .OnResponse((s, response) =>
                    {
                        if (response.IsSuccess)
                        {
                            DisplayHeadline(response.Data.Raw);
                        }
                        else
                        {
                            Console.WriteLine(response.Status);
                        }
                    }));

                    // Open the subscriber to begin polling for messages
                    subscriber.StartPolling();
                    Console.WriteLine("Polling for messages from the queue...hit any key to stop polling");

                    // Hit any key to stop...
                    Console.ReadKey();

                    // Close the subscription - stops polling for messages
                    subscriber.StopPolling();
                    Console.WriteLine($"{Environment.NewLine}Stopped polling for messages from the queue.");

                    // Prompt the user to delete the queue
                    Console.Write("Delete the queue (Y/N) [N]: ");
                    var delete = Console.ReadLine();
                    if (delete?.ToUpper() == "Y")
                    {
                        if (manager.DeleteQueue(queue))
                        {
                            Console.WriteLine("Successfully deleted queue.");
                        }
                        else
                        {
                            Console.WriteLine($"Issues deleting queue {manager.Error}");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"\n**************\nFailed to execute: {e.Message}\n***************");
            }
        }
        static void Main(string[] _)
        {
            const string newsStoriesEndpoint = "https://api.refinitiv.com/message-services/v1/news-stories/subscriptions";

            try
            {
                using (ISession session = Configuration.Sessions.GetSession())
                {
                    // Open the session
                    session.Open();

                    // Create our stories definition
                    var definition = Queue.Definition(newsStoriesEndpoint);

                    // Create a QueueManager to actively manage our queues
                    IQueueManager manager = definition.CreateQueueManager().OnError((err, qm) => Console.WriteLine(err));

                    // First, check to see if we have any news headline queues active in the cloud...
                    var queues = manager.GetAllQueues();

                    // If no existing queue exists, create one.
                    IQueueNode queue = (queues.Count > 0 ? queues[0] : manager.CreateQueue());

                    // Ensure our queue is created
                    if (queue != null)
                    {
                        Console.WriteLine($"{Environment.NewLine}{(queues.Count > 0 ? "Using existing" : "Created a new")} queue.  Waiting for stories...");

                        // Subscribe to the queue.
                        // Note: The subscriber interface has 2 mechanisms to retrieve data from the queue.  The first mechanism is to selectively
                        //       poll the queue for new messages.  The second mechanism is to define a callback/lambda expression and notify the
                        //       the subscriber to poll for messages as they come in - this mechansim provides a near realtime result.
                        //
                        // The following example demonstrates the second mechanism.
                        IQueueSubscriber subscriber = definition.CreateAWSSubscriber(queue);

                        // Open the subscriber to begin polling for messages. Use Async() as this method is a long running task.
                        var task = subscriber.StartPollingAsync((story, s) => DisplayStory(story));
                        Console.ReadKey();

                        // Close the subscription - stops polling for messages
                        subscriber.StopPolling();
                        task.GetAwaiter().GetResult();
                        Console.WriteLine("Stopped polling for messages from the queue.");

                        // Prompt the user to delete the queue
                        Console.Write("Delete the queue (Y/N) [N]: ");
                        var delete = Console.ReadLine();
                        if (delete?.ToUpper() == "Y")
                        {
                            if (manager.DeleteQueue(queue))
                            {
                                Console.WriteLine("Successfully deleted queue.");
                            }
                            else
                            {
                                Console.WriteLine($"Issues deleting queue.");
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"\n**************\nFailed to execute: {e.Message}\n{e.InnerException}\n***************");
            }
        }