#269 Fixed update queue polling order and added TelegramBatchLongPollingBot
This commit is contained in:
parent
ffcdbd126e
commit
5741e4ba97
@ -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);
|
||||
}
|
@ -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<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 {
|
||||
@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<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) {
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user