Exemple #1
0
  public <T> T call(
      String queue, long timeout, MediaType mediaType, Object payload, Class<T> responseClass)
      throws IOException, TimeoutException, InterruptedException {
    String encoding = MediaTypes.getCharset(mediaType);

    byte[] body = getBytes(mediaType, payload);

    AMQP.BasicProperties.Builder bpb = new AMQP.BasicProperties.Builder();
    if (encoding != null) {
      bpb.contentEncoding(encoding);
    }

    RpcResponse response = call(queue, timeout, bpb.build(), body);

    MediaType responseMediaType = MediaType.valueOf(response.getProperties().getContentType());
    if (MediaTypes.isError(responseMediaType)) {
      RpcError rpcError;
      try {
        rpcError = fromBytes(responseMediaType, response.getBody(), RpcError.class);
      } catch (IllegalArgumentException e) {
        throw new IOException("unknown remote error");
      }

      if (rpcError == null) {
        throw new IOException("unknown remote error");
      } else {
        throw new IOException(rpcError.error + "\n" + rpcError.trace);
      }

    } else {
      return fromBytes(responseMediaType, response.getBody(), responseClass);
    }
  }