本文整理汇总了Java中scala.concurrent.ExecutionContext类的典型用法代码示例。如果您正苦于以下问题:Java ExecutionContext类的具体用法?Java ExecutionContext怎么用?Java ExecutionContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ExecutionContext类属于scala.concurrent包,在下文中一共展示了ExecutionContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getListenerActorsInfo
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
@SuppressWarnings("checkstyle:IllegalCatch")
private List<DataTreeListenerInfo> getListenerActorsInfo(Collection<ActorSelection> actors) {
final Timeout timeout = new Timeout(20, TimeUnit.SECONDS);
final List<Future<Object>> futureList = new ArrayList<>(actors.size());
for (ActorSelection actor: actors) {
futureList.add(Patterns.ask(actor, GetInfo.INSTANCE, timeout));
}
try {
final List<DataTreeListenerInfo> listenerInfoList = new ArrayList<>();
Await.result(Futures.sequence(futureList, ExecutionContext.Implicits$.MODULE$.global()),
timeout.duration()).forEach(obj -> listenerInfoList.add((DataTreeListenerInfo) obj));
return listenerInfoList;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例2: completableFutureOf
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
/**
* Returns a new {@link CompletableFuture} equivalent to the given Scala {@link Future}.
*
* <p>Note that since Scala {@link Future} instances are unmodifiable, calling
* {@link CompletableFuture#cancel(boolean)} will have no effect on the original {@link Future} instance.
*
* @param scalaFuture the Scala {@link Future}
* @param executionContext execution context from which the returned {@link CompletableFuture} will be completed
* @param <T> type of the object that the futures will be completed with in case of success
* @return the new future
*/
static <T> CompletableFuture<T> completableFutureOf(
Future<T> scalaFuture, ExecutionContext executionContext) {
CompletableFuture<T> completableFuture = new CompletableFuture<>();
scalaFuture.onComplete(new OnComplete<T>() {
@Override
public void onComplete(@Nullable Throwable failure, @Nullable T success) {
if (failure != null) {
completableFuture.completeExceptionally(failure);
} else {
completableFuture.complete(success);
}
}
}, executionContext);
return completableFuture;
}
示例3: receiveHandler
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
public void receiveHandler(Object message) {
log.debug("Master client received: {}",message);
Timeout timeout = new Timeout(120, TimeUnit.SECONDS);
Future<Object> future = ask(masterProxy, message, timeout);
final ExecutionContext ec = getContext().system().dispatcher();
Future<Object> res = future.map(new Mapper<Object, Object>() {
@Override
public Object apply(Object msg) {
return new Ok(msg);
}
}, ec).recover(new Recover<Object>() {
@Override
public Object recover(Throwable failure) throws Throwable {
return new NotOk(null);
}
}, ec);
pipe(res, ec).to(getSender());
}
示例4: retry
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
/**
* Retries to send asynchronously a message up to numberRetries times. The response to this
* message is returned as a future. The message is re-sent if the number of retries is not yet
* exceeded and if an exception occurred while sending it.
*
* @param message Message to be sent
* @param numberRetries Number of times to retry sending the message
* @param timeout Timeout for each sending attempt
* @param executionContext ExecutionContext which is used to send the message multiple times
* @return Future of the response to the sent message
*/
@Override
public Future<Object> retry(
Object message,
int numberRetries,
FiniteDuration timeout,
ExecutionContext executionContext) {
Object newMessage = decorator.decorate(message);
return AkkaUtils.retry(
actor,
newMessage,
numberRetries,
executionContext,
timeout);
}
示例5: handleAsync
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
@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;
}
示例6: handleMessage
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
protected <T> void handleMessage(final Future<T> value, final ActorRef sender, final ActorRef self) {
final ExecutionContext ec = getContext().system().dispatcher();
value.onSuccess(new OnSuccess<T>() {
@Override
public void onSuccess(T result) throws Throwable {
sender.tell(new ExecutionResultMessage(result), self); // prevent message is null error
}
}, ec);
value.onFailure(new OnFailure() {
@Override
public void onFailure(Throwable failure) throws Throwable {
log.debug("Future failure: [{}]", failure.getMessage());
sender.tell(new akka.actor.Status.Failure(failure), self);
}
}, ec);
}
示例7: initInternal
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
private void initInternal(final ActorRef sender, final ActorRef self) {
if (!loading && !loaded) {
loading = true;
final ExecutionContext ec = getContext().system().dispatcher();
log.debug("Attempting init.");
Promise<Void> p = Futures.promise();
p.future().onFailure(new OnFailure() {
@Override
public void onFailure(Throwable failure) throws Throwable {
loading = false;
loaded = false;
}
}, ec);
p.future().onSuccess(new OnSuccess<Void>() {
@Override
public void onSuccess(Void result) throws Throwable {
loaded = true;
loading = false;
}
}, ec);
handleMessage(p.future(), sender, self);
init(p);
}
}
示例8: sendUsingTCP
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
private void sendUsingTCP(final MediatorSocketRequest request) throws IOException {
final Socket socket = getSocket(request);
ExecutionContext ec = getContext().dispatcher();
Future<Boolean> f = future(new Callable<Boolean>() {
public Boolean call() throws IOException {
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeBytes(request.getBody());
return Boolean.TRUE;
}
}, ec);
f.onComplete(new OnComplete<Boolean>() {
@Override
public void onComplete(Throwable ex, Boolean result) throws Throwable {
IOUtils.closeQuietly(socket);
if (ex!=null) {
log.error(ex, "Exception during TCP send");
}
}
}, ec);
}
示例9: onReceive
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
public void onReceive(Object message) {
Timeout timeout = new Timeout(5, TimeUnit.SECONDS);
Future<Object> f = ask(masterProxy, message, timeout);
final ExecutionContext ec = getContext().system().dispatcher();
Future<Object> res = f.map(new Mapper<Object, Object>() {
@Override
public Object apply(Object msg) {
if (msg instanceof Master.Ack)
return Ok.getInstance();
else
return super.apply(msg);
}
}, ec).recover(new Recover<Object>() {
@Override
public Object recover(Throwable failure) throws Throwable {
return NotOk.getInstance();
}
}, ec);
pipe(res, ec).to(getSender());
}
示例10: handleListChannels
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
private void handleListChannels(ListChannels listChannels) {
ActorRef sender = sender();
ActorRef self = self();
ExecutionContext ec = context().dispatcher();
Iterable<ActorRef> children = getContext().getChildren();
sequence(
stream(children.spliterator(), false)
.map(c -> ask(c, listChannels, 20).map(mapper(o -> (Channel) o), ec))
.collect(Collectors.toList()),
ec
).onSuccess(onSuccess(i -> sender.tell(Channels.of(i), self)), ec);
}
示例11: ActorGatewayResultPartitionConsumableNotifier
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
public ActorGatewayResultPartitionConsumableNotifier(
ExecutionContext executionContext,
ActorGateway jobManager,
FiniteDuration jobManagerMessageTimeout) {
this.executionContext = Preconditions.checkNotNull(executionContext);
this.jobManager = Preconditions.checkNotNull(jobManager);
this.jobManagerMessageTimeout = Preconditions.checkNotNull(jobManagerMessageTimeout);
}
示例12: ActorGatewayJobManagerCommunicationFactory
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
public ActorGatewayJobManagerCommunicationFactory(
ExecutionContext executionContext,
ActorGateway jobManagerGateway,
ActorGateway taskManagerGateway,
FiniteDuration jobManagerMessageTimeout) {
this.executionContext = Preconditions.checkNotNull(executionContext);
this.jobManagerGateway = Preconditions.checkNotNull(jobManagerGateway);
this.taskManagerGateway = Preconditions.checkNotNull(taskManagerGateway);
this.jobManagerMessageTimeout = Preconditions.checkNotNull(jobManagerMessageTimeout);
}
示例13: QueryableStateClient
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
/**
* Creates a client.
*
* @param lookupService Location lookup service
* @param kvStateClient Network client for queries
* @param executionContext Execution context for futures
*/
public QueryableStateClient(
KvStateLocationLookupService lookupService,
KvStateClient kvStateClient,
ExecutionContext executionContext) {
this.lookupService = Preconditions.checkNotNull(lookupService, "KvStateLocationLookupService");
this.kvStateClient = Preconditions.checkNotNull(kvStateClient, "KvStateClient");
this.executionContext = Preconditions.checkNotNull(executionContext, "ExecutionContext");
this.actorSystem = null;
this.lookupService.start();
}
示例14: thenComposeAsync
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
@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);
}
示例15: thenCombineAsync
import scala.concurrent.ExecutionContext; //导入依赖的package包/类
@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);
}