Skip to content

Instantly share code, notes, and snippets.

@alexandrebl
Created February 5, 2022 20:13
Show Gist options
  • Select an option

  • Save alexandrebl/e0dd44fd2ce2c27ede1681233eddca4f to your computer and use it in GitHub Desktop.

Select an option

Save alexandrebl/e0dd44fd2ce2c27ede1681233eddca4f to your computer and use it in GitHub Desktop.
NATS.Queue.Group.Subscriber.cs
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