io.reactivex.Observable.toFuture()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(200)

本文整理了Java中io.reactivex.Observable.toFuture()方法的一些代码示例,展示了Observable.toFuture()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.toFuture()方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:toFuture

Observable.toFuture介绍

[英]Returns a Future representing the single value emitted by this Observable.

If the Observable emits more than one item, java.util.concurrent.Future will receive an java.lang.IllegalArgumentException. If the Observable is empty, java.util.concurrent.Futurewill receive an java.util.NoSuchElementException.

If the Observable may emit more than one item, use Observable.toList().toFuture().

Scheduler: toFuture does not operate by default on a particular Scheduler.
[中]

代码示例

代码示例来源:origin: ReactiveX/RxJava

  1. @Override
  2. public Integer apply(Integer v) throws Exception {
  3. Observable.just(1).delay(10, TimeUnit.SECONDS).toFuture().get();
  4. return v;
  5. }
  6. })

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testToFuture() throws InterruptedException, ExecutionException {
  3. Observable<String> obs = Observable.just("one");
  4. Future<String> f = obs.toFuture();
  5. assertEquals("one", f.get());
  6. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(expected = NoSuchElementException.class)
  2. public void testGetWithEmptyFlowable() throws Throwable {
  3. Observable<String> obs = Observable.empty();
  4. Future<String> f = obs.toFuture();
  5. try {
  6. f.get();
  7. }
  8. catch (ExecutionException e) {
  9. throw e.getCause();
  10. }
  11. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(/* timeout = 5000, */expected = IndexOutOfBoundsException.class)
  2. public void testExceptionWithMoreThanOneElement() throws Throwable {
  3. Observable<String> obs = Observable.just("one", "two");
  4. Future<String> f = obs.toFuture();
  5. try {
  6. // we expect an exception since there are more than 1 element
  7. f.get();
  8. fail("Should have thrown!");
  9. }
  10. catch (ExecutionException e) {
  11. throw e.getCause();
  12. }
  13. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Ignore("null value is not allowed")
  2. @Test
  3. public void testGetWithASingleNullItem() throws Exception {
  4. Observable<String> obs = Observable.just((String)null);
  5. Future<String> f = obs.toFuture();
  6. assertEquals(null, f.get());
  7. }
  8. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(expected = CancellationException.class)
  2. public void testGetAfterCancel() throws Exception {
  3. Observable<String> obs = Observable.never();
  4. Future<String> f = obs.toFuture();
  5. boolean cancelled = f.cancel(true);
  6. assertTrue(cancelled); // because OperationNeverComplete never does
  7. f.get(); // Future.get() docs require this to throw
  8. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(expected = CancellationException.class)
  2. public void testGetWithTimeoutAfterCancel() throws Exception {
  3. Observable<String> obs = Observable.never();
  4. Future<String> f = obs.toFuture();
  5. boolean cancelled = f.cancel(true);
  6. assertTrue(cancelled); // because OperationNeverComplete never does
  7. f.get(Long.MAX_VALUE, TimeUnit.NANOSECONDS); // Future.get() docs require this to throw
  8. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testToFutureWithException() {
  3. Observable<String> obs = Observable.unsafeCreate(new ObservableSource<String>() {
  4. @Override
  5. public void subscribe(Observer<? super String> observer) {
  6. observer.onSubscribe(Disposables.empty());
  7. observer.onNext("one");
  8. observer.onError(new TestException());
  9. }
  10. });
  11. Future<String> f = obs.toFuture();
  12. try {
  13. f.get();
  14. fail("expected exception");
  15. } catch (Throwable e) {
  16. assertEquals(TestException.class, e.getCause().getClass());
  17. }
  18. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void fromFutureTimeout() throws Exception {
  3. Observable.fromFuture(Observable.never()
  4. .toFuture(), 100, TimeUnit.MILLISECONDS, Schedulers.io())
  5. .test()
  6. .awaitDone(5, TimeUnit.SECONDS)
  7. .assertFailure(TimeoutException.class);
  8. }

代码示例来源:origin: nemtech/nem2-docs

  1. @Test
  2. void gettingBlockByHeight() throws ExecutionException, InterruptedException, MalformedURLException {
  3. final BlockchainHttp blockchainHttp = new BlockchainHttp("http://localhost:3000");
  4. // Replace with block height
  5. final BigInteger blockHeight = BigInteger.valueOf(1);
  6. final BlockInfo blockInfo = blockchainHttp.getBlockByHeight(blockHeight).toFuture().get();
  7. System.out.print(blockInfo);
  8. }
  9. }

代码示例来源:origin: nemtech/nem2-docs

  1. @Test
  2. void gettingLastBlockchainBlock() throws ExecutionException, InterruptedException, MalformedURLException {
  3. final BlockchainHttp blockchainHttp = new BlockchainHttp("http://localhost:3000");
  4. final BigInteger blockchainHeight = blockchainHttp.getBlockchainHeight().toFuture().get();
  5. System.out.print(blockchainHeight);
  6. }
  7. }

代码示例来源:origin: nemtech/nem2-docs

  1. @Test
  2. void gettingAccountInformation() throws ExecutionException, InterruptedException, MalformedURLException {
  3. final AccountHttp accountHttp = new AccountHttp("http://localhost:3000");
  4. // Replace with address
  5. final String address = "SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54";
  6. final AccountInfo accountInfo = accountHttp.getAccountInfo(Address.createFromRawAddress(address)).toFuture().get();
  7. System.out.println(accountInfo);
  8. }
  9. }

代码示例来源:origin: nemtech/nem2-docs

  1. @Test
  2. void listeningNewBlocks() throws ExecutionException, InterruptedException, MalformedURLException {
  3. Listener listener = new Listener("http://localhost:3000");
  4. listener.open().get();
  5. BlockInfo blockInfo = listener.newBlock().take(1).toFuture().get();
  6. System.out.println(blockInfo);
  7. }
  8. }

代码示例来源:origin: nemtech/nem2-docs

  1. @Test
  2. void gettingMultisigAccountInformation() throws ExecutionException, InterruptedException, MalformedURLException {
  3. final AccountHttp accountHttp = new AccountHttp("http://localhost:3000");
  4. // Replace with address
  5. final String addressRaw = "SB2RPH-EMTFMB-KELX2Y-Q3MZTD-RV7DQG-UZEADV-CYKC";
  6. final Address address = Address.createFromRawAddress(addressRaw);
  7. final MultisigAccountInfo multisigAccountInfo = accountHttp.getMultisigAccountInfo(address).toFuture().get();
  8. System.out.println(multisigAccountInfo);
  9. }
  10. }

代码示例来源:origin: nemtech/nem2-docs

  1. @Test
  2. void checkingNamespaceExistence() throws ExecutionException, InterruptedException, MalformedURLException {
  3. final NamespaceId namespaceId = new NamespaceId("foo");
  4. final NamespaceHttp namespaceHttp = new NamespaceHttp("http://localhost:3000");
  5. final NamespaceInfo namespaceInfo = namespaceHttp.getNamespace(namespaceId).toFuture().get();
  6. System.out.println(namespaceInfo);
  7. }
  8. }

代码示例来源:origin: nemtech/nem2-docs

  1. @Test
  2. void debuggingTransactionsConfirmed() throws ExecutionException, InterruptedException, MalformedURLException {
  3. Listener listener = new Listener("http://localhost:3000");
  4. Address address = Address.createFromRawAddress("SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54");
  5. listener.open().get();
  6. Transaction transaction = listener.confirmed(address).take(1).toFuture().get();
  7. System.out.println(transaction);
  8. }
  9. }

代码示例来源:origin: nemtech/nem2-docs

  1. @Test
  2. void gettingConfirmedTransactions() throws ExecutionException, InterruptedException, MalformedURLException {
  3. final AccountHttp accountHttp = new AccountHttp("http://localhost:3000");
  4. // Replace with public key
  5. final String publicKey = "";
  6. final PublicAccount publicAccount = PublicAccount.createFromPublicKey(publicKey, NetworkType.MIJIN_TEST);
  7. // Page size between 10 and 100, otherwise 10
  8. int pageSize = 20;
  9. final List<Transaction> transactions = accountHttp.transactions(publicAccount, new QueryParams(pageSize, null)).toFuture().get();
  10. System.out.print(transactions);
  11. }
  12. }

代码示例来源:origin: nemtech/nem2-docs

  1. @Test
  2. void registeringANamespace() throws ExecutionException, InterruptedException, MalformedURLException {
  3. // Replace with private key
  4. final String privateKey = "";
  5. final Account account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);
  6. // Replace with namespace name
  7. final String namespaceName = "foo";
  8. final RegisterNamespaceTransaction registerNamespaceTransaction = RegisterNamespaceTransaction.createRootNamespace(
  9. Deadline.create(2, ChronoUnit.HOURS),
  10. namespaceName,
  11. BigInteger.valueOf(1000),
  12. NetworkType.MIJIN_TEST
  13. );
  14. final SignedTransaction signedTransaction = account.sign(registerNamespaceTransaction);
  15. final TransactionHttp transactionHttp = new TransactionHttp("http://localhost:3000");
  16. transactionHttp.announce(signedTransaction).toFuture().get();
  17. }
  18. }

代码示例来源:origin: nemtech/nem2-docs

  1. @Test
  2. void signingAnnouncedAggregateBondedTransactionsAutomatically() throws ExecutionException, InterruptedException, MalformedURLException {
  3. // Replace with a private key
  4. final String privateKey = "";
  5. final Account account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);
  6. final AccountHttp accountHttp = new AccountHttp("http://localhost:3000");
  7. final TransactionHttp transactionHttp = new TransactionHttp("http://localhost:3000");
  8. final Listener listener = new Listener("http://localhost:3000");
  9. listener.open().get();
  10. final AggregateTransaction transaction = listener.aggregateBondedAdded(account.getAddress()).take(1).toFuture().get();
  11. if (!transaction.signedByAccount(account.getPublicAccount())) {
  12. // Filter aggregates that need my signature
  13. final CosignatureTransaction cosignatureTransaction = CosignatureTransaction.create(transaction);
  14. final CosignatureSignedTransaction cosignatureSignedTransaction = account.signCosignatureTransaction(cosignatureTransaction);
  15. transactionHttp.announceAggregateBondedCosignature(cosignatureSignedTransaction).toFuture().get();
  16. }
  17. }
  18. }

代码示例来源:origin: nemtech/nem2-docs

  1. @Test
  2. void modifyingMosaicSupply() throws ExecutionException, InterruptedException, MalformedURLException {
  3. // Replace with private key
  4. final String privateKey = "";
  5. final Account account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);
  6. // Replace with mosaic id
  7. final MosaicId mosaicId = new MosaicId("foo:token"); // replace with mosaic full name
  8. MosaicSupplyChangeTransaction mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.create(
  9. new Deadline(2, ChronoUnit.HOURS),
  10. mosaicId,
  11. MosaicSupplyType.INCREASE,
  12. BigInteger.valueOf(2000000),
  13. NetworkType.MIJIN_TEST
  14. );
  15. final SignedTransaction signedTransaction = account.sign(mosaicSupplyChangeTransaction);
  16. final TransactionHttp transactionHttp = new TransactionHttp("http://localhost:3000");
  17. transactionHttp.announce(signedTransaction).toFuture().get();
  18. }
  19. }

相关文章

Observable类方法