setConnectionRequestTimeout for fix problem with hang

This commit is contained in:
maratische 2016-03-16 22:11:06 +03:00
parent cc38a3c149
commit b795c33d90
3 changed files with 11 additions and 1 deletions

View File

@ -22,7 +22,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<jersey.version>2.22.1</jersey.version> <jersey.version>2.22.1</jersey.version>
<httpcompontents.version>4.5.1</httpcompontents.version> <httpcompontents.version>4.5.2</httpcompontents.version>
</properties> </properties>
<dependencyManagement> <dependencyManagement>

View File

@ -8,4 +8,5 @@ package org.telegram.telegrambots.api;
*/ */
public class Constants { public class Constants {
public static final String BASEURL = "https://api.telegram.org/bot"; public static final String BASEURL = "https://api.telegram.org/bot";
public static final int SOCKET_TIMEOUT = 30 * 1000;
} }

View File

@ -2,6 +2,7 @@ package org.telegram.telegrambots.updatesreceivers;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.entity.BufferedHttpEntity;
@ -57,9 +58,17 @@ public class UpdatesThread {
request.setOffset(lastReceivedUpdate + 1); request.setOffset(lastReceivedUpdate + 1);
CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionTimeToLive(20, TimeUnit.SECONDS).build(); CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionTimeToLive(20, TimeUnit.SECONDS).build();
String url = Constants.BASEURL + token + "/" + GetUpdates.PATH; 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); HttpPost httpPost = new HttpPost(url);
try { try {
httpPost.addHeader("charset", "UTF-8"); httpPost.addHeader("charset", "UTF-8");
httpPost.setConfig(requestConfig);
httpPost.setEntity(new StringEntity(request.toJson().toString(), ContentType.APPLICATION_JSON)); httpPost.setEntity(new StringEntity(request.toJson().toString(), ContentType.APPLICATION_JSON));
HttpResponse response; HttpResponse response;
response = httpclient.execute(httpPost); response = httpclient.execute(httpPost);