From b795c33d9074d4510e1e1098800c3e8916ff5416 Mon Sep 17 00:00:00 2001 From: maratische Date: Wed, 16 Mar 2016 22:11:06 +0300 Subject: [PATCH] setConnectionRequestTimeout for fix problem with hang --- pom.xml | 2 +- .../java/org/telegram/telegrambots/api/Constants.java | 1 + .../telegrambots/updatesreceivers/UpdatesThread.java | 9 +++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index cd8949d7..432d99e6 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ UTF-8 UTF-8 2.22.1 - 4.5.1 + 4.5.2 diff --git a/src/main/java/org/telegram/telegrambots/api/Constants.java b/src/main/java/org/telegram/telegrambots/api/Constants.java index aa406e34..8c05d9de 100644 --- a/src/main/java/org/telegram/telegrambots/api/Constants.java +++ b/src/main/java/org/telegram/telegrambots/api/Constants.java @@ -8,4 +8,5 @@ package org.telegram.telegrambots.api; */ public class Constants { public static final String BASEURL = "https://api.telegram.org/bot"; + public static final int SOCKET_TIMEOUT = 30 * 1000; } diff --git a/src/main/java/org/telegram/telegrambots/updatesreceivers/UpdatesThread.java b/src/main/java/org/telegram/telegrambots/updatesreceivers/UpdatesThread.java index 968a64ff..cbe9f650 100644 --- a/src/main/java/org/telegram/telegrambots/updatesreceivers/UpdatesThread.java +++ b/src/main/java/org/telegram/telegrambots/updatesreceivers/UpdatesThread.java @@ -2,6 +2,7 @@ package org.telegram.telegrambots.updatesreceivers; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.entity.BufferedHttpEntity; @@ -57,9 +58,17 @@ public class UpdatesThread { request.setOffset(lastReceivedUpdate + 1); CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionTimeToLive(20, TimeUnit.SECONDS).build(); String url = Constants.BASEURL + token + "/" + GetUpdates.PATH; + //config + RequestConfig defaultRequestConfig = RequestConfig.custom().build(); + RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig) + .setSocketTimeout(Constants.SOCKET_TIMEOUT) + .setConnectTimeout(Constants.SOCKET_TIMEOUT) + .setConnectionRequestTimeout(Constants.SOCKET_TIMEOUT).build(); + //http client HttpPost httpPost = new HttpPost(url); try { httpPost.addHeader("charset", "UTF-8"); + httpPost.setConfig(requestConfig); httpPost.setEntity(new StringEntity(request.toJson().toString(), ContentType.APPLICATION_JSON)); HttpResponse response; response = httpclient.execute(httpPost);