1

I am using web3j to listen to transactions and blockchains through geth. It seems that none of the subscriptions that I have created to the blockchain or transaction are detecting changes.

I start geth like like the following.

./geth \
 --fast --cache=512 \
 --rpc --rpcapi personal,db,eth,net,web3 \
 --ws --wsapi personal,db,eth,net,web3 \
 --metrics --verbosity 5 --fakepow

My Java code looks like the following.

public class App {
  public static void main(String[] args) throws ExecutionException, InterruptedException {
    Web3j web3 = Web3j.build(new HttpService(), 100, Async.defaultExecutorService());  // defaults to http://localhost:8545/
    Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().sendAsync().get();
    String clientVersion = web3ClientVersion.getWeb3ClientVersion();
    System.out.println(clientVersion);
    System.out.println(web3ClientVersion.getJsonrpc());

    web3.blockObservable(false).subscribe(block -> {
      System.out.println(block.getBlock().getNumber());
    }, Throwable::printStackTrace, () -> System.out.println("block done"));

    web3.transactionObservable().subscribe(tx -> {
      System.out.println(tx.getHash() + ", " + tx.getGasPriceRaw());
    }, Throwable::printStackTrace, () -> System.out.println("tx done"));

    web3.pendingTransactionObservable().subscribe(tx -> {
      System.out.println(tx.getHash() + ", " + tx.getGasPriceRaw());
    }, Throwable::printStackTrace, () -> System.out.println("ptx done"));

    System.out.println("umm reached the end");
  }
}

All I see on the console of my Java app is the following.

Geth/v1.6.1-stable-021c3c28/linux-amd64/go1.8.1
2.0
umm reached the end

Any ideas what I am doing wrong?

1

It seems that you forgot to add sleep statement. Remember, that subscription takes place asynchronously in a different thread of execution to the rest of our program. Also don't forget to make an unsubscribe method call. Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.