From d317a85a7b76ee94ebc31c218dc877bfadd54902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Wed, 28 Mar 2012 16:48:44 +0200 Subject: Client support for asynchronous requests. --- .../com/orbekk/protobuf/SimpleProtobufClient.java | 43 +++++++++++++--------- 1 file changed, 26 insertions(+), 17 deletions(-) (limited to 'src/main/java/com/orbekk/protobuf/SimpleProtobufClient.java') diff --git a/src/main/java/com/orbekk/protobuf/SimpleProtobufClient.java b/src/main/java/com/orbekk/protobuf/SimpleProtobufClient.java index ffaedb0..b17c0bc 100644 --- a/src/main/java/com/orbekk/protobuf/SimpleProtobufClient.java +++ b/src/main/java/com/orbekk/protobuf/SimpleProtobufClient.java @@ -1,26 +1,35 @@ package com.orbekk.protobuf; -import java.net.Socket; -import java.net.UnknownHostException; -import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.logging.Logger; + +import com.google.protobuf.RpcCallback; public class SimpleProtobufClient { + static final Logger logger = + Logger.getLogger(SimpleProtobufClient.class.getName()); + public void run() { - try { - Socket socket = new Socket("localhost", 10000); - Rpc.Request r1 = Rpc.Request.newBuilder() - .setFullServiceName("com.orbekk.protobuf.TestService") - .setMethodName("Run") + RpcChannel channel = RpcChannel.create("localhost", 10000); + Test.TestService test = Test.TestService.newStub(channel); + Test.TestRequest request = Test.TestRequest.newBuilder() + .setId("Hello!") .build(); - Rpc.Request r2 = Rpc.Request.newBuilder() - .setFullServiceName("Service2") - .build(); - r1.writeDelimitedTo(socket.getOutputStream()); - r2.writeDelimitedTo(socket.getOutputStream()); - } catch (UnknownHostException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + int count = 10; + final CountDownLatch stop = new CountDownLatch(count); + for (int i = 0; i < count; i++) { + logger.info("Sending request."); + test.run(null, request, new RpcCallback() { + @Override public void run(Test.TestResponse response) { + System.out.println("Response from server: " + response); + stop.countDown(); + } + }); + } + try { + stop.await(); + } catch (InterruptedException e) { + // Stop waiting. } } -- cgit v1.2.3