Exemplo n.º 1
0
        public static async Task RunAsync(ConnectionInfo cnInfo)
        {
            using (var client = new NatsClient("Sample9", cnInfo))
            {
                client.Connect();

                //Setup wildcard sub
                await client.SubWithHandlerAsync(
                    "demo.>",
                    msgOp => Console.WriteLine($"(1): {msgOp.Subject}:{msgOp.GetPayloadAsString()}"));

                await client.SubWithHandlerAsync(
                    "demo.*",
                    msgOp => Console.WriteLine($"(2): {msgOp.Subject}:{msgOp.GetPayloadAsString()}"));

                Console.WriteLine("Hit key to quit.");
                Console.ReadKey();
            }
        }
Exemplo n.º 2
0
        public static async Task RunAsync(ConnectionInfo cnInfo)
        {
            using (var client = new NatsClient("Sample1", cnInfo))
            {
                client.Connect();

                //Subscribe using handler (action)
                await client.SubWithHandlerAsync(
                    "demo",
                    msg => Console.WriteLine(msg.GetPayloadAsString()));

                Console.WriteLine("Hit key to quit.");
                Console.ReadKey();
            }
        }
Exemplo n.º 3
0
        public static async Task RunAsync(ConnectionInfo cnInfo)
        {
            using (var client = new NatsClient("Sample2", cnInfo))
            {
                client.Connect();

                //Subsribe and unsub after 3 messages
                var subInfo = new SubscriptionInfo("demo", maxMessages: 3);

                await client.SubWithHandlerAsync(
                    subInfo,
                    msg => Console.WriteLine(msg.GetPayloadAsString()));

                Console.WriteLine("Hit key to quit.");
                Console.ReadKey();
            }
        }
Exemplo n.º 4
0
        public void IsMessageReceivedAfterLoggingIsDone()
        {
            var config = new NatsConfiguration
            {
                Host                   = "localhost",
                Port                   = 4222,
                ClientId               = "Serilog.Sinks.Nats.IntegrationTests.Publisher",
                Subject                = "IntegrationTest.TestSubject",
                RequestTimeoutMs       = 1000,
                AutoReconnectOnFailure = true,
                AutoRespondToPing      = true,
                BatchPostingLimit      = 1,
                Credentials            = new Credentials("test", "test"),
                Period                 = TimeSpan.FromMilliseconds(100),
                PubFlushMode           = PubFlushMode.Auto,
                SocketOptions          = new SocketOptions {
                    ConnectTimeoutMs = 1000, ReceiveBufferSize = 50000, ReceiveTimeoutMs = 5000, SendBufferSize = 50000, SendTimeoutMs = 5000
                },
                Verbose = false
            };

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .Enrich.FromLogContext()
                         .WriteTo.Nats(config, new JsonFormatter())
                         .CreateLogger();

            var cnInfo = new ConnectionInfo("localhost")
            {
                Credentials = new Credentials("test", "test"), RequestTimeoutMs = 30000
            };
            var client = new NatsClient("Serilog.Sinks.Nats.IntegrationTests.Subscriber", cnInfo);

            client.Connect();

            var foundMessage = false;

            client.SubWithHandlerAsync(config.Subject, msg => { foundMessage = true; }).ConfigureAwait(false);
            Log.Debug("Test message");

            Thread.Sleep(1000);
            client.Disconnect();

            Assert.True(foundMessage, "No log message received within timeout period");
        }
Exemplo n.º 5
0
        public static async Task RunAsync(ConnectionInfo cnInfo)
        {
            var n = 0;

            using (var client = new NatsClient("Sample1", cnInfo))
            {
                client.Connect();

                //Subscribe using handler (action)
                await client.SubWithHandlerAsync(
                    "demo",
                    msg => n++);

                Console.WriteLine("Hit key to show num of received messages.");
                Console.ReadKey();
            }

            Console.WriteLine(n);
            Console.ReadKey();
        }
Exemplo n.º 6
0
        public static async Task RunAsync(ConnectionInfo cnInfo)
        {
            using (var client = new NatsClient("Sample1", cnInfo))
            {
                client.Connect();

                //Consume JSON
                await client.SubWithHandlerAsync(
                    "demo.greeting",
                    msg =>
                {
                    var greeting = msg.FromJson <Greeting>();

                    Console.WriteLine($"{greeting.SentBy} said: {greeting.Message}");
                });

                Console.WriteLine("Hit key to quit.");
                Console.ReadKey();
            }
        }
Exemplo n.º 7
0
        public static async Task RunAsync(ConnectionInfo cnInfo)
        {
            using (var client = new NatsClient("Sample6", cnInfo))
            {
                client.Connect();

                //Queue groups (start two or more consumers)
                //Queue groups means balancing as only one in the
                //group will get it.
                //NOTE! Others consumers on the same subject will also
                //get the message.
                var subInfo = new SubscriptionInfo("demo", queueGroup: "Grp1");
                await client.SubWithHandlerAsync(
                    subInfo,
                    msgOp => Console.WriteLine(msgOp.GetPayloadAsString()));

                Console.WriteLine("Hit key to quit.");
                Console.ReadKey();
            }
        }
Exemplo n.º 8
0
        public static async Task RunAsync(ConnectionInfo cnInfo)
        {
            using (var client = new NatsClient("Sample7", cnInfo))
            {
                client.Connect();

                //Respond to request's "replyTo"
                await client.SubWithHandlerAsync(
                    "demo-request",
                    msgOp =>
                {
                    var msg = msgOp.GetPayloadAsString();

                    Console.WriteLine($"I got request: {msg}");

                    client.Pub(msgOp.ReplyTo, $"You requested: {msg}");
                });

                Console.WriteLine("Hit key to quit.");
                Console.ReadKey();
            }
        }