@Override public Vertx vertx() { return ctx.owner(); }
LockWaiter(Context context, String lockName, long timeout, Handler<AsyncResult<Lock>> handler) { this.context = context; this.lockName = lockName; this.handler = handler; status = new AtomicReference<>(Status.WAITING); timerId = timeout != Long.MAX_VALUE ? context.owner().setTimer(timeout, tid -> timeout()) : null; }
void acquireLock() { if (status.compareAndSet(Status.WAITING, Status.ACQUIRED)) { if (timerId != null) { context.owner().cancelTimer(timerId); } context.runOnContext(v -> handler.handle(Future.succeededFuture(new AsyncLock(lockName)))); } else { context.runOnContext(v -> nextWaiter(lockName)); } } }
public synchronized <T> void executeBlocking(Handler<Future<T>> blockingCodeHandler, boolean ordered, Handler<AsyncResult<T>> asyncResultHandler) { if (closed) { throw new IllegalStateException("Worker executor closed"); } ContextImpl context = (ContextImpl) ctx.owner().getOrCreateContext(); context.executeBlocking(blockingCodeHandler, asyncResultHandler, pool.executor(), ordered ? context.orderedTasks : null, pool.metrics()); }
@Override public HttpServerFileUpload streamToFileSystem(String filename) { pause(); context.owner().fileSystem().open(filename, new OpenOptions(), ar -> { if (ar.succeeded()) { file = ar.result(); Pump p = Pump.pump(HttpServerFileUploadImpl.this, ar.result()); p.start(); resume(); } else { notifyExceptionHandler(ar.cause()); } }); return this; }
@Override Future<Void> handle(ReadStream<Buffer> stream) { Future<Void> fut = Future.future(); assert fetching.getAndSet(false); Vertx.currentContext().owner().setTimer(1, id -> { assert !fetching.getAndSet(true); stream.fetch(1); fut.complete(); }); return fut; } };
@Override Future<Void> handle(ReadStream<Buffer> stream) { Future<Void> fut = Future.future(); assert flowing.getAndSet(false); stream.pause(); Vertx.currentContext().owner().setTimer(1, id -> { assert !flowing.getAndSet(true); stream.resume(); fut.complete(); }); return fut; } },
@Override public Vertx vertx() { return ctx.owner(); }
protected void startCheckTimeout(Context context) { context.owner().setPeriodic(TimeUnit.SECONDS.toMillis(1), this::onCheckTimeout); }
@Override public boolean setTimeout(long time, TimeUnit unit) { synchronized (responseLock) { if (done || cancelled) return false; if (timerID > -1 && !context.owner().cancelTimer(timerID)) { return false; } timerID = context.owner().setTimer(unit.toMillis(time), v -> handleTimeout()); } return true; }
LockWaiter(Context context, String lockName, long timeout, Handler<AsyncResult<Lock>> handler) { this.context = context; this.lockName = lockName; this.handler = handler; status = new AtomicReference<>(Status.WAITING); timerId = timeout != Long.MAX_VALUE ? context.owner().setTimer(timeout, tid -> timeout()) : null; }
@Override public void execute(Runnable command) { Vertx.currentContext().owner().executeBlocking(future -> { command.run(); }, false, null); } }
@Override public CLIENT_POOL createClientPool(Context context) { Vertx vertx = context.owner(); NetClientWrapper netClientWrapper = new NetClientWrapper(vertx, normalClientConfig, sslClientConfig); return doCreateClientPool(context, netClientWrapper); }
@Override public HttpClientWithContext createClientPool(Context context) { HttpClient httpClient = context.owner().createHttpClient(httpClientOptions); return new HttpClientWithContext(httpClient, context); } }
void acquireLock() { if (status.compareAndSet(Status.WAITING, Status.ACQUIRED)) { if (timerId != null) { context.owner().cancelTimer(timerId); } context.runOnContext(v -> handler.handle(Future.succeededFuture(new AsyncLock(lockName)))); } else { context.runOnContext(v -> nextWaiter(lockName)); } } }
public synchronized <T> void executeBlocking(Handler<Future<T>> blockingCodeHandler, boolean ordered, Handler<AsyncResult<T>> asyncResultHandler) { if (closed) { throw new IllegalStateException("Worker executor closed"); } ContextImpl context = (ContextImpl) ctx.owner().getOrCreateContext(); context.executeBlocking(blockingCodeHandler, asyncResultHandler, pool.executor(), ordered ? context.orderedTasks : null, pool.metrics()); }
/** * * @param file * @param openOptions * @return future of save to file, future complete means write to file finished */ public CompletableFuture<File> saveToFile(File file, OpenOptions openOptions) { CompletableFuture<File> future = new CompletableFuture<>(); context.runOnContext((v) -> { Vertx vertx = context.owner(); vertx.fileSystem().open(file.getAbsolutePath(), openOptions, ar -> { onFileOpened(file, ar, future); }); }); return future; }
@Override public HttpServerFileUpload streamToFileSystem(String filename) { pause(); context.owner().fileSystem().open(filename, new OpenOptions(), ar -> { if (ar.succeeded()) { file = ar.result(); Pump p = Pump.pump(HttpServerFileUploadImpl.this, ar.result()); p.start(); resume(); } else { notifyExceptionHandler(ar.cause()); } }); return this; }
@Override Future<Void> handle(ReadStream<Buffer> stream) { Future<Void> fut = Future.future(); assert fetching.getAndSet(false); Vertx.currentContext().owner().setTimer(1, id -> { assert !fetching.getAndSet(true); stream.fetch(1); fut.complete(); }); return fut; } };
@Override Future<Void> handle(ReadStream<Buffer> stream) { Future<Void> fut = Future.future(); assert flowing.getAndSet(false); stream.pause(); Vertx.currentContext().owner().setTimer(1, id -> { assert !flowing.getAndSet(true); stream.resume(); fut.complete(); }); return fut; } },