static async Task MainAsync(string[] args)
        {
            // This message source will open a HTTP port to receive POST requests.
            HttpMessageSource <SampleMessage> httpMessageSource = new HttpMessageSourceBuilder <SampleMessage>()
                                                                  .ListenInUrl(_messageSourceUrl)
                                                                  .UseHttpRequestParser(new HttpRequestJsonParser <SampleMessage>())
                                                                  .Build();

            // This message processor will process messages received and published by the message source.
            MessageProcessor <SampleMessage> messageProcessor = new SampleMessageProcessor(httpMessageSource);

            Console.WriteLine("Press any key to start message processing.");
            Console.ReadLine();

            Console.WriteLine("Starting...");
            // Will not block.
            await messageProcessor.StartAsync();

            while (true)
            {
                // Enter raw text to send to the message source.
                Console.WriteLine($"Enter message to send to HTTP message source in {_messageSourceUrl}:");
                string input = Console.ReadLine();

                // Do nothing if input is empty.
                if (string.IsNullOrEmpty(input))
                {
                    continue;
                }

                // Stop if triggered.
                if (string.Equals(input, "stop", StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine("Stopping...");
                    // Wait til last message is finished.
                    await messageProcessor.StopAsync();

                    break;
                }

                // Create message.
                var message = new SampleMessage()
                {
                    Message = input
                };

                Console.WriteLine("-------------------------------------------------------------------------------------------------------------");
                Console.WriteLine($"Sending message to HTTP message source for processing: | Id=[{message.Id}] | Message=[{message.Message}] |");
                Console.WriteLine("-------------------------------------------------------------------------------------------------------------");

                // Send message to the HttpMessageSource.
                await _httpClient.PostAsync(_messageSourceUrl, new StringContent(JsonConvert.SerializeObject(message)));
            }
        }
Пример #2
0
        static async Task MainAsync(string[] args)
        {
            // This queue will be the source of message for InMemoryQueuePollingMessageSource.
            Queue <SampleMessage> queue = new Queue <SampleMessage>();

            // This message source will check the queue for any newly enqueued message every 1 second.
            IMessageSource <SampleMessage> messageSource = new InMemoryQueuePollingMessageSource(queue, pollingInterval: TimeSpan.FromSeconds(1));

            // This message processor will process messages received and published by the message source.
            MessageProcessor <SampleMessage> messageProcessor = new SampleMessageProcessor(messageSource);

            Console.WriteLine("Press any key to start message processing.");
            Console.ReadLine();

            Console.WriteLine("Starting...");
            // Will not block.
            await messageProcessor.StartAsync();

            while (true)
            {
                Console.WriteLine("Enter number of messages to queue:");
                string input = Console.ReadLine();

                if (string.Equals(input, "stop", StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine("Stopping...");
                    // Wait till last message is finished.
                    await messageProcessor.StopAsync();

                    break;
                }

                if (int.TryParse(input, out int num))
                {
                    for (int i = 0; i < num; i++)
                    {
                        var message = new SampleMessage();
                        Console.WriteLine($"Message {message.Id}: Queued for processing.");

                        // Newly queued message will be detected by the InMemoryQueuePollingMessageSource
                        // and will publish the new message to be processed by SampleMessageProcessor.
                        queue.Enqueue(message);
                    }
                }
            }
        }