From 5741e4ba97e3c5a8d222101c992629acf6b1ce5b Mon Sep 17 00:00:00 2001 From: vsubhuman Date: Wed, 2 Aug 2017 23:58:58 +0300 Subject: [PATCH] #269 Fixed update queue polling order and added `TelegramBatchLongPollingBot` --- .../bots/TelegramBatchLongPollingBot.java | 16 +++++++ .../updatesreceivers/DefaultBotSession.java | 42 +++++++++++++++---- 2 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramBatchLongPollingBot.java diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramBatchLongPollingBot.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramBatchLongPollingBot.java new file mode 100644 index 00000000..4b294868 --- /dev/null +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramBatchLongPollingBot.java @@ -0,0 +1,16 @@ +package org.telegram.telegrambots.bots; + +import org.telegram.telegrambots.api.objects.Update; + +import java.util.Collections; +import java.util.List; + +public abstract class TelegramBatchLongPollingBot extends TelegramLongPollingBot { + + @Override + public final void onUpdateReceived(Update update) { + onUpdatesReceived(Collections.singletonList(update)); + } + + public abstract void onUpdatesReceived(List updates); +} diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java index 1347e03a..c12c0eb1 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultBotSession.java @@ -18,6 +18,7 @@ import org.telegram.telegrambots.ApiConstants; import org.telegram.telegrambots.api.methods.updates.GetUpdates; import org.telegram.telegrambots.api.objects.Update; import org.telegram.telegrambots.bots.DefaultBotOptions; +import org.telegram.telegrambots.bots.TelegramBatchLongPollingBot; import org.telegram.telegrambots.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.generics.*; import org.telegram.telegrambots.logging.BotLogger; @@ -27,6 +28,8 @@ import java.io.InvalidObjectException; import java.net.SocketTimeoutException; import java.nio.charset.StandardCharsets; import java.security.InvalidParameterException; +import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.TimeUnit; @@ -252,23 +255,46 @@ public class DefaultBotSession implements BotSession { } } + private List getUpdateList() { + List updates = new ArrayList<>(); + for (Iterator it = receivedUpdates.iterator(); it.hasNext();) { + updates.add(it.next()); + it.remove(); + } + return updates; + } + private class HandlerThread extends Thread implements UpdatesHandler { @Override public void run() { setPriority(Thread.MIN_PRIORITY); while (running) { try { - Update update = receivedUpdates.pollLast(); - if (update == null) { - synchronized (receivedUpdates) { - receivedUpdates.wait(); - update = receivedUpdates.pollLast(); - if (update == null) { - continue; + if (callback instanceof TelegramBatchLongPollingBot) { + List updates = getUpdateList(); + if (updates.isEmpty()) { + synchronized (receivedUpdates) { + receivedUpdates.wait(); + updates = getUpdateList(); + if (updates.isEmpty()) { + continue; + } } } + ((TelegramBatchLongPollingBot) callback).onUpdatesReceived(updates); + } else { + Update update = receivedUpdates.pollFirst(); + if (update == null) { + synchronized (receivedUpdates) { + receivedUpdates.wait(); + update = receivedUpdates.pollFirst(); + if (update == null) { + continue; + } + } + } + callback.onUpdateReceived(update); } - callback.onUpdateReceived(update); } catch (InterruptedException e) { BotLogger.debug(LOGTAG, e); } catch (Exception e) {