Add shutdown hook
This commit is contained in:
parent
88ca5ce8ac
commit
ff712809b5
@ -174,6 +174,18 @@ public final class SimpleTelegramClient implements Authenticable {
|
|||||||
this.authenticationData = authenticationData;
|
this.authenticationData = authenticationData;
|
||||||
createDirectories();
|
createDirectories();
|
||||||
client.initialize(this::handleUpdate, this::handleUpdateException, this::handleDefaultException);
|
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() {
|
private void createDirectories() {
|
||||||
|
Loading…
Reference in New Issue
Block a user