#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.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) {
|
||||||
if (update == null) {
|
List<Update> updates = getUpdateList();
|
||||||
synchronized (receivedUpdates) {
|
if (updates.isEmpty()) {
|
||||||
receivedUpdates.wait();
|
synchronized (receivedUpdates) {
|
||||||
update = receivedUpdates.pollLast();
|
receivedUpdates.wait();
|
||||||
if (update == null) {
|
updates = getUpdateList();
|
||||||
continue;
|
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) {
|
} catch (InterruptedException e) {
|
||||||
BotLogger.debug(LOGTAG, e);
|
BotLogger.debug(LOGTAG, e);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user