I built a java web application which connects using Web3j with my smart contract deployed on Ropsten Network.

My web application provides a control panel to allow the user to send transactions to the smart contract. Sometimes the user can send more than 5 calls at the same time. (actually much more than 5 :) )

I have a java class wrapper of my smart contract to call the functions of it.

The question is: What I have to use, send() or sendAsync? How to manage() I tried to use send() and sendAync().get() and I got the same result.

TransactionReceipt receipt = myContract.callFunction1(...)         

What I get with this code is a long wait to complete the call and get the transaction receipt.... Is there another way to do this?

I know this could involve promises but I am not familiar with them. Could someone provide some code?

Thanks in advance

2 Answers 2


You can send and confirm transaction without waiting using CompletableFuture and callback function. rc-> Instance that calls contract function through remote call.

CompletableFuture<TransactionReceipt> transactionReceiptCompletableFuture = rc.sendAsync();

transactionReceiptCompletableFuture.thenAccept(transactionReceipt -> {

   // then accept gets transaction receipt only if the transaction is successful

 }).exceptionally(transactionReceipt  -> {
    return null;
  • 1
    While implementing this and start debugging, it always jumps over the thenAccept and my IDE is telling me that CompletableFuture is not completed which ends in an Exception. Maybe I misunderstood the concept. Can someone give some further explanation with maybe some deeper/more code snippets?
    – Tobias
    Commented Apr 29, 2019 at 7:25

If the long wait is the problem, then you can create a thread. Then the request will get sent and you can go can continue execution without stalling out your program. See this example:

Thread thread = new Thread(new Runnable() {
      public void run() {
        final Web3j client = Web3j.build(
            new HttpService(RPC_NODE_URL_WITH_API_KEY)
        Credentials metamaskCredentials = Credentials.create(WALLET_PRIVATE_KEY);
        ContractGasProvider contractGasProvider = new DefaultGasProvider();
        YourContractWrapper fpl = YourContractWrapper.load(CONTRACT_ADDRESS, client, metamaskCredentials, contractGasProvider);
        try {
          fpl.update(label, fingerprint).sendAsync().get(200, TimeUnit.SECONDS);
        } catch(TimeoutException e) {
          System.out.println("Timed out");
        } catch (Exception e) {
          throw new RuntimeException(e);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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