diff --git a/java-examples/src/main/java/bisq/bots/GrpcStubs.java b/java-examples/src/main/java/bisq/bots/GrpcStubs.java index c6ed9fb..2f6d101 100644 --- a/java-examples/src/main/java/bisq/bots/GrpcStubs.java +++ b/java-examples/src/main/java/bisq/bots/GrpcStubs.java @@ -19,6 +19,7 @@ package bisq.bots; import bisq.proto.grpc.*; import io.grpc.CallCredentials; +import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; import lombok.extern.slf4j.Slf4j; @@ -40,19 +41,13 @@ final class GrpcStubs { public final TradesGrpc.TradesBlockingStub tradesService; public final WalletsGrpc.WalletsBlockingStub walletsService; + private final ManagedChannel channel; + public GrpcStubs(String apiHost, int apiPort, String apiPassword) { CallCredentials credentials = new PasswordCallCredentials(apiPassword); - var channel = ManagedChannelBuilder.forAddress(apiHost, apiPort).usePlaintext().build(); - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - try { - log.info("Shutting down bot's grpc channel."); - channel.shutdown().awaitTermination(1, SECONDS); - log.info("Bot channel shutdown complete."); - } catch (InterruptedException ex) { - throw new IllegalStateException(ex); - } - })); + this.channel = ManagedChannelBuilder.forAddress(apiHost, apiPort).usePlaintext().build(); + Runtime.getRuntime().addShutdownHook(new Thread(this::close)); this.disputeAgentsService = DisputeAgentsGrpc.newBlockingStub(channel).withCallCredentials(credentials); this.helpService = HelpGrpc.newBlockingStub(channel).withCallCredentials(credentials); @@ -64,4 +59,16 @@ final class GrpcStubs { this.tradesService = TradesGrpc.newBlockingStub(channel).withCallCredentials(credentials); this.walletsService = WalletsGrpc.newBlockingStub(channel).withCallCredentials(credentials); } + + public void close() { + try { + if (!channel.isShutdown()) { + log.info("Shutting down bot's grpc channel."); + channel.shutdown().awaitTermination(1, SECONDS); + log.info("Bot channel shutdown complete."); + } + } catch (InterruptedException ex) { + throw new IllegalStateException(ex); + } + } } diff --git a/java-examples/src/main/java/bisq/bots/RegtestTradePaymentSimulator.java b/java-examples/src/main/java/bisq/bots/RegtestTradePaymentSimulator.java index 3bac466..7d2d8c8 100644 --- a/java-examples/src/main/java/bisq/bots/RegtestTradePaymentSimulator.java +++ b/java-examples/src/main/java/bisq/bots/RegtestTradePaymentSimulator.java @@ -116,6 +116,8 @@ public class RegtestTradePaymentSimulator extends AbstractBot { sleep(pollingInterval); log.info("Trade is completed, printing all closed trades and exiting {}.", this.getClass().getSimpleName()); printTradesSummary(CLOSED); + log.info("Closing {}'s gRPC channel.", this.getClass().getSimpleName()); + super.grpcStubs.close(); } private void waitForTakerDepositTxConfirmation() {