Created
February 5, 2022 20:13
-
-
Save alexandrebl/e0dd44fd2ce2c27ede1681233eddca4f to your computer and use it in GitHub Desktop.
NATS.Queue.Group.Subscriber.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using NATS.Client; | |
| using System; | |
| using System.Threading; | |
| namespace NatsSub1 | |
| { | |
| internal class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| // Define variáveis | |
| var subscription = "order"; | |
| // Obtem a conexão | |
| var connection = GetConnection(); | |
| // Assinatura | |
| var sAsync = connection.SubscribeAsync(subscription, "finance"); | |
| // Processamento do evento a cada mensagem recebida | |
| sAsync.MessageHandler += (sender, args) => | |
| { | |
| Console.WriteLine($"{DateTime.Now:F} - Received: {args.Message}"); | |
| Thread.Sleep(2000); | |
| }; | |
| // Inicia a leitura da assinatura | |
| sAsync.Start(); | |
| // Aguarda uma tecla ser pressionada | |
| Console.ReadLine(); | |
| // Desconectando | |
| sAsync.Unsubscribe(); | |
| connection.Drain(); | |
| connection.Close(); | |
| } | |
| /// <summary> | |
| /// Obtem a assinatura | |
| /// </summary> | |
| /// <param name="args">argumentos de linha de comando</param> | |
| /// <returns>assinatura</returns> | |
| public static string GetSubscription(String[] args) => args[0]; | |
| /// <summary> | |
| /// Obtem uma nova conexão | |
| /// </summary> | |
| /// <returns>nova conexão com NATs.io</returns> | |
| public static IConnection GetConnection() | |
| { | |
| var cf = new ConnectionFactory(); | |
| var c = cf.CreateConnection(); | |
| return c; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment