如何从jestclient池中重新选择链接

zf2sa74q  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(0)|浏览(242)

我遇到了一个“nohttpresponseexception”异常,看起来好像我从连接池中选择了一个服务器单方面关闭的链接,之后我又重复了一遍,但还是会返回这个异常,我觉得如果我在重试时能够从连接池中选择一个新的连接,可以解决这个问题,所以我想知道如何在jestclient连接池中重试时选择一个新链接。

  1. @Data
  2. @Configuration
  3. public class JestClientConfig {
  4. private String url;
  5. private Boolean multiThreaded;
  6. private Integer maxTotalConnectionPerRoute;
  7. private Integer maxTotalConnection;
  8. private Integer connTimeout;
  9. private Integer readTimeout;
  10. private Integer connectionIdleTime;
  11. @Bean
  12. public JestClient getJestClient() {
  13. String[] uriArr = url.split(",");
  14. Set<String> serverUris = new LinkedHashSet<String>(Arrays.asList(uriArr));
  15. JestClientFactory factory = new JestClientFactory();
  16. factory.setHttpClientConfig(new HttpClientConfig.Builder(serverUris)
  17. .multiThreaded(multiThreaded)
  18. .defaultMaxTotalConnectionPerRoute(maxTotalConnectionPerRoute)
  19. .maxTotalConnection(maxTotalConnection)
  20. .maxConnectionIdleTime(connectionIdleTime, TimeUnit.MILLISECONDS)
  21. .connTimeout(connTimeout)
  22. .readTimeout(readTimeout)
  23. .gson(new GsonBuilder().setDateFormat(DateUtil.TimeFormat.NORMAL_PATTERN.getValue()).create())
  24. .build());
  25. return factory.getObject();
  26. }
  27. }
  28. use client:
  29. @Resource
  30. @Qualifier(value = "getJestClient")
  31. private JestClient jestClient;
  32. @Autowired
  33. private JestClientConfig jestClientConfig;
  34. retry:
  35. public <T extends JestResult> T execute(Action<T> clientRequest, int retryTimes) {
  36. if (retryTimes <= 0) {
  37. return null;
  38. }
  39. try {
  40. return ngccJestClient.execute(clientRequest);
  41. } catch (Exception e) {
  42. LOGGER.warn("ElasticsearchService.execute(): execution met an error, retryTimes = {}", retryTimes, e);
  43. return execute(clientRequest, --retryTimes);
  44. }
  45. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题