ForwardingService: improve the implementation of close()

* Don't try to remove listener (access .wallet()) unless kit.isRunning()
* Don't wait for termination, just call kit.stopAsync()
* Add some JavaDoc
This commit is contained in:
Sean Gilligan 2022-07-18 19:34:30 -07:00 committed by Andreas Schildbach
parent 498d638915
commit fb6add760b

View file

@ -133,11 +133,20 @@ public class ForwardingService implements AutoCloseable {
kit.wallet().addCoinsReceivedEventListener(listener);
}
/**
* Close the service. {@link AutoCloseable} will be triggered if an unhandled exception occurs within
* a <i>try-with-resources</i> block.
* <p>
* Note that {@link WalletAppKit#setAutoStop(boolean)} is set by default and installs a shutdown handler
* via {@link Runtime#addShutdownHook(Thread)} so we do not need to worry about explicitly shutting down
* the {@code WalletAppKit} if the process is terminated.
*/
@Override
public void close() {
kit.wallet().removeCoinsReceivedEventListener(listener);
if (kit.isRunning()) {
kit.wallet().removeCoinsReceivedEventListener(listener);
}
kit.stopAsync();
kit.awaitTerminated();
}
/**