Remove duplicate shutdown hooks
This commit is contained in:
parent
b27c736866
commit
39bbe60e50
@ -26,7 +26,7 @@
|
||||
<dependency>
|
||||
<groupId>it.tdlight</groupId>
|
||||
<artifactId>tdlight-java-bom</artifactId>
|
||||
<version>2.7.10.0</version>
|
||||
<version>2.7.10.1</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
@ -205,18 +205,6 @@ 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() {
|
||||
|
@ -60,15 +60,7 @@ public final class InternalClientManager implements AutoCloseable {
|
||||
}
|
||||
return val;
|
||||
});
|
||||
if (clientManager.startIfNeeded()) {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
try {
|
||||
clientManager.onJVMShutdown();
|
||||
} catch (InterruptedException ex) {
|
||||
logger.error("Failed to close", ex);
|
||||
}
|
||||
}));
|
||||
}
|
||||
clientManager.startIfNeeded();
|
||||
return clientManager;
|
||||
}
|
||||
|
||||
@ -159,10 +151,6 @@ public final class InternalClientManager implements AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
private void onJVMShutdown() throws InterruptedException {
|
||||
responseReceiver.onJVMShutdown();
|
||||
}
|
||||
|
||||
private static final class DroppedEvent {
|
||||
|
||||
private final long id;
|
||||
|
@ -21,7 +21,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
abstract class ResponseReceiver extends Thread implements AutoCloseable {
|
||||
|
||||
private static final String FLAG_PAUSE_SHUTDOWN_UNTIL_ALL_CLOSED = "it.tdlight.pauseShutdownUntilAllClosed";
|
||||
private static final String FLAG_USE_OPTIMIZED_DISPATCHER = "tdlight.dispatcher.use_optimized_dispatcher";
|
||||
private static final boolean USE_OPTIMIZED_DISPATCHER
|
||||
= Boolean.parseBoolean(System.getProperty(FLAG_USE_OPTIMIZED_DISPATCHER, "true"));
|
||||
@ -36,7 +35,6 @@ abstract class ResponseReceiver extends Thread implements AutoCloseable {
|
||||
|
||||
private final AtomicBoolean startCalled = new AtomicBoolean();
|
||||
private final AtomicBoolean closeCalled = new AtomicBoolean();
|
||||
private final AtomicBoolean jvmShutdown = new AtomicBoolean();
|
||||
|
||||
private final EventsHandler eventsHandler;
|
||||
private final int[] clientIds = new int[MAX_EVENTS];
|
||||
@ -56,7 +54,7 @@ abstract class ResponseReceiver extends Thread implements AutoCloseable {
|
||||
public ResponseReceiver(EventsHandler eventsHandler) {
|
||||
super("TDLib thread");
|
||||
this.eventsHandler = eventsHandler;
|
||||
this.setDaemon(true);
|
||||
this.setDaemon(false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -82,7 +80,6 @@ abstract class ResponseReceiver extends Thread implements AutoCloseable {
|
||||
boolean interrupted;
|
||||
while (
|
||||
!(interrupted = Thread.interrupted())
|
||||
&& !jvmShutdown.get()
|
||||
&& (!closeCalled.get() || !registeredClients.isEmpty())
|
||||
) {
|
||||
// Timeout is expressed in seconds
|
||||
@ -224,13 +221,11 @@ abstract class ResponseReceiver extends Thread implements AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
if (interrupted) {
|
||||
if (!jvmShutdown.get()) {
|
||||
if (interrupted || closeCalled.get()) {
|
||||
for (Integer clientId : this.registeredClients) {
|
||||
eventsHandler.handleClientEvents(clientId, true, clientEventIds, clientEvents, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
this.closeWait.countDown();
|
||||
}
|
||||
@ -287,14 +282,4 @@ abstract class ResponseReceiver extends Thread implements AutoCloseable {
|
||||
throw new IllegalStateException("Start not called");
|
||||
}
|
||||
}
|
||||
|
||||
public void onJVMShutdown() throws InterruptedException {
|
||||
if (startCalled.get()) {
|
||||
if (this.jvmShutdown.compareAndSet(false, true)) {
|
||||
if (Boolean.parseBoolean(System.getProperty(FLAG_PAUSE_SHUTDOWN_UNTIL_ALL_CLOSED, "true"))) {
|
||||
this.closeWait.await();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user