ignore error sending message after shut down

This commit is contained in:
woodser 2023-12-29 10:28:45 -05:00
parent 342c212ba8
commit e2e2963b99
2 changed files with 14 additions and 15 deletions

View file

@ -58,7 +58,7 @@ public class UserThread {
} }
public static void execute(Runnable command) { public static void execute(Runnable command) {
UserThread.executor.execute(() -> { executor.execute(() -> {
Thread.currentThread().setName(USER_THREAD_NAME); Thread.currentThread().setName(USER_THREAD_NAME);
command.run(); command.run();
}); });
@ -79,7 +79,8 @@ public class UserThread {
} }
} }
public static boolean isUserThread(Thread thread) { // TODO: better way to determine if on UserThread, since this is not reliable
private static boolean isUserThread(Thread thread) {
return USER_THREAD_NAME.equals(thread.getName()); return USER_THREAD_NAME.equals(thread.getName());
} }

View file

@ -306,27 +306,25 @@ public abstract class NetworkNode implements MessageListener {
} }
public void onFailure(@NotNull Throwable throwable) { public void onFailure(@NotNull Throwable throwable) {
UserThread.execute(() -> { UserThread.execute(() -> resolveWithException(resultFuture, throwable));
if (!resultFuture.setException(throwable)) {
// In case the setException returns false we need to cancel the future.
resultFuture.cancel(true);
}
});
} }
}, MoreExecutors.directExecutor()); }, MoreExecutors.directExecutor());
} catch (RejectedExecutionException exception) { } catch (RejectedExecutionException exception) {
log.error("RejectedExecutionException at sendMessage: ", exception); if (!executor.isShutdown()) {
UserThread.execute(() -> { log.error("RejectedExecutionException at sendMessage: ", exception);
if (!resultFuture.setException(exception)) { UserThread.execute(() -> resolveWithException(resultFuture, exception));
// In case the setException returns false we need to cancel the future. }
resultFuture.cancel(true);
}
});
} }
return resultFuture; return resultFuture;
} }
private void resolveWithException(SettableFuture<?> future, Throwable exception) {
if (!future.setException(exception)) {
future.cancel(true); // In case the setException returns false we need to cancel the future.
}
}
public ReadOnlyObjectProperty<NodeAddress> nodeAddressProperty() { public ReadOnlyObjectProperty<NodeAddress> nodeAddressProperty() {
return nodeAddressProperty; return nodeAddressProperty;
} }