List of usage examples for com.google.common.util.concurrent Futures addCallback
public static <V> void addCallback(ListenableFuture<V> future, FutureCallback<? super V> callback)
From source file:org.guldenj.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 . co 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 = Address.fromBase58(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().addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() { @Override public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) { // Runs in the dedicated "user thread" (see guldenj 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.matthewmitchell.peercoinj.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 ww w . j a va 2s . co 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 "peercoind -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 peercoinj 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:org.litecoinj.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 v a2 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.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 = Address.fromBase58(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().addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() { @Override public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) { // Runs in the dedicated "user thread" (see litecoinj 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:org.bitcoinj_extra.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 a v a2s . 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 = Address.fromBase58(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().addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() { @Override public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) { // Runs in the dedicated "user thread" (see bitcoinj_extra 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:org.bitcoinj.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 ww w.ja va2s. 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 = Address.fromBase58(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().addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() { @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<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.spotify.asyncdatastoreclient.example.ExampleAsync.java
public static void main(final String... args) throws Exception { final DatastoreConfig config = DatastoreConfig.builder().connectTimeout(5000).requestTimeout(1000) .maxConnections(5).requestRetry(3).dataset("my-dataset").namespace("my-namespace") .credential(DatastoreHelper.getComputeEngineCredential()).build(); final Datastore datastore = Datastore.create(config); // Add a two entities asynchronously final ListenableFuture<MutationResult> addFirst = addData(datastore); final ListenableFuture<MutationResult> addSecond = addDataInTransaction(datastore); final ListenableFuture<List<Object>> addBoth = Futures.allAsList(addFirst, addSecond); // Query the entities we've just inserted final ListenableFuture<QueryResult> query = Futures.transform(addBoth, (List<Object> result) -> { return queryData(datastore); });// w w w . j av a2s . c o m // Print the query results before clean up final ListenableFuture<MutationResult> delete = Futures.transform(query, (QueryResult result) -> { for (final Entity entity : result) { System.out.println("Employee name: " + entity.getString("fullname")); System.out.println("Employee age: " + entity.getInteger("age")); } return deleteData(datastore); }); Futures.addCallback(delete, new FutureCallback<MutationResult>() { @Override public void onSuccess(final MutationResult result) { System.out.println("All complete."); } @Override public void onFailure(final Throwable throwable) { System.err.println("Storage exception: " + Throwables.getRootCause(throwable).getMessage()); } }); }
From source file:com.google.devcoin.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 v a 2 s . c om*/ 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. Ensure we always have at least one key. kit = new WalletAppKit(params, new File("."), filePrefix) { @Override protected void onSetupCompleted() { // This is called in a background thread after startAndWait is called, as setting up various objects // can do disk and network IO that may cause UI jank/stuttering in wallet apps if it were to be done // on the main thread. if (wallet().getKeychainSize() < 1) wallet().addKey(new ECKey()); } }; 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.logicoin.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 va 2 s . 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. Ensure we always have at least one key. kit = new WalletAppKit(params, new File("."), filePrefix) { @Override protected void onSetupCompleted() { // This is called in a background thread after startAndWait is called, as setting up various objects // can do disk and network IO that may cause UI jank/stuttering in wallet apps if it were to be done // on the main thread. if (wallet().getKeychainSize() < 1) wallet().addKey(new ECKey()); } }; 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 "logicoind -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 logicoinj 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.logicoinValueToFriendlyString(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.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);// www .j av a2 s . c o 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.amazonaws.services.kinesis.producer.sample.MetricsAwareSampleProducer.java
public static void main(String[] args) throws InterruptedException, ExecutionException { final long totalRecordsToPut = 50000; final int dataSize = 64; final long outstandingLimit = 5000; final AtomicLong sequenceNumber = new AtomicLong(0); final AtomicLong completed = new AtomicLong(0); final String timetstamp = Long.toString(System.currentTimeMillis()); KinesisProducerConfiguration config = new KinesisProducerConfiguration().setRecordMaxBufferedTime(3000) .setMaxConnections(1).setRequestTimeout(60000).setRegion(SampleProducer.REGION); final KinesisProducer kinesisProducer = new KinesisProducer(config); // Result handler final FutureCallback<UserRecordResult> callback = new FutureCallback<UserRecordResult>() { @Override//from w w w . ja v a 2 s.c o m public void onFailure(Throwable t) { if (t instanceof UserRecordFailedException) { Attempt last = Iterables.getLast(((UserRecordFailedException) t).getResult().getAttempts()); log.error(String.format("Record failed to put - %s : %s", last.getErrorCode(), last.getErrorMessage())); } log.error("Exception during put", t); System.exit(1); } @Override public void onSuccess(UserRecordResult result) { completed.getAndIncrement(); } }; // Progress updates Thread progress = new Thread(new Runnable() { @Override public void run() { while (true) { long put = sequenceNumber.get(); double putPercent = 100.0 * put / totalRecordsToPut; long done = completed.get(); double donePercent = 100.0 * done / totalRecordsToPut; log.info(String.format("Put %d of %d so far (%.2f %%), %d have completed (%.2f %%)", put, totalRecordsToPut, putPercent, done, donePercent)); if (done == totalRecordsToPut) { break; } // Numerous metrics are available from the KPL locally, as // well as uploaded to CloudWatch. See the metrics // documentation for details. // // KinesisProducer provides methods to retrieve metrics for // the current instance, with a customizable time window. // This allows us to get sliding window statistics in real // time for the current host. // // Here we're going to look at the number of user records // put over a 5 seconds sliding window. try { for (Metric m : kinesisProducer.getMetrics("UserRecordsPut", 5)) { // Metrics are emitted at different granularities, here // we only look at the stream level metric, which has a // single dimension of stream name. if (m.getDimensions().size() == 1 && m.getSampleCount() > 0) { log.info(String.format( "(Sliding 5 seconds) Avg put rate: %.2f per sec, success rate: %.2f, failure rate: %.2f, total attemped: %d", m.getSum() / 5, m.getSum() / m.getSampleCount() * 100, (m.getSampleCount() - m.getSum()) / m.getSampleCount() * 100, (long) m.getSampleCount())); } } } catch (Exception e) { log.error("Unexpected error getting metrics", e); System.exit(1); } try { Thread.sleep(1000); } catch (InterruptedException e) { } } } }); progress.start(); // Put records while (true) { // We're going to put as fast as we can until we've reached the max // number of records outstanding. if (sequenceNumber.get() < totalRecordsToPut) { if (kinesisProducer.getOutstandingRecordsCount() < outstandingLimit) { ByteBuffer data = Utils.generateData(sequenceNumber.incrementAndGet(), dataSize); ListenableFuture<UserRecordResult> f = kinesisProducer.addUserRecord(SampleProducer.STREAM_NAME, timetstamp, data); Futures.addCallback(f, callback); } else { Thread.sleep(1); } } else { break; } } // Wait for remaining records to finish while (kinesisProducer.getOutstandingRecordsCount() > 0) { kinesisProducer.flush(); Thread.sleep(100); } progress.join(); for (Metric m : kinesisProducer.getMetrics("UserRecordsPerKinesisRecord")) { if (m.getDimensions().containsKey("ShardId")) { log.info(String.format( "%.2f user records were aggregated into each Kinesis record on average for shard %s, for a total of %d Kinesis records.", m.getMean(), m.getDimensions().get("ShardId"), (long) m.getSampleCount())); } } kinesisProducer.destroy(); log.info("Finished."); }