From ce9d198d5cdf7c49e8bb001150cd610c44423b08 Mon Sep 17 00:00:00 2001 From: Rubenlagus Date: Fri, 27 May 2016 02:04:21 +0200 Subject: [PATCH] Merges #82 --- .../org/telegram/telegrambots/Constants.java | 1 + .../updatesreceivers/BotSession.java | 57 ++++++++++--------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/telegram/telegrambots/Constants.java b/src/main/java/org/telegram/telegrambots/Constants.java index 60371d0f..ac241cc3 100644 --- a/src/main/java/org/telegram/telegrambots/Constants.java +++ b/src/main/java/org/telegram/telegrambots/Constants.java @@ -8,6 +8,7 @@ package org.telegram.telegrambots; */ public class Constants { public static final String BASEURL = "https://api.telegram.org/bot"; + public static final int GETUPDATESTIMEOUT = 50; public static final String RESPONSEFIELDOK = "ok"; public static final String RESPONSEFIELDRESULT = "result"; public static final String ERRORDESCRIPTIONFIELD = "description"; diff --git a/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java b/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java index 39b5f1ec..554c69e6 100644 --- a/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java +++ b/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java @@ -14,12 +14,12 @@ import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import org.telegram.telegrambots.logging.BotLogger; import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.TelegramApiException; import org.telegram.telegrambots.api.methods.updates.GetUpdates; import org.telegram.telegrambots.api.objects.Update; import org.telegram.telegrambots.bots.ITelegramLongPollingBot; +import org.telegram.telegrambots.logging.BotLogger; import java.io.IOException; import java.io.InvalidObjectException; @@ -27,9 +27,6 @@ import java.nio.charset.StandardCharsets; import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.TimeUnit; -import static org.telegram.telegrambots.Constants.ERRORCODEFIELD; -import static org.telegram.telegrambots.Constants.ERRORDESCRIPTIONFIELD; - /** * @author Ruben Bermudez * @version 1.0 @@ -54,49 +51,55 @@ public class BotSession { public BotSession(String token, ITelegramLongPollingBot callback) { this.token = token; this.callback = callback; + httpclient = HttpClientBuilder.create() .setSSLHostnameVerifier(new NoopHostnameVerifier()) .setConnectionTimeToLive(70, TimeUnit.SECONDS) .setMaxConnTotal(100) .build(); + requestConfig = RequestConfig.copy(RequestConfig.custom().build()) .setSocketTimeout(SOCKET_TIMEOUT) .setConnectTimeout(SOCKET_TIMEOUT) .setConnectionRequestTimeout(SOCKET_TIMEOUT).build(); - this.readerThread = new ReaderThread(); + + readerThread = new ReaderThread(); readerThread.setName(callback.getBotUsername() + " Telegram Connection"); - this.readerThread.start(); - this.handlerThread = new HandlerThread(); - handlerThread.setName(callback.getBotUsername() + " Executor"); - this.handlerThread.start(); + readerThread.start(); + + handlerThread = new HandlerThread(); + handlerThread.setName(callback.getBotUsername() + " Telegram Executor"); + handlerThread.start(); } - - public void close() - { - running = false; - if(httpclient != null) - { - try - { - httpclient.close(); - httpclient = null; + + public void close() { + running = false; + if (readerThread != null) { + readerThread.interrupt(); + } + if (handlerThread != null) { + handlerThread.interrupt(); + } + if (httpclient != null) { + try { + httpclient.close(); + httpclient = null; } catch (IOException e) { BotLogger.severe(LOGTAG, e); } - } - + } } private class ReaderThread extends Thread { - @Override + @Override public void run() { setPriority(Thread.MIN_PRIORITY); - while(running) { + while (running) { try { GetUpdates request = new GetUpdates(); request.setLimit(100); - request.setTimeout(50); + request.setTimeout(Constants.GETUPDATESTIMEOUT); request.setOffset(lastReceivedUpdate + 1); String url = Constants.BASEURL + token + "/" + GetUpdates.PATH; //http client @@ -111,7 +114,9 @@ public class BotSession { String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8); JSONObject jsonObject = new JSONObject(responseContent); if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { - throw new TelegramApiException("Error getting updates", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD)); + throw new TelegramApiException("Error getting updates", + jsonObject.getString(Constants.ERRORDESCRIPTIONFIELD), + jsonObject.getInt(Constants.ERRORCODEFIELD)); } JSONArray jsonArray = jsonObject.getJSONArray(Constants.RESPONSEFIELDRESULT); if (jsonArray.length() != 0) { @@ -155,7 +160,7 @@ public class BotSession { @Override public void run() { setPriority(Thread.MIN_PRIORITY); - while(running) { + while (running) { try { Update update = receivedUpdates.pollLast(); if (update == null) {