From 3df8631a6dc906010d89c23f11c629ba900d8ce5 Mon Sep 17 00:00:00 2001 From: Vinpasso Date: Sun, 17 Apr 2016 15:50:54 +0200 Subject: [PATCH] Closeable Bot Sessions Return a Bot Session Class when registering a Bot that can be closed as needed Give Threads a default Name to distinguish them in Debug Mode --- .../telegrambots/TelegramBotsApi.java | 8 ++--- .../{UpdatesThread.java => BotSession.java} | 35 +++++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) rename src/main/java/org/telegram/telegrambots/updatesreceivers/{UpdatesThread.java => BotSession.java} (87%) diff --git a/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java b/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java index 0993d3ed..19017ea1 100644 --- a/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java +++ b/src/main/java/org/telegram/telegrambots/TelegramBotsApi.java @@ -15,7 +15,7 @@ import org.json.JSONObject; import org.telegram.telegrambots.api.methods.updates.SetWebhook; import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.bots.TelegramWebhookBot; -import org.telegram.telegrambots.updatesreceivers.UpdatesThread; +import org.telegram.telegrambots.updatesreceivers.BotSession; import org.telegram.telegrambots.updatesreceivers.Webhook; import java.io.File; @@ -122,12 +122,12 @@ public class TelegramBotsApi { } /** - * + * Register a bot. The Bot Session is started immediately, and may be disconnected by calling close. * @param bot */ - public void registerBot(TelegramLongPollingBot bot) throws TelegramApiException { + public BotSession registerBot(TelegramLongPollingBot bot) throws TelegramApiException { setWebhook(bot.getBotToken()); - new UpdatesThread(bot.getBotToken(), bot); + return new BotSession(bot.getBotToken(), bot); } /** diff --git a/src/main/java/org/telegram/telegrambots/updatesreceivers/UpdatesThread.java b/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java similarity index 87% rename from src/main/java/org/telegram/telegrambots/updatesreceivers/UpdatesThread.java rename to src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java index 190b9e5c..f86453ef 100644 --- a/src/main/java/org/telegram/telegrambots/updatesreceivers/UpdatesThread.java +++ b/src/main/java/org/telegram/telegrambots/updatesreceivers/BotSession.java @@ -31,7 +31,7 @@ import java.util.concurrent.TimeUnit; * @brief Thread to request updates with active wait * @date 20 of June of 2015 */ -public class UpdatesThread { +public class BotSession { private static final int SOCKET_TIMEOUT = 30 * 1000; private final ITelegramLongPollingBot callback; @@ -40,26 +40,49 @@ public class UpdatesThread { private final ConcurrentLinkedDeque receivedUpdates = new ConcurrentLinkedDeque<>(); private final String token; private int lastReceivedUpdate = 0; + private volatile boolean running = true; + private volatile CloseableHttpClient httpclient; - public UpdatesThread(String token, ITelegramLongPollingBot callback) { + + public BotSession(String token, ITelegramLongPollingBot callback) { this.token = token; this.callback = callback; this.readerThread = new ReaderThread(); + readerThread.setName(callback.getBotUsername() + " Telegram Connection"); this.readerThread.start(); this.handlerThread = new HandlerThread(); + handlerThread.setName(callback.getBotUsername() + " Executor"); this.handlerThread.start(); } + + public void close() + { + running = false; + if(httpclient != null) + { + try + { + httpclient.close(); + httpclient = null; + } catch (IOException e) + { + //Ignore it + } + } + + } private class ReaderThread extends Thread { - @Override + + @Override public void run() { setPriority(Thread.MIN_PRIORITY); - while(true) { + while(running) { GetUpdates request = new GetUpdates(); request.setLimit(100); request.setTimeout(20); request.setOffset(lastReceivedUpdate + 1); - CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionTimeToLive(20, TimeUnit.SECONDS).build(); + httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionTimeToLive(20, TimeUnit.SECONDS).build(); String url = Constants.BASEURL + token + "/" + GetUpdates.PATH; //config RequestConfig defaultRequestConfig = RequestConfig.custom().build(); @@ -117,7 +140,7 @@ public class UpdatesThread { @Override public void run() { setPriority(Thread.MIN_PRIORITY); - while(true) { + while(running) { try { Update update = receivedUpdates.pollLast(); if (update == null) {