org.apache.druid.java.util.http.client.HttpClient.go()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(13.0k)|赞(0)|评价(0)|浏览(148)

本文整理了Java中org.apache.druid.java.util.http.client.HttpClient.go()方法的一些代码示例,展示了HttpClient.go()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpClient.go()方法的具体详情如下:
包路径:org.apache.druid.java.util.http.client.HttpClient
类名称:HttpClient
方法名:go

HttpClient.go介绍

[英]Submit a request and process the response with the given response handler.

Note that the Request object passed in to the HttpClient may be mutated by the actual client. This is largely done by composed clients, but the contract is that mutation is possible. It is the caller's responsibility to pass in a copy of the Request object if they want to have an object that is not mutated.
[中]提交请求并使用给定的响应处理程序处理响应。
请注意,传递给HttpClient的请求对象可能会被实际的客户端修改。这在很大程度上是由组合客户机完成的,但合同规定变异是可能的。如果调用方希望有一个未变异的对象,则调用方有责任传入请求对象的副本。

代码示例

代码示例来源:origin: apache/incubator-druid

/**
 * Executes the request object aimed at the leader and process the response with given handler
 * Note: this method doesn't do retrying on errors or handle leader changes occurred during communication
 */
public <Intermediate, Final> ListenableFuture<Final> goAsync(
  final Request request,
  final HttpResponseHandler<Intermediate, Final> handler
)
{
 return httpClient.go(request, handler);
}

代码示例来源:origin: apache/incubator-druid

@Override
 public <Intermediate, Final> ListenableFuture<Final> go(
   Request request,
   HttpResponseHandler<Intermediate, Final> handler,
   Duration requestReadTimeout
 )
 {
  return delegate.go(creds.addCredentials(request), handler, requestReadTimeout);
 }
}

代码示例来源:origin: apache/incubator-druid

@Override
 public InputStream openStream() throws IOException
 {
  try {
   return httpClient.go(
     new Request(HttpMethod.GET, url),
     new InputStreamResponseHandler()
   ).get();
  }
  catch (InterruptedException e) {
   throw Throwables.propagate(e);
  }
  catch (ExecutionException e) {
   // Unwrap if possible
   Throwables.propagateIfPossible(e.getCause(), IOException.class);
   throw Throwables.propagate(e);
  }
 }
}

代码示例来源:origin: apache/incubator-druid

private FullResponseHolder submitRequest(Request request) throws IOException, ChannelException
{
 try {
  log.debug("HTTP %s: %s", request.getMethod().getName(), request.getUrl().toString());
  return httpClient.go(request, new FullResponseHandler(StandardCharsets.UTF_8), httpTimeout).get();
 }
 catch (Exception e) {
  throw throwIfPossible(e);
 }
}

代码示例来源:origin: apache/incubator-druid

private StatusResponseHolder makeRequest(HttpMethod method, String url)
{
 try {
  StatusResponseHolder response = this.httpClient
    .go(new Request(method, new URL(url)), responseHandler).get();
  if (!response.getStatus().equals(HttpResponseStatus.OK)) {
   throw new ISE("Error while making request to indexer [%s %s]", response.getStatus(), response.getContent());
  }
  return response;
 }
 catch (Exception e) {
  LOG.error(e, "Exception while sending request");
  throw Throwables.propagate(e);
 }
}

代码示例来源:origin: apache/incubator-druid

private StatusResponseHolder makeRequest(HttpMethod method, String url)
 {
  try {
   StatusResponseHolder response = httpClient.go(
     new Request(method, new URL(url)),
     responseHandler
   ).get();
   if (!response.getStatus().equals(HttpResponseStatus.OK)) {
    throw new ISE(
      "Error while making request to url[%s] status[%s] content[%s]",
      url,
      response.getStatus(),
      response.getContent()
    );
   }
   return response;
  }
  catch (Exception e) {
   throw Throwables.propagate(e);
  }
 }
}

代码示例来源:origin: apache/incubator-druid

@Override
 public void run()
 {
  try {
   ListenableFuture<InputStream> go = client.go(
     new Request(HttpMethod.GET, new URL("http://localhost:" + port + "/exception/exception")),
     new InputStreamResponseHandler()
   );
   StringWriter writer = new StringWriter();
   IOUtils.copy(go.get(), writer, "utf-8");
  }
  catch (IOException e) {
   // Expected.
  }
  catch (Throwable t) {
   Throwables.propagate(t);
  }
  latch.countDown();
 }
}

代码示例来源:origin: apache/incubator-druid

public void shutdownSupervisor(String id)
{
 try {
  StatusResponseHolder response = httpClient.go(
    new Request(HttpMethod.POST, new URL(StringUtils.format("%ssupervisor/%s/shutdown", getIndexerURL(), StringUtils.urlEncode(id)))),
    responseHandler
  ).get();
  if (!response.getStatus().equals(HttpResponseStatus.OK)) {
   throw new ISE(
     "Error while shutting down supervisor, response [%s %s]",
     response.getStatus(),
     response.getContent()
   );
  }
  LOG.info("Shutdown supervisor with id[%s]", id);
 }
 catch (Exception e) {
  throw Throwables.propagate(e);
 }
}

代码示例来源:origin: apache/incubator-druid

public String submitSupervisor(String spec)
{
 try {
  StatusResponseHolder response = httpClient.go(
    new Request(HttpMethod.POST, new URL(getIndexerURL() + "supervisor"))
      .setContent(
        "application/json",
        StringUtils.toUtf8(spec)
      ),
    responseHandler
  ).get();
  if (!response.getStatus().equals(HttpResponseStatus.OK)) {
   throw new ISE(
     "Error while submitting supervisor to overlord, response [%s %s]",
     response.getStatus(),
     response.getContent()
   );
  }
  Map<String, String> responseData = jsonMapper.readValue(
    response.getContent(), JacksonUtils.TYPE_REFERENCE_MAP_STRING_STRING
  );
  String id = responseData.get("id");
  LOG.info("Submitted supervisor with id[%s]", id);
  return id;
 }
 catch (Exception e) {
  throw Throwables.propagate(e);
 }
}

代码示例来源:origin: apache/incubator-druid

private List<ListenableFuture<StatusResponseHolder>> sendUpdate(String updatedAuthorizerPrefix, byte[] serializedUserMap)
{
 List<ListenableFuture<StatusResponseHolder>> futures = new ArrayList<>();
 for (NodeType nodeType : NODE_TYPES) {
  DruidNodeDiscovery nodeDiscovery = discoveryProvider.getForNodeType(nodeType);
  Collection<DiscoveryDruidNode> nodes = nodeDiscovery.getAllNodes();
  for (DiscoveryDruidNode node : nodes) {
   URL listenerURL = getListenerURL(node.getDruidNode(), baseUrl, updatedAuthorizerPrefix);
   // best effort, if this fails, remote node will poll and pick up the update eventually
   Request req = new Request(HttpMethod.POST, listenerURL);
   req.setContent(MediaType.APPLICATION_JSON, serializedUserMap);
   BasicAuthDBConfig itemConfig = itemConfigMap.get(updatedAuthorizerPrefix);
   ListenableFuture<StatusResponseHolder> future = httpClient.go(
     req,
     new ResponseHandler(),
     Duration.millis(itemConfig.getCacheNotificationTimeout())
   );
   futures.add(future);
  }
 }
 return futures;
}

代码示例来源:origin: apache/incubator-druid

@Test
 public void testGzipRequestDecompression() throws Exception
 {
  String text = "hello";
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(out)) {
   gzipOutputStream.write(text.getBytes(Charset.defaultCharset()));
  }
  Request request = new Request(HttpMethod.POST, new URL("http://localhost:" + port + "/return"));
  request.setHeader("Content-Encoding", "gzip");
  request.setContent(MediaType.TEXT_PLAIN, out.toByteArray());
  Assert.assertEquals(text, new String(IOUtils.toByteArray(client.go(
    request,
    new InputStreamResponseHandler()
  ).get()), Charset.defaultCharset()));
 }
}

代码示例来源:origin: apache/incubator-druid

public void waitUntilInstanceReady(final HttpClient client, final String host)
 {
  final StatusResponseHandler handler = new StatusResponseHandler(StandardCharsets.UTF_8);
  RetryUtil.retryUntilTrue(
    () -> {
     try {
      StatusResponseHolder response = client.go(
        new Request(HttpMethod.GET, new URL(StringUtils.format("%s/status/health", host))),
        handler
      ).get();
      LOG.info("%s %s", response.getStatus(), response.getContent());
      return response.getStatus().equals(HttpResponseStatus.OK);
     }
     catch (Throwable e) {
      LOG.error(e, "");
      return false;
     }
    },
    "Waiting for instance to be ready: [" + host + "]"
  );
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
@Ignore
// above bug is not fixed in jetty for gzip encoding, and the chunk is still finalized instead of throwing exception.
public void testChunkNotFinalized() throws Exception
{
 ListenableFuture<InputStream> go =
   client.go(
     new Request(HttpMethod.GET, new URL("http://localhost:" + port + "/exception/exception")),
     new InputStreamResponseHandler()
   );
 try {
  StringWriter writer = new StringWriter();
  IOUtils.copy(go.get(), writer, "utf-8");
  Assert.fail("Should have thrown Exception");
 }
 catch (IOException e) {
  // Expected.
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testHttpSilentServerWithRequestTimeout() throws Throwable
{
 final Lifecycle lifecycle = new Lifecycle();
 try {
  final HttpClientConfig config = HttpClientConfig.builder().withReadTimeout(new Duration(86400L * 365)).build();
  final HttpClient client = HttpClientInit.createClient(config, lifecycle);
  final ListenableFuture<StatusResponseHolder> future = client
    .go(
      new Request(HttpMethod.GET, new URL(StringUtils.format("http://localhost:%d/", silentServerSocket.getLocalPort()))),
      new StatusResponseHandler(StandardCharsets.UTF_8),
      new Duration(100L)
    );
  Throwable e = null;
  try {
   future.get();
  }
  catch (ExecutionException e1) {
   e = e1.getCause();
  }
  Assert.assertTrue("ReadTimeoutException thrown by 'get'", e instanceof ReadTimeoutException);
 }
 finally {
  lifecycle.stop();
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testHttpsConnectionClosingServer() throws Throwable
{
 final Lifecycle lifecycle = new Lifecycle();
 try {
  final HttpClientConfig config = HttpClientConfig.builder().withSslContext(SSLContext.getDefault()).build();
  final HttpClient client = HttpClientInit.createClient(config, lifecycle);
  final ListenableFuture<StatusResponseHolder> response = client
    .go(
      new Request(HttpMethod.GET, new URL(StringUtils.format("https://localhost:%d/", closingServerSocket.getLocalPort()))),
      new StatusResponseHandler(StandardCharsets.UTF_8)
    );
  Throwable e = null;
  try {
   response.get();
  }
  catch (ExecutionException e1) {
   e = e1.getCause();
   e1.printStackTrace();
  }
  Assert.assertTrue("ChannelException thrown by 'get'", isChannelClosedException(e));
 }
 finally {
  lifecycle.stop();
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testHttpsSilentServer() throws Throwable
{
 final Lifecycle lifecycle = new Lifecycle();
 try {
  final HttpClientConfig config = HttpClientConfig.builder()
                          .withSslContext(SSLContext.getDefault())
                          .withSslHandshakeTimeout(new Duration(100))
                          .build();
  final HttpClient client = HttpClientInit.createClient(config, lifecycle);
  final ListenableFuture<StatusResponseHolder> response = client
    .go(
      new Request(HttpMethod.GET, new URL(StringUtils.format("https://localhost:%d/", silentServerSocket.getLocalPort()))),
      new StatusResponseHandler(StandardCharsets.UTF_8)
    );
  Throwable e = null;
  try {
   response.get();
  }
  catch (ExecutionException e1) {
   e = e1.getCause();
  }
  Assert.assertTrue("ChannelException thrown by 'get'", e instanceof ChannelException);
 }
 finally {
  lifecycle.stop();
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testHttpSilentServerWithGlobalTimeout() throws Throwable
{
 final Lifecycle lifecycle = new Lifecycle();
 try {
  final HttpClientConfig config = HttpClientConfig.builder().withReadTimeout(new Duration(100)).build();
  final HttpClient client = HttpClientInit.createClient(config, lifecycle);
  final ListenableFuture<StatusResponseHolder> future = client
    .go(
      new Request(HttpMethod.GET, new URL(StringUtils.format("http://localhost:%d/", silentServerSocket.getLocalPort()))),
      new StatusResponseHandler(StandardCharsets.UTF_8)
    );
  Throwable e = null;
  try {
   future.get();
  }
  catch (ExecutionException e1) {
   e = e1.getCause();
  }
  Assert.assertTrue("ReadTimeoutException thrown by 'get'", e instanceof ReadTimeoutException);
 }
 finally {
  lifecycle.stop();
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testHttpConnectionClosingServer() throws Throwable
{
 final Lifecycle lifecycle = new Lifecycle();
 try {
  final HttpClientConfig config = HttpClientConfig.builder().build();
  final HttpClient client = HttpClientInit.createClient(config, lifecycle);
  final ListenableFuture<StatusResponseHolder> response = client
    .go(
      new Request(HttpMethod.GET, new URL(StringUtils.format("http://localhost:%d/", closingServerSocket.getLocalPort()))),
      new StatusResponseHandler(StandardCharsets.UTF_8)
    );
  Throwable e = null;
  try {
   response.get();
  }
  catch (ExecutionException e1) {
   e = e1.getCause();
   e1.printStackTrace();
  }
  Assert.assertTrue("ChannelException thrown by 'get'", isChannelClosedException(e));
 }
 finally {
  lifecycle.stop();
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
 public void testHttpsEchoServer() throws Throwable
 {
  final Lifecycle lifecycle = new Lifecycle();
  try {
   final HttpClientConfig config = HttpClientConfig.builder().withSslContext(SSLContext.getDefault()).build();
   final HttpClient client = HttpClientInit.createClient(config, lifecycle);

   final ListenableFuture<StatusResponseHolder> response = client
     .go(
       new Request(HttpMethod.GET, new URL(StringUtils.format("https://localhost:%d/", echoServerSocket.getLocalPort()))),
       new StatusResponseHandler(StandardCharsets.UTF_8)
     );

   expectedException.expect(ExecutionException.class);
   expectedException.expectMessage("org.jboss.netty.channel.ChannelException: Faulty channel in resource pool");

   response.get();
  }
  finally {
   lifecycle.stop();
  }
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testHttpEchoServer() throws Throwable
{
 final Lifecycle lifecycle = new Lifecycle();
 try {
  final HttpClientConfig config = HttpClientConfig.builder().build();
  final HttpClient client = HttpClientInit.createClient(config, lifecycle);
  final ListenableFuture<StatusResponseHolder> response = client
    .go(
      new Request(HttpMethod.GET, new URL(StringUtils.format("http://localhost:%d/", echoServerSocket.getLocalPort()))),
      new StatusResponseHandler(StandardCharsets.UTF_8)
    );
  expectedException.expect(ExecutionException.class);
  expectedException.expectMessage("java.lang.IllegalArgumentException: invalid version format: GET");
  response.get();
 }
 finally {
  lifecycle.stop();
 }
}

相关文章