io.vertx.core.Context.executeBlocking()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(147)

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

Context.executeBlocking介绍

[英]Invoke #executeBlocking(Handler,boolean,Handler) with order = true.
[中]使用order=true调用#executeBlock(处理程序,布尔值,处理程序)。

代码示例

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testExecuteBlockingThreadAsyncComplete() throws Exception {
 Context context = vertx.getOrCreateContext();
 context.<Void>runOnContext(v -> {
  Thread expected = Thread.currentThread();
  context.executeBlocking(fut -> {
   new Thread(() -> {
    try {
     // Wait some time to allow the worker thread to set the handler on the future and have the future
     // handler callback to be done this thread
     Thread.sleep(200);
    } catch (InterruptedException e) {
     Thread.currentThread().interrupt();
    }
    fut.complete();
   }).start();
  }, r -> {
   assertSame(context, Vertx.currentContext());
   assertSame(expected, Thread.currentThread());
   testComplete();
  });
 });
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testExecuteBlockingThreadSyncComplete() throws Exception {
 Context context = vertx.getOrCreateContext();
 context.<Void>runOnContext(v -> {
  Thread expected = Thread.currentThread();
  context.executeBlocking(Future::complete, r -> {
   assertSame(expected, Thread.currentThread());
   testComplete();
  });
 });
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testExecuteOrderedBlocking() throws Exception {
 Context context = vertx.getOrCreateContext();
 context.executeBlocking(f -> {
  assertTrue(Context.isOnWorkerThread());
  f.complete(1 + 2);
 }, r -> {
  assertTrue(Context.isOnEventLoopThread());
  assertEquals(r.result(), 3);
  testComplete();
 });
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testExecuteUnorderedBlocking() throws Exception {
 Context context = vertx.getOrCreateContext();
 context.executeBlocking(f -> {
  assertTrue(Context.isOnWorkerThread());
  f.complete(1 + 2);
 }, false, r -> {
  assertTrue(Context.isOnEventLoopThread());
  assertEquals(r.result(), 3);
  testComplete();
 });
 await();
}

代码示例来源:origin: apache/servicecomb-java-chassis

private synchronized void doRead() {
 if (!readInProgress) {
  readInProgress = true;
  context.executeBlocking(this::readInWorker,
    true,
    this::afterReadInEventloop);
 }
}

代码示例来源:origin: apache/servicecomb-java-chassis

@Override
public synchronized WriteStream<Buffer> write(Buffer data) {
 currentBufferCount++;
 buffers.add(data);
 context.executeBlocking(this::writeInWorker,
   true,
   ar -> {
    if (ar.failed()) {
     handleException(ar.cause());
    }
   });
 return this;
}

代码示例来源:origin: gravitee-io/gravitee-gateway

Objects.requireNonNull(supplier);
VertxCompletableFuture<T> future = new VertxCompletableFuture<>(context);
context.<T>executeBlocking(
    fut -> {
      try {

代码示例来源:origin: gravitee-io/gravitee-gateway

/**
 * Returns a new CompletableFuture that is asynchronously completed by a action running in the worker thread pool of
 * Vert.x
 * <p>
 * This method is different from {@link CompletableFuture#runAsync(Runnable)} as it does not use a fork join
 * executor, but the worker thread pool.
 *
 * @param context  the Vert.x context
 * @param runnable the action, when its execution completes, it completes the returned CompletableFuture. If the
 *                 execution throws an exception, the returned CompletableFuture is completed exceptionally.
 * @return the new CompletableFuture
 */
public static VertxCompletableFuture<Void> runBlockingAsync(Context context, Runnable runnable) {
  Objects.requireNonNull(runnable);
  VertxCompletableFuture<Void> future = new VertxCompletableFuture<>(Objects.requireNonNull(context));
  context.executeBlocking(
      fut -> {
        try {
          runnable.run();
          future.complete(null);
        } catch (Throwable e) {
          future.completeExceptionally(e);
        }
      },
      null
  );
  return future;
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void testExecuteBlockingThreadAsyncComplete() throws Exception {
 Context context = vertx.getOrCreateContext();
 context.<Void>runOnContext(v -> {
  Thread expected = Thread.currentThread();
  context.executeBlocking(fut -> {
   new Thread(() -> {
    try {
     // Wait some time to allow the worker thread to set the handler on the future and have the future
     // handler callback to be done this thread
     Thread.sleep(200);
    } catch (InterruptedException e) {
     Thread.currentThread().interrupt();
    }
    fut.complete();
   }).start();
  }, r -> {
   assertSame(context, Vertx.currentContext());
   assertSame(expected, Thread.currentThread());
   testComplete();
  });
 });
 await();
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void testExecuteBlockingThreadSyncComplete() throws Exception {
 Context context = vertx.getOrCreateContext();
 context.<Void>runOnContext(v -> {
  Thread expected = Thread.currentThread();
  context.executeBlocking(Future::complete, r -> {
   assertSame(expected, Thread.currentThread());
   testComplete();
  });
 });
 await();
}

代码示例来源:origin: io.vertx/vertx-rx-java

/**
 * Invoke {@link io.vertx.rxjava.core.Context#executeBlocking} with order = true.
 * @param blockingCodeHandler handler representing the blocking code to run
 * @param resultHandler handler that will be called when the blocking code is complete
 */
public <T> void executeBlocking(Handler<io.vertx.rxjava.core.Future<T>> blockingCodeHandler, Handler<AsyncResult<T>> resultHandler) { 
 delegate.executeBlocking(new Handler<io.vertx.core.Future<T>>() {
  public void handle(io.vertx.core.Future<T> event) {
   blockingCodeHandler.handle(io.vertx.rxjava.core.Future.newInstance(event, io.vertx.lang.rx.TypeArg.unknown()));
  }
 }, resultHandler);
}

代码示例来源:origin: vert-x3/vertx-rx

/**
 * Invoke {@link io.vertx.rxjava.core.Context#executeBlocking} with order = true.
 * @param blockingCodeHandler handler representing the blocking code to run
 * @param resultHandler handler that will be called when the blocking code is complete
 */
public <T> void executeBlocking(Handler<io.vertx.rxjava.core.Future<T>> blockingCodeHandler, Handler<AsyncResult<T>> resultHandler) { 
 delegate.executeBlocking(new Handler<io.vertx.core.Future<T>>() {
  public void handle(io.vertx.core.Future<T> event) {
   blockingCodeHandler.handle(io.vertx.rxjava.core.Future.newInstance(event, io.vertx.lang.rx.TypeArg.unknown()));
  }
 }, resultHandler);
}

代码示例来源:origin: org.apache.servicecomb/foundation-vertx

private synchronized void doRead() {
 if (!readInProgress) {
  readInProgress = true;
  context.executeBlocking(this::readInWorker,
    true,
    this::afterReadInEventloop);
 }
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void testExecuteOrderedBlocking() throws Exception {
 Context context = vertx.getOrCreateContext();
 context.executeBlocking(f -> {
  assertTrue(Context.isOnWorkerThread());
  f.complete(1 + 2);
 }, r -> {
  assertTrue(Context.isOnEventLoopThread());
  assertEquals(r.result(), 3);
  testComplete();
 });
 await();
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void testExecuteUnorderedBlocking() throws Exception {
 Context context = vertx.getOrCreateContext();
 context.executeBlocking(f -> {
  assertTrue(Context.isOnWorkerThread());
  f.complete(1 + 2);
 }, false, r -> {
  assertTrue(Context.isOnEventLoopThread());
  assertEquals(r.result(), 3);
  testComplete();
 });
 await();
}

代码示例来源:origin: io.vertx/vertx-lang-groovy

public static <T>void executeBlocking(io.vertx.core.Context j_receiver, io.vertx.core.Handler<io.vertx.core.Future<java.lang.Object>> blockingCodeHandler, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.lang.Object>> resultHandler) {
 j_receiver.executeBlocking(blockingCodeHandler != null ? event -> blockingCodeHandler.handle(io.vertx.core.impl.ConversionHelper.fromObject(event)) : null,
  resultHandler != null ? new io.vertx.core.Handler<io.vertx.core.AsyncResult<java.lang.Object>>() {
  public void handle(io.vertx.core.AsyncResult<java.lang.Object> ar) {
   resultHandler.handle(ar.map(event -> io.vertx.core.impl.ConversionHelper.fromObject(event)));
  }
 } : null);
}
public static java.util.Map<String, Object> config(io.vertx.core.Context j_receiver) {

代码示例来源:origin: io.vertx/vertx-lang-groovy

public static <T>void executeBlocking(io.vertx.core.Context j_receiver, io.vertx.core.Handler<io.vertx.core.Future<java.lang.Object>> blockingCodeHandler, boolean ordered, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.lang.Object>> resultHandler) {
 j_receiver.executeBlocking(blockingCodeHandler != null ? event -> blockingCodeHandler.handle(io.vertx.core.impl.ConversionHelper.fromObject(event)) : null,
  ordered,
  resultHandler != null ? new io.vertx.core.Handler<io.vertx.core.AsyncResult<java.lang.Object>>() {
  public void handle(io.vertx.core.AsyncResult<java.lang.Object> ar) {
   resultHandler.handle(ar.map(event -> io.vertx.core.impl.ConversionHelper.fromObject(event)));
  }
 } : null);
}
public static <T>void executeBlocking(io.vertx.core.Context j_receiver, io.vertx.core.Handler<io.vertx.core.Future<java.lang.Object>> blockingCodeHandler, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.lang.Object>> resultHandler) {

代码示例来源:origin: vert-x3/vertx-kafka-client

@Override
public KafkaWriteStreamImpl<K, V> flush(Handler<Void> completionHandler) {
 this.context.executeBlocking(future -> {
  this.producer.flush();
  future.complete();
 }, ar -> completionHandler.handle(null));
 return this;
}

代码示例来源:origin: org.apache.servicecomb/foundation-vertx

@Override
public synchronized WriteStream<Buffer> write(Buffer data) {
 currentBufferCount++;
 buffers.add(data);
 context.executeBlocking(this::writeInWorker,
   true,
   ar -> {
    if (ar.failed()) {
     handleException(ar.cause());
    }
   });
 return this;
}

代码示例来源:origin: vert-x3/vertx-kafka-client

public void close(long timeout, Handler<AsyncResult<Void>> completionHandler) {
 this.context.executeBlocking(future -> {
  if (timeout > 0) {
   this.producer.close(timeout, TimeUnit.MILLISECONDS);
  } else {
   this.producer.close();
  }
  future.complete();
 }, completionHandler);
}

相关文章