private Object unaryRequest(String methodName, Object[] args) throws Exception { CallFuture<Object> callFuture = new CallFuture<>(); unaryRequest(methodName, args, callFuture); try { return callFuture.get(); } catch (Exception e) { if (e.getCause() instanceof Exception) { throw (Exception)e.getCause(); } throw new AvroRemoteException(e.getCause()); } }
@Override public void handleError(Throwable error) { future2.handleError(error); } });
@Override public void handleResult(TestRecord result) { future2.handleResult(result); } @Override
@Test public void echoBytes() throws Exception { ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }); // Test synchronous RPC: Assert.assertEquals(byteBuffer, simpleClient.echoBytes(byteBuffer)); // Test asynchronous RPC (future): CallFuture<ByteBuffer> future1 = new CallFuture<>(); simpleClient.echoBytes(byteBuffer, future1); Assert.assertEquals(byteBuffer, future1.get(2, TimeUnit.SECONDS)); Assert.assertNull(future1.getError()); // Test asynchronous RPC (callback): final CallFuture<ByteBuffer> future2 = new CallFuture<>(); simpleClient.echoBytes(byteBuffer, new Callback<ByteBuffer>() { @Override public void handleResult(ByteBuffer result) { future2.handleResult(result); } @Override public void handleError(Throwable error) { future2.handleError(error); } }); Assert.assertEquals(byteBuffer, future2.get(2, TimeUnit.SECONDS)); Assert.assertNull(future2.getError()); }
CallFuture<Void> future = new CallFuture<>(); simpleClient.error(future); try { future.get(2, TimeUnit.SECONDS); Assert.fail("Expected " + TestError.class.getCanonicalName() + " to be thrown"); } catch (ExecutionException e) { e.getCause() instanceof TestError); Assert.assertNotNull(future.getError()); Assert.assertTrue("Expected " + TestError.class.getCanonicalName(), future.getError() instanceof TestError); Assert.assertNull(future.getResult());
CallFuture<T> callFuture = new CallFuture<>(callback); t.transceive(request.getBytes(), new TransceiverCallback<>(request, callFuture)); callFuture.await(); if (request.getMessage().isOneWay()) { Throwable error = callFuture.getError(); if (error != null) { if (error instanceof Exception) {
final CallFuture<Status> callFuture = new CallFuture<Status>();
/** * Helper method that waits for a Status future to come back and validates * that it returns Status == OK. * @param callFuture Future to wait on * @param timeout Time to wait before failing * @param tu Time Unit of {@code timeout} * @throws EventDeliveryException If there is a timeout or if Status != OK */ private void waitForStatusOK(CallFuture<Status> callFuture, long timeout, TimeUnit tu) throws EventDeliveryException { try { Status status = callFuture.get(timeout, tu); if (status != Status.OK) { throw new EventDeliveryException(this + ": Avro RPC call returned " + "Status: " + status); } } catch (CancellationException ex) { throw new EventDeliveryException(this + ": RPC future was cancelled", ex); } catch (ExecutionException ex) { throw new EventDeliveryException(this + ": Exception thrown from " + "remote handler", ex); } catch (TimeoutException ex) { throw new EventDeliveryException(this + ": RPC request timed out", ex); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new EventDeliveryException(this + ": RPC request interrupted", ex); } }
@Test public void add() throws Exception { // Test synchronous RPC: Assert.assertEquals(8, simpleClient.add(2, 6)); // Test asynchronous RPC (future): CallFuture<Integer> future1 = new CallFuture<>(); simpleClient.add(8, 8, future1); Assert.assertEquals(new Integer(16), future1.get(2, TimeUnit.SECONDS)); Assert.assertNull(future1.getError()); // Test asynchronous RPC (callback): final CallFuture<Integer> future2 = new CallFuture<>(); simpleClient.add(512, 256, new Callback<Integer>() { @Override public void handleResult(Integer result) { future2.handleResult(result); } @Override public void handleError(Throwable error) { future2.handleError(error); } }); Assert.assertEquals(new Integer(768), future2.get(2, TimeUnit.SECONDS)); Assert.assertNull(future2.getError()); }
CallFuture<T> callFuture = new CallFuture<T>(callback); t.transceive(request.getBytes(), new TransceiverCallback<T>(request, callFuture)); callFuture.await(); if (request.getMessage().isOneWay()) { Throwable error = callFuture.getError(); if (error != null) { if (error instanceof Exception) {
final CallFuture<Status> callFuture = new CallFuture<Status>();
/** * Helper method that waits for a Status future to come back and validates * that it returns Status == OK. * @param callFuture Future to wait on * @param timeout Time to wait before failing * @param tu Time Unit of {@code timeout} * @throws EventDeliveryException If there is a timeout or if Status != OK */ private void waitForStatusOK(CallFuture<Status> callFuture, long timeout, TimeUnit tu) throws EventDeliveryException { try { Status status = callFuture.get(timeout, tu); if (status != Status.OK) { throw new EventDeliveryException(this + ": Avro RPC call returned " + "Status: " + status); } } catch (CancellationException ex) { throw new EventDeliveryException(this + ": RPC future was cancelled", ex); } catch (ExecutionException ex) { throw new EventDeliveryException(this + ": Exception thrown from " + "remote handler", ex); } catch (TimeoutException ex) { throw new EventDeliveryException(this + ": RPC request timed out", ex); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new EventDeliveryException(this + ": RPC request interrupted", ex); } }
/** * Override as non-synchronized method because the method is thread safe. */ @Override public List<ByteBuffer> transceive(List<ByteBuffer> request) throws IOException { try { CallFuture<List<ByteBuffer>> transceiverFuture = new CallFuture<>(); transceive(request, transceiverFuture); return transceiverFuture.get(); } catch (InterruptedException e) { LOG.debug("failed to get the response", e); return null; } catch (ExecutionException e) { LOG.debug("failed to get the response", e); return null; } }
@Test public void greeting() throws Exception { // Test synchronous RPC: Assert.assertEquals("Hello, how are you?", simpleClient.hello("how are you?")); // Test asynchronous RPC (future): CallFuture<String> future1 = new CallFuture<>(); simpleClient.hello("World!", future1); Assert.assertEquals("Hello, World!", future1.get(2, TimeUnit.SECONDS)); Assert.assertNull(future1.getError()); // Test asynchronous RPC (callback): final CallFuture<String> future2 = new CallFuture<>(); simpleClient.hello("what's up?", new Callback<String>() { @Override public void handleResult(String result) { future2.handleResult(result); } @Override public void handleError(Throwable error) { future2.handleError(error); } }); Assert.assertEquals("Hello, what's up?", future2.get(2, TimeUnit.SECONDS)); Assert.assertNull(future2.getError()); }
final CallFuture<Status> callFuture = new CallFuture<Status>();
@Override public void handleError(Throwable error) { future2.handleError(error); } });
@Override public void handleResult(ByteBuffer result) { future2.handleResult(result); } @Override
/** Writes a request message and reads a response or error message. */ public Object request(String messageName, Object request) throws Exception { // Initialize request Request rpcRequest = new Request(messageName, request, new RPCContext()); CallFuture<Object> future = /* only need a Future for two-way messages */ rpcRequest.getMessage().isOneWay() ? null : new CallFuture<>(); // Send request request(rpcRequest, future); if (future == null) // the message is one-way, so return immediately return null; try { // the message is two-way, wait for the result return future.get(); } catch (ExecutionException e) { if (e.getCause() instanceof Exception) { throw (Exception)e.getCause(); } else { throw new AvroRemoteException(e.getCause()); } } }
@Test public void echo() throws Exception { TestRecord record = TestRecord.newBuilder().setHash( new org.apache.avro.test.MD5( new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 })). setKind(org.apache.avro.test.Kind.FOO). setName("My Record").build(); // Test synchronous RPC: Assert.assertEquals(record, simpleClient.echo(record)); // Test asynchronous RPC (future): CallFuture<TestRecord> future1 = new CallFuture<>(); simpleClient.echo(record, future1); Assert.assertEquals(record, future1.get(2, TimeUnit.SECONDS)); Assert.assertNull(future1.getError()); // Test asynchronous RPC (callback): final CallFuture<TestRecord> future2 = new CallFuture<>(); simpleClient.echo(record, new Callback<TestRecord>() { @Override public void handleResult(TestRecord result) { future2.handleResult(result); } @Override public void handleError(Throwable error) { future2.handleError(error); } }); Assert.assertEquals(record, future2.get(2, TimeUnit.SECONDS)); Assert.assertNull(future2.getError()); }
final CallFuture<Status> callFuture = new CallFuture<Status>();