use of scala.concurrent.ExecutionContext in project flink by apache.
the class FlinkFuture method thenCombineAsync.
@Override
public <U, R> Future<R> thenCombineAsync(final Future<U> other, final BiFunction<? super T, ? super U, ? extends R> biFunction, final Executor executor) {
Preconditions.checkNotNull(other);
Preconditions.checkNotNull(biFunction);
Preconditions.checkNotNull(executor);
final ExecutionContext executionContext = createExecutionContext(executor);
final scala.concurrent.Future<U> thatScalaFuture;
if (other instanceof FlinkFuture) {
thatScalaFuture = ((FlinkFuture<U>) other).scalaFuture;
} else {
thatScalaFuture = Futures.future(new Callable<U>() {
@Override
public U call() throws Exception {
try {
return other.get();
} catch (ExecutionException e) {
// unwrap the execution exception if the cause is an Exception
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause();
} else {
// it's an error or a throwable which we have to wrap for the moment
throw new FlinkFuture.ThrowableWrapperException(e.getCause());
}
}
}
}, executionContext);
}
scala.concurrent.Future<R> result = scalaFuture.zip(thatScalaFuture).map(new Mapper<Tuple2<T, U>, R>() {
@Override
public R apply(Tuple2<T, U> tuple2) {
return biFunction.apply(tuple2._1, tuple2._2);
}
}, executionContext);
return new FlinkFuture<>(result);
}
use of scala.concurrent.ExecutionContext in project flink by apache.
the class RobustActorSystem method create.
private static RobustActorSystem create(String name, ActorSystemSetup setup, Option<Thread.UncaughtExceptionHandler> uncaughtExceptionHandler) {
final Optional<BootstrapSetup> bootstrapSettings = setup.get(BootstrapSetup.class);
final ClassLoader classLoader = RobustActorSystem.class.getClassLoader();
final Config appConfig = bootstrapSettings.map(BootstrapSetup::config).flatMap(RobustActorSystem::toJavaOptional).orElseGet(() -> ConfigFactory.load(classLoader));
final Option<ExecutionContext> defaultEC = toScalaOption(bootstrapSettings.map(BootstrapSetup::defaultExecutionContext).flatMap(RobustActorSystem::toJavaOptional));
final RobustActorSystem robustActorSystem = new RobustActorSystem(name, appConfig, classLoader, defaultEC, setup) {
@Override
public Thread.UncaughtExceptionHandler uncaughtExceptionHandler() {
return uncaughtExceptionHandler.getOrElse(super::uncaughtExceptionHandler);
}
};
robustActorSystem.start();
return robustActorSystem;
}
use of scala.concurrent.ExecutionContext in project flink by apache.
the class FlinkFuture method thenComposeAsync.
@Override
public <R> Future<R> thenComposeAsync(final ApplyFunction<? super T, ? extends Future<R>> applyFunction, Executor executor) {
Preconditions.checkNotNull(scalaFuture);
Preconditions.checkNotNull(applyFunction);
Preconditions.checkNotNull(executor);
final ExecutionContext executionContext = createExecutionContext(executor);
scala.concurrent.Future<R> flatMappedFuture = scalaFuture.flatMap(new Mapper<T, scala.concurrent.Future<R>>() {
@Override
public scala.concurrent.Future<R> apply(T value) {
final Future<? extends R> future = applyFunction.apply(value);
if (future instanceof FlinkFuture) {
@SuppressWarnings("unchecked") FlinkFuture<R> flinkFuture = (FlinkFuture<R>) future;
return flinkFuture.scalaFuture;
} else {
return Futures.future(new Callable<R>() {
@Override
public R call() throws Exception {
try {
return future.get();
} catch (ExecutionException e) {
// unwrap the execution exception if it's not a throwable
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause();
} else {
throw new FlinkFuture.ThrowableWrapperException(e.getCause());
}
}
}
}, executionContext);
}
}
}, executionContext);
return new FlinkFuture<>(flatMappedFuture);
}
use of scala.concurrent.ExecutionContext in project flink by apache.
the class FlinkFuture method handleAsync.
@Override
public <R> Future<R> handleAsync(final BiFunction<? super T, Throwable, ? extends R> biFunction, Executor executor) {
Preconditions.checkNotNull(scalaFuture);
Preconditions.checkNotNull(biFunction);
Preconditions.checkNotNull(executor);
final ExecutionContext executionContext = createExecutionContext(executor);
final CompletableFuture<R> resultFuture = new FlinkCompletableFuture<>();
scalaFuture.onComplete(new OnComplete<T>() {
@Override
public void onComplete(Throwable failure, T success) throws Throwable {
final R result = biFunction.apply(success, failure);
resultFuture.complete(result);
}
}, executionContext);
return resultFuture;
}
use of scala.concurrent.ExecutionContext in project controller by opendaylight.
the class ShardManager method onShutDown.
private void onShutDown() {
List<Future<Boolean>> stopFutures = new ArrayList<>(localShards.size());
for (ShardInformation info : localShards.values()) {
if (info.getActor() != null) {
LOG.debug("{}: Issuing gracefulStop to shard {}", persistenceId(), info.getShardId());
FiniteDuration duration = info.getDatastoreContext().getShardRaftConfig().getElectionTimeOutInterval().$times(2);
stopFutures.add(Patterns.gracefulStop(info.getActor(), duration, Shutdown.INSTANCE));
}
}
LOG.info("Shutting down ShardManager {} - waiting on {} shards", persistenceId(), stopFutures.size());
ExecutionContext dispatcher = new Dispatchers(context().system().dispatchers()).getDispatcher(Dispatchers.DispatcherType.Client);
Future<Iterable<Boolean>> combinedFutures = Futures.sequence(stopFutures, dispatcher);
combinedFutures.onComplete(new OnComplete<Iterable<Boolean>>() {
@Override
public void onComplete(final Throwable failure, final Iterable<Boolean> results) {
LOG.debug("{}: All shards shutdown - sending PoisonPill to self", persistenceId());
self().tell(PoisonPill.getInstance(), self());
if (failure != null) {
LOG.warn("{}: An error occurred attempting to shut down the shards", persistenceId(), failure);
} else {
int nfailed = 0;
for (Boolean result : results) {
if (!result) {
nfailed++;
}
}
if (nfailed > 0) {
LOG.warn("{}: {} shards did not shut down gracefully", persistenceId(), nfailed);
}
}
}
}, dispatcher);
}
Aggregations