From d0950a23e1be9946e2970436d0d7113bfb6aac53 Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Sun, 8 May 2016 01:54:53 +0200 Subject: [PATCH] Possible improvements, may be unestable --- .../telegrambots/api/objects/Message.java | 14 +++--- .../updatesreceivers/BotSession.java | 46 +++++++++---------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/api/objects/Message.java b/src/main/java/org/telegram/telegrambots/api/objects/Message.java index 126cb67f..6f68a03a 100644 --- a/src/main/java/org/telegram/telegrambots/api/objects/Message.java +++ b/src/main/java/org/telegram/telegrambots/api/objects/Message.java @@ -61,7 +61,7 @@ public class Message implements IBotApiObject { @JsonProperty(FORWARDFROM_FIELD) private User forwardFrom; ///< Optional. For forwarded messages, sender of the original message @JsonProperty(FORWARDFROMCHAT_FIELD) - private Chat forwardedFromChat; ///< Optional. For messages forwarded from a channel, information about the original channel + private Chat forwardFromChat; ///< Optional. For messages forwarded from a channel, information about the original channel @JsonProperty(FORWARDDATE_FIELD) private Integer forwardDate; ///< Optional. For forwarded messages, date the original message was sent @JsonProperty(TEXT_FIELD) @@ -130,7 +130,7 @@ public class Message implements IBotApiObject { } this.chat = new Chat(jsonObject.getJSONObject(CHAT_FIELD)); if (jsonObject.has(FORWARDFROMCHAT_FIELD)) { - this.forwardedFromChat = new Chat(jsonObject.getJSONObject(FORWARDFROMCHAT_FIELD)); + this.forwardFromChat = new Chat(jsonObject.getJSONObject(FORWARDFROMCHAT_FIELD)); } if (jsonObject.has(FORWARDFROM_FIELD)) { this.forwardFrom = new User(jsonObject.getJSONObject(FORWARDFROM_FIELD)); @@ -373,8 +373,8 @@ public class Message implements IBotApiObject { return location != null; } - public Chat getForwardedFromChat() { - return forwardedFromChat; + public Chat getForwardFromChat() { + return forwardFromChat; } @Override @@ -388,8 +388,8 @@ public class Message implements IBotApiObject { gen.writeNumberField(DATE_FIELD, date); } gen.writeObjectField(CHAT_FIELD, chat); - if (forwardedFromChat != null) { - gen.writeObjectField(FORWARDFROMCHAT_FIELD, forwardedFromChat); + if (forwardFromChat != null) { + gen.writeObjectField(FORWARDFROMCHAT_FIELD, forwardFromChat); } if (forwardFrom != null) { gen.writeObjectField(FORWARDFROM_FIELD, forwardFrom); @@ -485,7 +485,7 @@ public class Message implements IBotApiObject { ", date=" + date + ", chat=" + chat + ", forwardFrom=" + forwardFrom + - ", forwardedFromChat=" + forwardedFromChat + + ", forwardFromChat=" + forwardFromChat + ", forwardDate=" + forwardDate + ", text='" + text + '\'' + ", audio=" + audio + diff --git a/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java b/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java index f770700c..519fdf22 100644 --- a/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java +++ b/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java @@ -1,8 +1,8 @@ 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.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.entity.BufferedHttpEntity; @@ -34,7 +34,7 @@ import java.util.concurrent.TimeUnit; */ public class BotSession { private static final String LOGTAG = "BOTSESSION"; - private static final int SOCKET_TIMEOUT = 30 * 1000; + private static final int SOCKET_TIMEOUT = 10 * 1000; private final ITelegramLongPollingBot callback; private final ReaderThread readerThread; @@ -55,6 +55,10 @@ public class BotSession { this.handlerThread = new HandlerThread(); handlerThread.setName(callback.getBotUsername() + " Executor"); this.handlerThread.start(); + httpclient = HttpClientBuilder.create() + .setSSLHostnameVerifier(new NoopHostnameVerifier()) + .setConnectionTimeToLive(120, TimeUnit.SECONDS) + .build(); } public void close() @@ -79,32 +83,28 @@ public class BotSession { public void run() { setPriority(Thread.MIN_PRIORITY); while(running) { - GetUpdates request = new GetUpdates(); - request.setLimit(100); - request.setTimeout(20); - request.setOffset(lastReceivedUpdate + 1); - 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(SOCKET_TIMEOUT) - .setConnectTimeout(SOCKET_TIMEOUT) - .setConnectionRequestTimeout(SOCKET_TIMEOUT).build(); - //http client - HttpPost httpPost = new HttpPost(url); try { + GetUpdates request = new GetUpdates(); + request.setLimit(100); + request.setTimeout(50); + request.setOffset(lastReceivedUpdate + 1); + String url = Constants.BASEURL + token + "/" + GetUpdates.PATH; + //config + RequestConfig defaultRequestConfig = RequestConfig.custom().build(); + RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig) + .setSocketTimeout(SOCKET_TIMEOUT) + .setConnectTimeout(SOCKET_TIMEOUT) + .setConnectionRequestTimeout(SOCKET_TIMEOUT).build(); + //http client + HttpPost httpPost = new HttpPost(url); httpPost.addHeader("charset", StandardCharsets.UTF_8.name()); httpPost.setConfig(requestConfig); httpPost.setEntity(new StringEntity(request.toJson().toString(), ContentType.APPLICATION_JSON)); - HttpResponse response; - response = httpclient.execute(httpPost); - HttpEntity ht = response.getEntity(); - BufferedHttpEntity buf = new BufferedHttpEntity(ht); - String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8); - - try { + try (CloseableHttpResponse response = httpclient.execute(httpPost)) { + HttpEntity ht = response.getEntity(); + BufferedHttpEntity buf = new BufferedHttpEntity(ht); + String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8); JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { throw new InvalidObjectException(jsonObject.toString());