Merge branch 'myplacedk-SocketException' into dev
This commit is contained in:
commit
09c2b36559
@ -69,7 +69,7 @@ public class DefaultBotSession implements BotSession {
|
|||||||
|
|
||||||
lastReceivedUpdate = 0;
|
lastReceivedUpdate = 0;
|
||||||
|
|
||||||
readerThread = new ReaderThread(updatesSupplier);
|
readerThread = new ReaderThread(updatesSupplier, this);
|
||||||
readerThread.setName(callback.getBotUsername() + " Telegram Connection");
|
readerThread.setName(callback.getBotUsername() + " Telegram Connection");
|
||||||
readerThread.start();
|
readerThread.start();
|
||||||
|
|
||||||
@ -135,12 +135,14 @@ public class DefaultBotSession implements BotSession {
|
|||||||
private class ReaderThread extends Thread implements UpdatesReader {
|
private class ReaderThread extends Thread implements UpdatesReader {
|
||||||
|
|
||||||
private final UpdatesSupplier updatesSupplier;
|
private final UpdatesSupplier updatesSupplier;
|
||||||
|
private final Object lock;
|
||||||
private CloseableHttpClient httpclient;
|
private CloseableHttpClient httpclient;
|
||||||
private ExponentialBackOff exponentialBackOff;
|
private ExponentialBackOff exponentialBackOff;
|
||||||
private RequestConfig requestConfig;
|
private RequestConfig requestConfig;
|
||||||
|
|
||||||
public ReaderThread(UpdatesSupplier updatesSupplier) {
|
public ReaderThread(UpdatesSupplier updatesSupplier, Object lock) {
|
||||||
this.updatesSupplier = Optional.ofNullable(updatesSupplier).orElse(this::getUpdatesFromServer);
|
this.updatesSupplier = Optional.ofNullable(updatesSupplier).orElse(this::getUpdatesFromServer);
|
||||||
|
this.lock = lock;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -183,12 +185,12 @@ public class DefaultBotSession implements BotSession {
|
|||||||
public void run() {
|
public void run() {
|
||||||
setPriority(Thread.MIN_PRIORITY);
|
setPriority(Thread.MIN_PRIORITY);
|
||||||
while (running) {
|
while (running) {
|
||||||
|
synchronized (lock) {
|
||||||
|
if (running) {
|
||||||
try {
|
try {
|
||||||
List<Update> updates = updatesSupplier.getUpdates();
|
List<Update> updates = updatesSupplier.getUpdates();
|
||||||
if (updates.isEmpty()) {
|
if (updates.isEmpty()) {
|
||||||
synchronized (this) {
|
lock.wait(500);
|
||||||
this.wait(500);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
updates.removeIf(x -> x.getUpdateId() < lastReceivedUpdate);
|
updates.removeIf(x -> x.getUpdateId() < lastReceivedUpdate);
|
||||||
lastReceivedUpdate = updates.parallelStream()
|
lastReceivedUpdate = updates.parallelStream()
|
||||||
@ -207,24 +209,28 @@ 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 {
|
||||||
synchronized (this) {
|
synchronized (lock) {
|
||||||
this.wait(exponentialBackOff.nextBackOffMillis());
|
lock.wait(exponentialBackOff.nextBackOffMillis());
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
if (!running) {
|
if (!running) {
|
||||||
receivedUpdates.clear();
|
receivedUpdates.clear();
|
||||||
}
|
}
|
||||||
BotLogger.debug(LOGTAG, e);
|
BotLogger.debug(LOGTAG, e);
|
||||||
|
interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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)
|
||||||
@ -248,8 +254,8 @@ public class DefaultBotSession implements BotSession {
|
|||||||
|
|
||||||
if (response.getStatusLine().getStatusCode() >= 500) {
|
if (response.getStatusLine().getStatusCode() >= 500) {
|
||||||
BotLogger.warn(LOGTAG, responseContent);
|
BotLogger.warn(LOGTAG, responseContent);
|
||||||
synchronized (this) {
|
synchronized (lock) {
|
||||||
this.wait(500);
|
lock.wait(500);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
@ -260,14 +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) {
|
||||||
if (!e.getMessage().equals("Socket Closed")) {
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
@ -275,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() {
|
||||||
@ -306,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);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user