Add shutdown hook

This commit is contained in:
Andrea Cavalli 2021-09-28 00:19:34 +02:00
parent 88ca5ce8ac
commit ff712809b5

View File

@ -174,6 +174,18 @@ public final class SimpleTelegramClient implements Authenticable {
this.authenticationData = authenticationData;
createDirectories();
client.initialize(this::handleUpdate, this::handleUpdateException, this::handleDefaultException);
// Handle unexpected shutdown
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
// Send close function
this.client.send(new TdApi.Close(), ok -> {}, ex -> {});
// Wait until the client has been closed successfully
this.waitForExit();
} catch (Throwable ignored) {
// Ignore errors since we are shutting down everything
}
}));
}
private void createDirectories() {