private void blockUntilAddressChanged(Runnable eventThatCausesChange, Address address, int depth) throws InterruptedException { CountDownLatch latchMined = new CountDownLatch(1); TransactionConfidenceEventListener listener2 = (wallet, tx) -> { tx.getOutputs().stream() .filter(out -> { return (Objects.equals(address, out.getAddressFromP2PKHScript(params)) || Objects.equals(address, out.getAddressFromP2SH(params))) && out.getParentTransactionDepthInBlocks() >= depth; }) .findAny() .ifPresent(transactionOutput -> latchMined.countDown()); }; walletService.getWallet().addTransactionConfidenceEventListener(listener2); eventThatCausesChange.run(); latchMined.await(); Thread.sleep(100); // Allow for handlers walletService.getWallet().removeTransactionConfidenceEventListener(listener2); }
/** * Add a listener that gets executed when the confidence of a transaction changes. * @param listener */ public void addConfidenceChangedListener(TransactionConfidenceEventListener listener) { // Use a custom thread pool to speed up the processing of transactions. // Queue is blocking and limited to 10'000 // to avoid memory exhaustion. After threshold is reached, the // CallerRunsPolicy() forces blocking behavior. ContextPropagatingThreadFactory factory = new ContextPropagatingThreadFactory("listenerFactory"); Executor listenerExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), Runtime .getRuntime().availableProcessors(), 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10000), factory, new ThreadPoolExecutor.CallerRunsPolicy()); wallet.addTransactionConfidenceEventListener(listenerExecutor, listener); }