0

I'm trying to write simple program that will be web client for bitcoin payment. I use bitcoinj as a library; But getting errors.

2017-04-12 15:07:35.099 ERROR 14507 --- [inj user thread] 
org.bitcoinj.core.Context                : Performing thread fixup: 
you are accessing bitcoinj via a thread that has not had any context 
set on it.
2017-04-12 15:07:35.099 ERROR 14507 --- [inj user thread]     
org.bitcoinj.core.Context                : This error has been 
corrected for, but doing this makes your app less robust.
2017-04-12 15:07:35.099 ERROR 14507 --- [inj user thread] 
org.bitcoinj.core.Context                : You should use 
Context.propagate() or a ContextPropagatingThreadFactory.
2017-04-12 15:07:35.099 ERROR 14507 --- [inj user thread] 
org.bitcoinj.core.Context                : Please refer to the user 
guide for more information about this.
2017-04-12 15:07:35.099 ERROR 14507 --- [inj user thread] 
org.bitcoinj.core.Context                : Thread name is bitcoinj 
user thread.

I just want to know how to implement bitcoin payment to my webpage on Spring framework

@Component public class BitcoinjService {

private final NetworkParameters params;
private final WalletAppKit kit;

@PreDestroy
private void close(){
    System.out.println("shutting down again");
    kit.stopAsync();
    kit.awaitTerminated();
}

public BitcoinjService() {
    params = TestNet3Params.get();
    kit = new WalletAppKit(params, new File("."), "walletappkit-example");
    kit.startAsync();
    kit.awaitRunning();

    kit.wallet().addCoinsReceivedEventListener((wallet, tx, prevBalance, newBalance) -> {
        System.out.println("-----> coins resceived: " + tx.getHashAsString());
        System.out.println("received: " + tx.getValue(wallet));
    });

    kit.wallet().addCoinsSentEventListener((wallet, tx, prevBalance, newBalance) -> System.out.println("coins sent"));

    kit.wallet().addKeyChainEventListener(keys -> System.out.println("new key added"));

    kit.wallet().addScriptsChangeEventListener((wallet, scripts, isAddingScripts) -> System.out.println("new script added"));

    kit.wallet().addTransactionConfidenceEventListener((wallet, tx) -> {
        System.out.println("-----> confidence changed: " + tx.getHashAsString());
        TransactionConfidence confidence = tx.getConfidence();
        System.out.println("new block depth: " + confidence.getDepthInBlocks());
    });

    // Ready to run. The kit syncs the blockchain and our wallet event listener gets notified when something happens.
    // To test everything we create and print a fresh receiving address. Send some coins to that address and see if everything works.
    System.out.println("send money to: " + kit.wallet().freshReceiveAddress().toString());

}


public String  getFreshReciveAddress() {
    Context.propagate(new Context(params));
    return kit.wallet().currentReceiveAddress().toString();
}

public String getBalance(){
    Context.propagate(new Context(params));
    return kit.wallet().getBalance().toFriendlyString();
}

public void sendTo(String address) {
    Context.propagate(new Context(params));
    // Get the address 1RbxbA1yP2Lebauuef3cBiBho853f7jxs in object form.
    Address targetAddress = Address.fromBase58(params, address);
    // Do the send of 1 BTC in the background. This could throw InsufficientMoneyException.
    Wallet.SendResult result = null;
    try {
        result = kit.wallet().sendCoins(kit.peerGroup(), targetAddress, Coin.parseCoin(kit.wallet().getBalance().toPlainString()).minus(Coin.parseCoin("0.0005")));//TODO: calulating fee
        kit.wallet().saveToFile(new File("TestFile"));
        result.broadcastComplete.get();
    } catch (InsufficientMoneyException | IOException | InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }

}
  • The error message tells you ``You should use Context.propagate() or a ContextPropagatingThreadFactory. [...] Please refer to the user guide for more information about this.`` - did you already read the guide? – f1sh Apr 12 '17 at 12:14
  • as you can see "Context.propagate(new Context(params));" i tried add this to every method. – Vadym Ozarynskyi Apr 12 '17 at 12:20

0 Answers0