Example usage for com.google.common.util.concurrent FutureCallback FutureCallback

List of usage examples for com.google.common.util.concurrent FutureCallback FutureCallback

Introduction

In this page you can find the example usage for com.google.common.util.concurrent FutureCallback FutureCallback.

Prototype

FutureCallback

Source Link

Usage

From source file:com.amazonaws.kinesis.producer.SampleKPLProducer.java

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        System.err.println("Usage SampleKPLProducer <stream name> <region>");
        System.exit(1);/*from w w w  .  java  2s .co m*/
    }

    String streamName = args[0];
    String regionName = args[1];

    final KinesisProducer producer = getKinesisProducer(regionName);

    final AtomicLong sequenceNumber = new AtomicLong(0);
    final AtomicLong completed = new AtomicLong(0);

    final FutureCallback<UserRecordResult> callback = new FutureCallback<UserRecordResult>() {
        @Override
        public void onFailure(Throwable t) {
            if (t instanceof UserRecordFailedException) {
                Attempt last = Iterables.getLast(((UserRecordFailedException) t).getResult().getAttempts());
                System.err.println(String.format("Record failed to put - %s : %s", last.getErrorCode(),
                        last.getErrorMessage()));
            }
            System.err.println("Exception during put: " + t.getMessage());
            t.printStackTrace();
            System.exit(1);
        }

        @Override
        public void onSuccess(UserRecordResult result) {
            completed.getAndIncrement();
        }
    };

    final Runnable putOneRecord = new Runnable() {
        @Override
        public void run() {
            byte[] data = ProducerUtils.randomData(sequenceNumber.get(), ProducerConfig.RECORD_SIZE_BYTES);
            ListenableFuture<UserRecordResult> f = producer.addUserRecord(streamName,
                    ProducerUtils.randomPartitionKey(), ProducerUtils.randomExplicitHashKey(),
                    ByteBuffer.wrap(data));
            Futures.addCallback(f, callback);
        }
    };

    EXECUTOR.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            long put = sequenceNumber.get();
            long total = RECORDS_PER_SECOND * SECONDS_TO_RUN;
            double putPercent = 100.0 * put / total;
            long done = completed.get();
            double donePercent = 100.0 * done / total;
            System.out.println(String.format("Put %d of %d so far (%.2f %%), %d have completed (%.2f %%)", put,
                    total, putPercent, done, donePercent));
        }
    }, 1, 1, TimeUnit.SECONDS);

    System.out.println(String.format("Starting puts... will run for %d seconds at %d records per second",
            SECONDS_TO_RUN, RECORDS_PER_SECOND));

    executeAtTargetRate(EXECUTOR, putOneRecord, sequenceNumber, SECONDS_TO_RUN, RECORDS_PER_SECOND);

    EXECUTOR.awaitTermination(SECONDS_TO_RUN + 1, TimeUnit.SECONDS);

    System.out.println("Waiting for remaining puts to finish...");
    producer.flushSync();
    System.out.println("All records complete.");

    producer.destroy();
    System.out.println("Finished.");
}

From source file:com.google.vertcoin.examples.ForwardingService.java

public static void main(String[] args) throws Exception {
    // This line makes the log output more compact and easily read, especially when using the JDK log adapter.
    BriefLogFormatter.init();//  w w w .  jav  a2s  . co m
    if (args.length < 2) {
        System.err.println("Usage: address-to-send-back-to [regtest|testnet]");
        return;
    }

    // Figure out which network we should connect to. Each one gets its own set of files.
    NetworkParameters params;
    String filePrefix;
    if (args[1].equals("testnet")) {
        params = TestNet3Params.get();
        filePrefix = "forwarding-service-testnet";
    } else if (args[1].equals("regtest")) {
        params = RegTestParams.get();
        filePrefix = "forwarding-service-regtest";
    } else {
        params = MainNetParams.get();
        filePrefix = "forwarding-service";
    }
    // Parse the address given as the first parameter.
    forwardingAddress = new Address(params, args[0]);

    // Start up a basic app using a class that automates some boilerplate.
    kit = new WalletAppKit(params, new File("."), filePrefix);

    if (params == RegTestParams.get()) {
        // Regression test mode is designed for testing and development only, so there's no public network for it.
        // If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance.
        kit.connectToLocalHost();
    }

    // Download the block chain and wait until it's done.
    kit.startAndWait();

    // We want to know when we receive money.
    kit.wallet().addEventListener(new AbstractWalletEventListener() {
        @Override
        public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) {
            // Runs in the dedicated "user thread" (see bitcoinj docs for more info on this).
            //
            // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast).
            BigInteger value = tx.getValueSentToMe(w);
            System.out.println("Received tx for " + Utils.bitcoinValueToFriendlyString(value) + ": " + tx);
            System.out.println("Transaction will be forwarded after it confirms.");
            // Wait until it's made it into the block chain (may run immediately if it's already there).
            //
            // For this dummy app of course, we could just forward the unconfirmed transaction. If it were
            // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to
            // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common
            // case of waiting for a block.
            Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() {
                @Override
                public void onSuccess(Transaction result) {
                    // "result" here is the same as "tx" above, but we use it anyway for clarity.
                    forwardCoins(result);
                }

                @Override
                public void onFailure(Throwable t) {
                    // This kind of future can't fail, just rethrow in case something weird happens.
                    throw new RuntimeException(t);
                }
            });
        }
    });

    Address sendToAddress = kit.wallet().getKeys().get(0).toAddress(params);
    System.out.println("Send coins to: " + sendToAddress);
    System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit.");

    try {
        Thread.sleep(Long.MAX_VALUE);
    } catch (InterruptedException ignored) {
    }
}

From source file:com.google.zetacoin.examples.ForwardingService.java

public static void main(String[] args) throws Exception {
    // This line makes the log output more compact and easily read, especially when using the JDK log adapter.
    BriefLogFormatter.init();/*from   w w w.  j a va2  s .  c o m*/
    if (args.length < 2) {
        System.err.println("Usage: address-to-send-back-to [regtest|testnet]");
        return;
    }

    // Figure out which network we should connect to. Each one gets its own set of files.
    NetworkParameters params;
    String filePrefix;
    if (args[1].equals("testnet")) {
        params = TestNet3Params.get();
        filePrefix = "forwarding-service-testnet";
    } else if (args[1].equals("regtest")) {
        params = RegTestParams.get();
        filePrefix = "forwarding-service-regtest";
    } else {
        params = MainNetParams.get();
        filePrefix = "forwarding-service";
    }
    // Parse the address given as the first parameter.
    forwardingAddress = new Address(params, args[0]);

    // Start up a basic app using a class that automates some boilerplate.
    kit = new WalletAppKit(params, new File("."), filePrefix);

    if (params == RegTestParams.get()) {
        // Regression test mode is designed for testing and development only, so there's no public network for it.
        // If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance.
        kit.connectToLocalHost();
    }

    // Download the block chain and wait until it's done.
    kit.startAndWait();

    // We want to know when we receive money.
    kit.wallet().addEventListener(new AbstractWalletEventListener() {
        @Override
        public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) {
            // Runs in the dedicated "user thread" (see zetacoinj docs for more info on this).
            //
            // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast).
            BigInteger value = tx.getValueSentToMe(w);
            System.out.println("Received tx for " + Utils.bitcoinValueToFriendlyString(value) + ": " + tx);
            System.out.println("Transaction will be forwarded after it confirms.");
            // Wait until it's made it into the block chain (may run immediately if it's already there).
            //
            // For this dummy app of course, we could just forward the unconfirmed transaction. If it were
            // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to
            // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common
            // case of waiting for a block.
            Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() {
                @Override
                public void onSuccess(Transaction result) {
                    // "result" here is the same as "tx" above, but we use it anyway for clarity.
                    forwardCoins(result);
                }

                @Override
                public void onFailure(Throwable t) {
                    // This kind of future can't fail, just rethrow in case something weird happens.
                    throw new RuntimeException(t);
                }
            });
        }
    });

    Address sendToAddress = kit.wallet().getKeys().get(0).toAddress(params);
    System.out.println("Send coins to: " + sendToAddress);
    System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit.");

    try {
        Thread.sleep(Long.MAX_VALUE);
    } catch (InterruptedException ignored) {
    }
}

From source file:com.google.betacoin.examples.ForwardingService.java

public static void main(String[] args) throws Exception {
    // This line makes the log output more compact and easily read, especially when using the JDK log adapter.
    BriefLogFormatter.init();/* www .  j  a  v  a  2 s  .  c om*/
    if (args.length < 1) {
        System.err.println("Usage: address-to-send-back-to [regtest|testnet]");
        return;
    }

    // Figure out which network we should connect to. Each one gets its own set of files.
    NetworkParameters params;
    String filePrefix;
    //if (args[1].equals("testnet")) {
    //    params = TestNet3Params.get();
    //    filePrefix = "forwarding-service-testnet";
    //} else if (args[1].equals("regtest")) {
    //    params = RegTestParams.get();
    //    filePrefix = "forwarding-service-regtest";
    //} else {
    params = MainNetParams.get();
    filePrefix = "forwarding-service";
    //}
    // Parse the address given as the first parameter.
    forwardingAddress = new Address(params, args[0]);

    // Start up a basic app using a class that automates some boilerplate.
    kit = new WalletAppKit(params, new File("."), filePrefix);

    if (params == RegTestParams.get()) {
        // Regression test mode is designed for testing and development only, so there's no public network for it.
        // If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance.
        kit.connectToLocalHost();
    }

    // Download the block chain and wait until it's done.
    kit.startAndWait();

    // We want to know when we receive money.
    kit.wallet().addEventListener(new AbstractWalletEventListener() {
        @Override
        public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) {
            // Runs in the dedicated "user thread" (see betacoinj docs for more info on this).
            //
            // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast).
            BigInteger value = tx.getValueSentToMe(w);
            System.out.println("Received tx for " + Utils.bitcoinValueToFriendlyString(value) + ": " + tx);
            System.out.println("Transaction will be forwarded after it confirms.");
            // Wait until it's made it into the block chain (may run immediately if it's already there).
            //
            // For this dummy app of course, we could just forward the unconfirmed transaction. If it were
            // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to
            // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common
            // case of waiting for a block.
            Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() {
                @Override
                public void onSuccess(Transaction result) {
                    // "result" here is the same as "tx" above, but we use it anyway for clarity.
                    forwardCoins(result);
                }

                @Override
                public void onFailure(Throwable t) {
                    // This kind of future can't fail, just rethrow in case something weird happens.
                    throw new RuntimeException(t);
                }
            });
        }
    });

    Address sendToAddress = kit.wallet().getKeys().get(0).toAddress(params);
    System.out.println("Send coins to: " + sendToAddress);
    System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit.");

    try {
        Thread.sleep(Long.MAX_VALUE);
    } catch (InterruptedException ignored) {
    }
}

From source file:com.google.worldcoin.examples.ForwardingService.java

public static void main(String[] args) throws Exception {
    // This line makes the log output more compact and easily read, especially when using the JDK log adapter.
    BriefLogFormatter.init();//from   w ww.j a va2  s  .c o m
    if (args.length < 2) {
        System.err.println("Usage: address-to-send-back-to [regtest|testnet]");
        return;
    }

    // Figure out which network we should connect to. Each one gets its own set of files.
    NetworkParameters params;
    String filePrefix;
    if (args[1].equals("testnet")) {
        params = TestNet3Params.get();
        filePrefix = "forwarding-service-testnet";
    } else if (args[1].equals("regtest")) {
        params = RegTestParams.get();
        filePrefix = "forwarding-service-regtest";
    } else {
        params = MainNetParams.get();
        filePrefix = "forwarding-service";
    }
    // Parse the address given as the first parameter.
    forwardingAddress = new Address(params, args[0]);

    // Start up a basic app using a class that automates some boilerplate.
    kit = new WalletAppKit(params, new File("."), filePrefix);

    if (params == RegTestParams.get()) {
        // Regression test mode is designed for testing and development only, so there's no public network for it.
        // If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance.
        kit.connectToLocalHost();
    }

    // Download the block chain and wait until it's done.
    kit.startAndWait();

    // We want to know when we receive money.
    kit.wallet().addEventListener(new AbstractWalletEventListener() {
        @Override
        public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) {
            // Runs in the dedicated "user thread" (see worldcoinj docs for more info on this).
            //
            // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast).
            BigInteger value = tx.getValueSentToMe(w);
            System.out.println("Received tx for " + Utils.worldcoinValueToFriendlyString(value) + ": " + tx);
            System.out.println("Transaction will be forwarded after it confirms.");
            // Wait until it's made it into the block chain (may run immediately if it's already there).
            //
            // For this dummy app of course, we could just forward the unconfirmed transaction. If it were
            // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to
            // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common
            // case of waiting for a block.
            Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() {
                @Override
                public void onSuccess(Transaction result) {
                    // "result" here is the same as "tx" above, but we use it anyway for clarity.
                    forwardCoins(result);
                }

                @Override
                public void onFailure(Throwable t) {
                    // This kind of future can't fail, just rethrow in case something weird happens.
                    throw new RuntimeException(t);
                }
            });
        }
    });

    Address sendToAddress = kit.wallet().getKeys().get(0).toAddress(params);
    System.out.println("Send coins to: " + sendToAddress);
    System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit.");

    try {
        Thread.sleep(Long.MAX_VALUE);
    } catch (InterruptedException ignored) {
    }
}

From source file:org.neoscoinj.examples.ForwardingService.java

public static void main(String[] args) throws Exception {
    // This line makes the log output more compact and easily read, especially when using the JDK log adapter.
    BriefLogFormatter.init();//w  w w  .ja  v a  2s.  c  om
    if (args.length < 1) {
        System.err.println("Usage: address-to-send-back-to [regtest|testnet]");
        return;
    }

    // Figure out which network we should connect to. Each one gets its own set of files.
    NetworkParameters params;
    String filePrefix;
    if (args.length > 1 && args[1].equals("testnet")) {
        params = TestNet3Params.get();
        filePrefix = "forwarding-service-testnet";
    } else if (args.length > 1 && args[1].equals("regtest")) {
        params = RegTestParams.get();
        filePrefix = "forwarding-service-regtest";
    } else {
        params = MainNetParams.get();
        filePrefix = "forwarding-service";
    }
    // Parse the address given as the first parameter.
    forwardingAddress = new Address(params, args[0]);

    // Start up a basic app using a class that automates some boilerplate.
    kit = new WalletAppKit(params, new File("."), filePrefix);

    if (params == RegTestParams.get()) {
        // Regression test mode is designed for testing and development only, so there's no public network for it.
        // If you pick this mode, you're expected to be running a local "neoscoind -regtest" instance.
        kit.connectToLocalHost();
    }

    // Download the block chain and wait until it's done.
    kit.startAsync();
    kit.awaitRunning();

    // We want to know when we receive money.
    kit.wallet().addEventListener(new AbstractWalletEventListener() {
        @Override
        public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) {
            // Runs in the dedicated "user thread" (see neoscoinj docs for more info on this).
            //
            // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast).
            Coin value = tx.getValueSentToMe(w);
            System.out.println("Received tx for " + value.toFriendlyString() + ": " + tx);
            System.out.println("Transaction will be forwarded after it confirms.");
            // Wait until it's made it into the block chain (may run immediately if it's already there).
            //
            // For this dummy app of course, we could just forward the unconfirmed transaction. If it were
            // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to
            // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common
            // case of waiting for a block.
            Futures.addCallback(tx.getConfidence().getDepthFuture(1),
                    new FutureCallback<TransactionConfidence>() {
                        @Override
                        public void onSuccess(TransactionConfidence result) {
                            forwardCoins(tx);
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            // This kind of future can't fail, just rethrow in case something weird happens.
                            throw new RuntimeException(t);
                        }
                    });
        }
    });

    Address sendToAddress = kit.wallet().currentReceiveKey().toAddress(params);
    System.out.println("Send coins to: " + sendToAddress);
    System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit.");

    try {
        Thread.sleep(Long.MAX_VALUE);
    } catch (InterruptedException ignored) {
    }
}

From source file:com.tribesman.kobocoinj.examples.ForwardingService.java

public static void main(String[] args) throws Exception {
    // This line makes the log output more compact and easily read, especially when using the JDK log adapter.
    BriefLogFormatter.init();/*  w w w  .j  a va 2 s  .  c o m*/
    if (args.length < 1) {
        System.err.println("Usage: address-to-send-back-to [regtest|testnet]");
        return;
    }

    // Figure out which network we should connect to. Each one gets its own set of files.
    NetworkParameters params;
    String filePrefix;
    if (args.length > 1 && args[1].equals("testnet")) {
        params = TestNet3Params.get();
        filePrefix = "forwarding-service-testnet";
    } else if (args.length > 1 && args[1].equals("regtest")) {
        params = RegTestParams.get();
        filePrefix = "forwarding-service-regtest";
    } else {
        params = MainNetParams.get();
        filePrefix = "forwarding-service";
    }
    // Parse the address given as the first parameter.
    forwardingAddress = new Address(params, args[0]);

    // Start up a basic app using a class that automates some boilerplate.
    kit = new WalletAppKit(params, new File("."), filePrefix);

    if (params == RegTestParams.get()) {
        // Regression test mode is designed for testing and development only, so there's no public network for it.
        // If you pick this mode, you're expected to be running a local "kobocoind -regtest" instance.
        kit.connectToLocalHost();
    }

    // Download the block chain and wait until it's done.
    kit.startAndWait();

    // We want to know when we receive money.
    kit.wallet().addEventListener(new AbstractWalletEventListener() {
        @Override
        public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) {
            // Runs in the dedicated "user thread" (see kobocoinj docs for more info on this).
            //
            // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast).
            BigInteger value = tx.getValueSentToMe(w);
            System.out.println("Received tx for " + Utils.kobocoinValueToFriendlyString(value) + ": " + tx);
            System.out.println("Transaction will be forwarded after it confirms.");
            // Wait until it's made it into the block chain (may run immediately if it's already there).
            //
            // For this dummy app of course, we could just forward the unconfirmed transaction. If it were
            // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to
            // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common
            // case of waiting for a block.
            Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() {
                @Override
                public void onSuccess(Transaction result) {
                    // "result" here is the same as "tx" above, but we use it anyway for clarity.
                    forwardCoins(result);
                }

                @Override
                public void onFailure(Throwable t) {
                    // This kind of future can't fail, just rethrow in case something weird happens.
                    throw new RuntimeException(t);
                }
            });
        }
    });

    Address sendToAddress = kit.wallet().getKeys().get(0).toAddress(params);
    System.out.println("Send coins to: " + sendToAddress);
    System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit.");

    try {
        Thread.sleep(Long.MAX_VALUE);
    } catch (InterruptedException ignored) {
    }
}

From source file:com.google.sha1coin.examples.ForwardingService.java

public static void main(String[] args) throws Exception {
    // This line makes the log output more compact and easily read, especially when using the JDK log adapter.
    BriefLogFormatter.init();// ww  w. j av a  2  s  .c  o  m
    if (args.length < 1) {
        System.err.println("Usage: address-to-send-back-to [regtest|testnet]");
        return;
    }

    // Figure out which network we should connect to. Each one gets its own set of files.
    NetworkParameters params;
    String filePrefix;
    if (args.length > 1 && args[1].equals("testnet")) {
        params = TestNet3Params.get();
        filePrefix = "forwarding-service-testnet";
    } else if (args.length > 1 && args[1].equals("regtest")) {
        params = RegTestParams.get();
        filePrefix = "forwarding-service-regtest";
    } else {
        params = MainNetParams.get();
        filePrefix = "forwarding-service";
    }
    // Parse the address given as the first parameter.
    forwardingAddress = new Address(params, args[0]);

    // Start up a basic app using a class that automates some boilerplate.
    kit = new WalletAppKit(params, new File("."), filePrefix);

    if (params == RegTestParams.get()) {
        // Regression test mode is designed for testing and development only, so there's no public network for it.
        // If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance.
        kit.connectToLocalHost();
    }

    // Download the block chain and wait until it's done.
    kit.startAsync();
    kit.awaitRunning();

    // We want to know when we receive money.
    kit.wallet().addEventListener(new AbstractWalletEventListener() {
        @Override
        public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) {
            // Runs in the dedicated "user thread" (see bitcoinj docs for more info on this).
            //
            // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast).
            Coin value = tx.getValueSentToMe(w);
            System.out.println("Received tx for " + value.toFriendlyString() + ": " + tx);
            System.out.println("Transaction will be forwarded after it confirms.");
            // Wait until it's made it into the block chain (may run immediately if it's already there).
            //
            // For this dummy app of course, we could just forward the unconfirmed transaction. If it were
            // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to
            // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common
            // case of waiting for a block.
            Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() {
                @Override
                public void onSuccess(Transaction result) {
                    // "result" here is the same as "tx" above, but we use it anyway for clarity.
                    forwardCoins(result);
                }

                @Override
                public void onFailure(Throwable t) {
                    // This kind of future can't fail, just rethrow in case something weird happens.
                    throw new RuntimeException(t);
                }
            });
        }
    });

    Address sendToAddress = kit.wallet().currentReceiveKey().toAddress(params);
    System.out.println("Send coins to: " + sendToAddress);
    System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit.");

    try {
        Thread.sleep(Long.MAX_VALUE);
    } catch (InterruptedException ignored) {
    }
}

From source file:com.google.spartancoin.examples.ForwardingService.java

public static void main(String[] args) throws Exception {
    // This line makes the log output more compact and easily read, especially when using the JDK log adapter.
    BriefLogFormatter.init();// w  w  w .  ja  v  a  2 s  .  c o  m
    if (args.length < 1) {
        System.err.println("Usage: address-to-send-back-to [regtest|testnet]");
        return;
    }

    // Figure out which network we should connect to. Each one gets its own set of files.
    NetworkParameters params;
    String filePrefix;
    if (args.length > 1 && args[1].equals("testnet")) {
        params = TestNet3Params.get();
        filePrefix = "forwarding-service-testnet";
    } else if (args.length > 1 && args[1].equals("regtest")) {
        params = RegTestParams.get();
        filePrefix = "forwarding-service-regtest";
    } else {
        params = MainNetParams.get();
        filePrefix = "forwarding-service";
    }
    // Parse the address given as the first parameter.
    forwardingAddress = new Address(params, args[0]);

    // Start up a basic app using a class that automates some boilerplate.
    kit = new WalletAppKit(params, new File("."), filePrefix);

    if (params == RegTestParams.get()) {
        // Regression test mode is designed for testing and development only, so there's no public network for it.
        // If you pick this mode, you're expected to be running a local "bitcoind -regtest" instance.
        kit.connectToLocalHost();
    }

    // Download the block chain and wait until it's done.
    kit.startAndWait();

    // We want to know when we receive money.
    kit.wallet().addEventListener(new AbstractWalletEventListener() {
        @Override
        public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) {
            // Runs in the dedicated "user thread" (see bitcoinj docs for more info on this).
            //
            // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast).
            BigInteger value = tx.getValueSentToMe(w);
            System.out.println("Received tx for " + Utils.bitcoinValueToFriendlyString(value) + ": " + tx);
            System.out.println("Transaction will be forwarded after it confirms.");
            // Wait until it's made it into the block chain (may run immediately if it's already there).
            //
            // For this dummy app of course, we could just forward the unconfirmed transaction. If it were
            // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to
            // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common
            // case of waiting for a block.
            Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() {
                @Override
                public void onSuccess(Transaction result) {
                    // "result" here is the same as "tx" above, but we use it anyway for clarity.
                    forwardCoins(result);
                }

                @Override
                public void onFailure(Throwable t) {
                    // This kind of future can't fail, just rethrow in case something weird happens.
                    throw new RuntimeException(t);
                }
            });
        }
    });

    Address sendToAddress = kit.wallet().getKeys().get(0).toAddress(params);
    System.out.println("Send coins to: " + sendToAddress);
    System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit.");

    try {
        Thread.sleep(Long.MAX_VALUE);
    } catch (InterruptedException ignored) {
    }
}

From source file:com.woollysammoth.nubitj.examples.ForwardingService.java

public static void main(String[] args) throws Exception {
    // This line makes the log output more compact and easily read, especially when using the JDK log adapter.
    BriefLogFormatter.init();//  w ww  .j a  va 2 s. c o m
    if (args.length < 1) {
        System.err.println("Usage: address-to-send-back-to [regtest|testnet]");
        return;
    }

    // Figure out which network we should connect to. Each one gets its own set of files.
    NetworkParameters params;
    String filePrefix;
    if (args.length > 1 && args[1].equals("testnet")) {
        params = TestNet3Params.get();
        filePrefix = "forwarding-service-testnet";
    } else if (args.length > 1 && args[1].equals("regtest")) {
        params = RegTestParams.get();
        filePrefix = "forwarding-service-regtest";
    } else {
        params = MainNetParams.get();
        filePrefix = "forwarding-service";
    }
    // Parse the address given as the first parameter.
    forwardingAddress = new Address(params, args[0]);

    // Start up a basic app using a class that automates some boilerplate.
    kit = new WalletAppKit(params, new File("."), filePrefix);

    if (params == RegTestParams.get()) {
        // Regression test mode is designed for testing and development only, so there's no public network for it.
        // If you pick this mode, you're expected to be running a local "nubitd -regtest" instance.
        kit.connectToLocalHost();
    }

    // Download the block chain and wait until it's done.
    kit.startAndWait();

    // We want to know when we receive money.
    kit.wallet().addEventListener(new AbstractWalletEventListener() {
        @Override
        public void onCoinsReceived(Wallet w, Transaction tx, BigInteger prevBalance, BigInteger newBalance) {
            // Runs in the dedicated "user thread" (see nubitj docs for more info on this).
            //
            // The transaction "tx" can either be pending, or included into a block (we didn't see the broadcast).
            BigInteger value = tx.getValueSentToMe(w);
            System.out.println("Received tx for " + Utils.nubitValueToFriendlyString(value) + ": " + tx);
            System.out.println("Transaction will be forwarded after it confirms.");
            // Wait until it's made it into the block chain (may run immediately if it's already there).
            //
            // For this dummy app of course, we could just forward the unconfirmed transaction. If it were
            // to be double spent, no harm done. Wallet.allowSpendingUnconfirmedTransactions() would have to
            // be called in onSetupCompleted() above. But we don't do that here to demonstrate the more common
            // case of waiting for a block.
            Futures.addCallback(tx.getConfidence().getDepthFuture(1), new FutureCallback<Transaction>() {
                @Override
                public void onSuccess(Transaction result) {
                    // "result" here is the same as "tx" above, but we use it anyway for clarity.
                    forwardCoins(result);
                }

                @Override
                public void onFailure(Throwable t) {
                    // This kind of future can't fail, just rethrow in case something weird happens.
                    throw new RuntimeException(t);
                }
            });
        }
    });

    Address sendToAddress = kit.wallet().getKeys().get(0).toAddress(params);
    System.out.println("Send coins to: " + sendToAddress);
    System.out.println("Waiting for coins to arrive. Press Ctrl-C to quit.");

    try {
        Thread.sleep(Long.MAX_VALUE);
    } catch (InterruptedException ignored) {
    }
}