Clean up a bit in DefaultBotSession

This commit is contained in:
Niels Ulrik Andersen 2017-11-19 16:59:23 +01:00
parent 4cf29bd75f
commit e2aed55b11

View File

@ -190,9 +190,7 @@ public class DefaultBotSession implements BotSession {
try { try {
List<Update> updates = updatesSupplier.getUpdates(); List<Update> updates = updatesSupplier.getUpdates();
if (updates.isEmpty()) { if (updates.isEmpty()) {
if (running) { lock.wait(500);
lock.wait(500);
}
} else { } else {
updates.removeIf(x -> x.getUpdateId() < lastReceivedUpdate); updates.removeIf(x -> x.getUpdateId() < lastReceivedUpdate);
lastReceivedUpdate = updates.parallelStream() lastReceivedUpdate = updates.parallelStream()
@ -211,6 +209,7 @@ public class DefaultBotSession implements BotSession {
receivedUpdates.clear(); receivedUpdates.clear();
} }
BotLogger.debug(LOGTAG, e); BotLogger.debug(LOGTAG, e);
interrupt();
} catch (Exception global) { } catch (Exception global) {
BotLogger.severe(LOGTAG, global); BotLogger.severe(LOGTAG, global);
try { try {
@ -222,6 +221,7 @@ public class DefaultBotSession implements BotSession {
receivedUpdates.clear(); receivedUpdates.clear();
} }
BotLogger.debug(LOGTAG, e); BotLogger.debug(LOGTAG, e);
interrupt();
} }
} }
} }
@ -230,7 +230,7 @@ public class DefaultBotSession implements BotSession {
BotLogger.debug(LOGTAG, "Reader thread has being closed"); BotLogger.debug(LOGTAG, "Reader thread has being closed");
} }
private List<Update> getUpdatesFromServer() throws InterruptedException, Exception { private List<Update> getUpdatesFromServer() throws IOException {
GetUpdates request = new GetUpdates() GetUpdates request = new GetUpdates()
.setLimit(100) .setLimit(100)
.setTimeout(ApiConstants.GETUPDATES_TIMEOUT) .setTimeout(ApiConstants.GETUPDATES_TIMEOUT)
@ -266,12 +266,13 @@ public class DefaultBotSession implements BotSession {
BotLogger.severe(responseContent, LOGTAG, e); BotLogger.severe(responseContent, LOGTAG, e);
} }
} }
} catch (SocketException e) { } catch (SocketException | InvalidObjectException | TelegramApiRequestException e) {
BotLogger.severe(LOGTAG, e); BotLogger.severe(LOGTAG, e);
} catch (SocketTimeoutException e) { } catch (SocketTimeoutException e) {
BotLogger.fine(LOGTAG, e); BotLogger.fine(LOGTAG, e);
} catch (InvalidObjectException | TelegramApiRequestException e) { } catch (InterruptedException e) {
BotLogger.severe(LOGTAG, e); BotLogger.fine(LOGTAG, e);
interrupt();
} }
return Collections.emptyList(); return Collections.emptyList();
} }
@ -279,7 +280,7 @@ public class DefaultBotSession implements BotSession {
public interface UpdatesSupplier { public interface UpdatesSupplier {
List<Update> getUpdates() throws InterruptedException, Exception; List<Update> getUpdates() throws Exception;
} }
private List<Update> getUpdateList() { private List<Update> getUpdateList() {
@ -310,6 +311,7 @@ public class DefaultBotSession implements BotSession {
callback.onUpdatesReceived(updates); callback.onUpdatesReceived(updates);
} catch (InterruptedException e) { } catch (InterruptedException e) {
BotLogger.debug(LOGTAG, e); BotLogger.debug(LOGTAG, e);
interrupt();
} catch (Exception e) { } catch (Exception e) {
BotLogger.severe(LOGTAG, e); BotLogger.severe(LOGTAG, e);
} }