io.trane.future.Future.ensure()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(4.2k)|赞(0)|评价(0)|浏览(151)

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

Future.ensure介绍

[英]Runs r when this future completes.
[中]当这个未来完成时运行r。

代码示例

代码示例来源:origin: traneio/future

@Override
 final Future<T> apply(final Future<T> result) {
  return result.ensure(f);
 }
}

代码示例来源:origin: traneio/future

@Benchmark
public Void ensureConstN() throws CheckedFutureException {
 Future<Void> f = constVoidFuture;
 for (int i = 0; i < N.n; i++)
  f = f.ensure(ensureF);
 return f.get(Duration.ofMillis(Long.MAX_VALUE));
}

代码示例来源:origin: traneio/future

@Benchmark
public Void ensureConst() throws CheckedFutureException {
 return constVoidFuture.ensure(ensureF).get(Duration.ofMillis(Long.MAX_VALUE));
}

代码示例来源:origin: traneio/ndbc

private final <T> InterruptHandler handler(final Promise<T> p) {
 return ex -> {
  final DataSource<PreparedStatement, Row> ds = dataSourceSupplier.get();
  ds.execute("KILL QUERY " + connectionId)
    .onFailure(e -> log.warn("Can't cancel request. Reason: " + e))
    .ensure(() -> ds.close());
 };
}

代码示例来源:origin: traneio/ndbc

private final <T> Future<T> withConnection(final Function<Connection, Future<T>> f) {
  final Optional<Connection> transaction = currentTransaction.get();
  if (transaction.isPresent())
    return f.apply(transaction.get());
  else
    return pool.acquire().flatMap(c -> {
      return Future.flatApply(() -> f.apply(c)).ensure(() -> pool.release(c));
    });
}

代码示例来源:origin: traneio/future

@Benchmark
public Void ensurePromiseN() throws CheckedFutureException {
 Promise<Void> p = Promise.apply();
 Future<Void> f = p;
 for (int i = 0; i < N.n; i++)
  f = f.ensure(ensureF);
 p.setValue(null);
 return f.get(Duration.ofMillis(Long.MAX_VALUE));
}

代码示例来源:origin: traneio/ndbc

private final <T> Future<T> run(final Exchange<T> exchange) {
 final Promise<T> next = Promise.create(this::handler);
 final Future<?> previous = mutex.getAndSet(next);
 if (previous == null)
  next.become(execute(exchange));
 else
  previous.ensure(() -> next.become(execute(exchange)));
 return next;
}

代码示例来源:origin: traneio/ndbc

private final <T> Future<T> run(final Exchange<T> exchange) {
 final Promise<T> next = Promise.create(this::handler);
 final Future<?> previous = mutex.getAndSet(next);
 if (previous == null)
  next.become(execute(exchange));
 else
  previous.ensure(() -> next.become(execute(exchange)));
 return next;
}

代码示例来源:origin: traneio/ndbc

private final Future<Void> validateN(final int n) {
 if (n >= 0) {
  final T item = items.poll();
  if (item == null)
   return Future.VOID;
  else
   return item.isValid().rescue(e -> Future.FALSE).flatMap(valid -> {
    if (!valid)
     return item.close().rescue(e -> Future.VOID).ensure(() -> sizeSemaphore.release());
    else {
     items.offer(item);
     return Future.VOID;
    }
   }).flatMap(v -> validateN(n - 1));
 } else
  return Future.VOID;
}

代码示例来源:origin: traneio/future

/**
 * Selects the index of the first satisfied future.
 *
 * @param list  the list of futures to select from
 * @return      a future with the index of the first satisfied future of the list.
 */
public static <T> Future<Integer> selectIndex(final List<Future<T>> list) {
 switch (list.size()) {
 case 0:
  return Future.exception(new IllegalArgumentException("Can't select from empty list."));
 case 1:
  return list.get(0).map(v -> 0);
 default:
  final Promise<Integer> p = Promise.apply(list);
  int i = 0;
  for (final Future<?> f : list) {
   if (f instanceof SatisfiedFuture)
    return Future.value(i);
   final int ii = i;
   f.ensure(() -> p.becomeIfEmpty(Future.value(ii)));
   i++;
  }
  return p;
 }
}

代码示例来源:origin: traneio/ndbc

@Override
public final <T> Future<T> transactional(final Supplier<Future<T>> supplier) {
  if (currentTransaction.get().isPresent())
    return Future.flatApply(supplier);
  else
    return pool.acquire().flatMap(c -> {
      currentTransaction.set(Optional.of(c));
      return c.beginTransaction().flatMap(v -> supplier.get()).transformWith(new Transformer<T, Future<T>>() {
        @Override
        public Future<T> onException(final Throwable ex) {
          currentTransaction.set(Optional.empty());
          return c.rollback().flatMap(v -> Future.exception(ex));
        }
        @Override
        public Future<T> onValue(final T value) {
          currentTransaction.set(Optional.empty());
          return c.commit().map(v -> value);
        }
      }).ensure(() -> pool.release(c));
    });
}

相关文章