@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(); }
@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(); }
@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(); }
@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(); }
private synchronized void doRead() { if (!readInProgress) { readInProgress = true; context.executeBlocking(this::readInWorker, true, this::afterReadInEventloop); } }
@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; }
Objects.requireNonNull(supplier); VertxCompletableFuture<T> future = new VertxCompletableFuture<>(context); context.<T>executeBlocking( fut -> { try {
/** * 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; }
@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(); }
@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(); }
/** * 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); }
/** * 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); }
private synchronized void doRead() { if (!readInProgress) { readInProgress = true; context.executeBlocking(this::readInWorker, true, this::afterReadInEventloop); } }
@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(); }
@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(); }
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) {
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) {
@Override public KafkaWriteStreamImpl<K, V> flush(Handler<Void> completionHandler) { this.context.executeBlocking(future -> { this.producer.flush(); future.complete(); }, ar -> completionHandler.handle(null)); return this; }
@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; }
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); }