コード例 #1
0
        static async Task RPCCallsAsync()
        {
            // Queue
            var queue = "rpc_queue";

            // RPC Server
            using var server = new RabbitMQEx(factory);
            server.PreparePRCServer((body, routingKey, redelivered) => {
                var message = Encoding.UTF8.GetString(body.ToArray());
                // Console.WriteLine($"Message received at {DateTime.UtcNow:ss:fff} from {routingKey} : {message}");

                var bytes = Encoding.UTF8.GetBytes("Echo: " + message);
                return(Task.FromResult(new ReadOnlyMemory <byte>(bytes)));
            }, queue);

            // PRC Client
            using var client = new RabbitMQEx(server.Connection);
            client.PreparePRCClient();

            var inputs = new string[] { "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10" };

            foreach (var input in inputs)
            {
                Console.WriteLine($"Message sent at {DateTime.UtcNow:ss:fff}: {input}");
                var result = await client.PRCCallAsync(Encoding.UTF8.GetBytes(input), queue);

                Console.WriteLine($"Client received at {DateTime.UtcNow:ss:fff} for {input}: {Encoding.UTF8.GetString(result.Span)}");
            }

            Console.ReadKey();
        }
コード例 #2
0
        static void PRCParallelCalls()
        {
            // Queue
            var queue = "rpc_queue";

            // RPC Server
            using var server = new RabbitMQEx(factory);
            server.PreparePRCServer((body, routingKey, redelivered) => {
                var message = Encoding.UTF8.GetString(body.ToArray());
                //Console.WriteLine($"Message received at {DateTime.UtcNow:ss:fff} from {routingKey} : {message}");

                var bytes = Encoding.UTF8.GetBytes("Echo: " + message);
                return(Task.FromResult(new ReadOnlyMemory <byte>(bytes)));
            }, queue);

            Console.ReadKey();
        }