#269 Fixed update queue polling order and added TelegramBatchLongPollingBot

This commit is contained in:
vsubhuman 2017-08-02 23:58:58 +03:00
parent ffcdbd126e
commit 5741e4ba97
2 changed files with 50 additions and 8 deletions

View File

@ -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<Update> updates);
}

View File

@ -18,6 +18,7 @@ import org.telegram.telegrambots.ApiConstants;
import org.telegram.telegrambots.api.methods.updates.GetUpdates; import org.telegram.telegrambots.api.methods.updates.GetUpdates;
import org.telegram.telegrambots.api.objects.Update; import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramBatchLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.generics.*; import org.telegram.telegrambots.generics.*;
import org.telegram.telegrambots.logging.BotLogger; import org.telegram.telegrambots.logging.BotLogger;
@ -27,6 +28,8 @@ import java.io.InvalidObjectException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -252,23 +255,46 @@ public class DefaultBotSession implements BotSession {
} }
} }
private List<Update> getUpdateList() {
List<Update> updates = new ArrayList<>();
for (Iterator<Update> it = receivedUpdates.iterator(); it.hasNext();) {
updates.add(it.next());
it.remove();
}
return updates;
}
private class HandlerThread extends Thread implements UpdatesHandler { private class HandlerThread extends Thread implements UpdatesHandler {
@Override @Override
public void run() { public void run() {
setPriority(Thread.MIN_PRIORITY); setPriority(Thread.MIN_PRIORITY);
while (running) { while (running) {
try { try {
Update update = receivedUpdates.pollLast(); if (callback instanceof TelegramBatchLongPollingBot) {
List<Update> 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) { if (update == null) {
synchronized (receivedUpdates) { synchronized (receivedUpdates) {
receivedUpdates.wait(); receivedUpdates.wait();
update = receivedUpdates.pollLast(); update = receivedUpdates.pollFirst();
if (update == null) { if (update == null) {
continue; continue;
} }
} }
} }
callback.onUpdateReceived(update); callback.onUpdateReceived(update);
}
} catch (InterruptedException e) { } catch (InterruptedException e) {
BotLogger.debug(LOGTAG, e); BotLogger.debug(LOGTAG, e);
} catch (Exception e) { } catch (Exception e) {