Как использовать apache HttpClient для соединения с одним и тем же хостом, чтобы публиковать постоянные множественные события в многопоточном режиме - PullRequest
0 голосов
/ 24 января 2020

Я использую org. apache .httpcomponents.httpclient: 4.2.1.

мое требование: Я должен продолжать публиковать события на одном хосте в нескольких потоках. Я могу опубликовать событие на веб-сервере, но в журнале я вижу много исключений. Может кто-нибудь, пожалуйста, помогите мне с этим, почему это происходит и каково решение.

Примечание: я должен исправить эту проблему с помощью той же версии jar HTTP-клиента.

мой подход:

class HttpPostHandler {
 private static PoolingClientConnectionManager connectionManager;
 private static HttpClient httpClient;
 static {
      connectionManager = new PoolingClientConnectionManager();
      connectionManager.setDefaultMaxPerRoute(20);
      DefaultHttpClient  defaultHttpClient = new DefaultHttpClient(connectionManager);
      httpClient = new AutoRetryHttpClient(defaultHttpClient);
  }
  public void post(){
   mUrl = new URL( notifyTarget );
   post = new HttpPost(notifyTarget);
   HttpHost targetHost = new HttpHost(mUrl.getHost(), mUrl.getPort(), mUrl.getProtocol());
   executor.submit(new HttpRequest(post, targetHost));
  }

  class HttpRequest implements Callable<Boolean> {
    HttpPost postM = null;
    HttpHost targetHost = null;

    HttpRequest(HttpPost post, HttpHost targetHost) {
    postM = post;
    this.targetHost = targetHost;
  }
   @Override
    public Boolean call() throws Exception {
    HttpResponse response = null;
    try {
      response = httpClient.execute(targetHost, postM);
      int resCode = response.getStatusLine().getStatusCode();
      log.debug("->call() Got response to post request, response-code: " + resCode);
    } catch (Exception e) {
      log.error("MMCC notification failed with: " , e);
      return Boolean.valueOf(false);
    } finally {
        postM.releaseConnection();
    }
  } 
}

Исключение, которое я получаю:

Исключение 1:

2020-01-24 02:34:23,227 ERROR [com.learn.CallEventNotifier] (pool-65-thread-3) <AppSess=ApplicationSession-2-40003@15.112.153.11> MMCC notification failed with: : java.io.InterruptedIOException: Connection has been shut down
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:608) [httpclient-4.2.1.jar:4.2.1]
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) [httpclient-4.2.1.jar:4.2.1]
        at org.apache.http.impl.client.AutoRetryHttpClient.execute(AutoRetryHttpClient.java:156) [httpclient-4.2.1.jar:4.2.1]
        at org.apache.http.impl.client.AutoRetryHttpClient.execute(AutoRetryHttpClient.java:113) [httpclient-4.2.1.jar:4.2.1]
        at com.learn.CallEventNotifier$HttpRequest.call(CallEventNotifier.java:240) [mmcc-core-2.3.2-3-SNAPSHOT.jar:]
        at com.learn.CallEventNotifier$HttpRequest.call(CallEventNotifier.java:223) [mmcc-core-2.3.2-3-SNAPSHOT.jar:]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_45]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_45]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_45]
        at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_45]
Caused by: org.apache.http.impl.conn.ConnectionShutdownException
        at org.apache.http.impl.conn.ManagedClientConnectionImpl.ensureConnection(ManagedClientConnectionImpl.java:109) [httpclient-4.2.1.jar:4.2.1]
        at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseEntity(ManagedClientConnectionImpl.java:185) [httpclient-4.2.1.jar:4.2.1]
        at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:302) [httpcore-4.2.1.jar:4.2.1]
        at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127) [httpcore-4.2.1.jar:4.2.1]
        at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:712) [httpclient-4.2.1.jar:4.2.1]
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:517) [httpclient-4.2.1.jar:4.2.1]
        ... 9 more

Исключение 2:

2020-01-24 02:10:26,012 ERROR [com.learn.CallEventNotifier] (pool-65-thread-3) <AppSess=ApplicationSession-2-40003@15.112.153.11> MMCC notification failed with: : java.lang.IllegalStateException: Connection is not open
        at org.apache.http.impl.SocketHttpClientConnection.assertOpen(SocketHttpClientConnection.java:84) [httpcore-4.2.1.jar:4.2.1]
        at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseEntity(AbstractHttpClientConnection.java:301) [httpcore-4.2.1.jar:4.2.1]
        at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseEntity(ManagedClientConnectionImpl.java:186) [httpclient-4.2.1.jar:4.2.1]
        at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:302) [httpcore-4.2.1.jar:4.2.1]
        at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127) [httpcore-4.2.1.jar:4.2.1]
        at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:712) [httpclient-4.2.1.jar:4.2.1]
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:517) [httpclient-4.2.1.jar:4.2.1]
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) [httpclient-4.2.1.jar:4.2.1]
        at org.apache.http.impl.client.AutoRetryHttpClient.execute(AutoRetryHttpClient.java:156) [httpclient-4.2.1.jar:4.2.1]
        at org.apache.http.impl.client.AutoRetryHttpClient.execute(AutoRetryHttpClient.java:113) [httpclient-4.2.1.jar:4.2.1]
        at com.learn.CallEventNotifier$HttpRequest.call(CallEventNotifier.java:240) [mmcc-core-2.3.2-3-SNAPSHOT.jar:]
        at com.learn.CallEventNotifier$HttpRequest.call(CallEventNotifier.java:223) [mmcc-core-2.3.2-3-SNAPSHOT.jar:]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_45]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_45]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_45]
        at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_45]
...