Example #1
0
  public static void main(String[] args) throws Exception {
    System.out.println("Connecting to KeyValue Server…");
    TChannel tchannel = new TChannel.Builder("keyvalue-client").build();

    setValue(tchannel, "foo", "bar");
    String value = getValue(tchannel, "foo");

    System.out.println(String.format("{'%s' => '%s'}", "foo", value));

    try {
      String otherValue = getValue(tchannel, "baz");
      System.out.println(String.format("{'%s' => '%s'}", "foo", otherValue));
    } catch (NotFoundError e) {
      System.out.println(String.format("Key '%s' not found.", e.getKey()));
    }

    tchannel.shutdown();

    System.out.println("Disconnected from KeyValue Server.");
  }
Example #2
0
  public static void setValue(TChannel tchannel, String key, String value) throws Exception {
    KeyValue.setValue_args setValue = new KeyValue.setValue_args(key, value);

    ListenableFuture<Response<KeyValue.setValue_result>> future =
        tchannel.callThrift(
            InetAddress.getLocalHost(),
            8888,
            new Request.Builder<>(setValue, "keyvalue-service", "KeyValue::setValue").build(),
            KeyValue.setValue_result.class);

    future.get(100, TimeUnit.MILLISECONDS);
  }
Example #3
0
  public static String getValue(TChannel tchannel, String key)
      throws NotFoundError, UnknownHostException, TimeoutException, ExecutionException,
          InterruptedException, TChannelError {

    KeyValue.getValue_args getValue = new KeyValue.getValue_args(key);

    ListenableFuture<Response<KeyValue.getValue_result>> future =
        tchannel.callThrift(
            InetAddress.getLocalHost(),
            8888,
            new Request.Builder<>(getValue, "keyvalue-service", "KeyValue::getValue").build(),
            KeyValue.getValue_result.class);

    Response<KeyValue.getValue_result> getResult = future.get(100, TimeUnit.MILLISECONDS);

    String value = getResult.getBody().getSuccess();
    NotFoundError err = getResult.getBody().getNotFound();

    if (value == null) {
      throw err;
    }

    return value;
  }