private void acquireLock(Handler resultHandler, Handler<Lock> next) { vertx .sharedData() .getLock( "service.registry.lock", maybeLock -> { if (maybeLock.failed()) { resultHandler.handle(Future.failedFuture("Cannot acquire registry lock")); } else { next.handle(maybeLock.result()); } }); }
private void failTest(Handler<TestContext> thrower) { AtomicReference<Throwable> failure = new AtomicReference<>(); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { try { thrower.handle(context); } catch (Error | RuntimeException e) { failure.set(e); throw e; } }); TestReporter reporter = new TestReporter(); run(suite, reporter); reporter.await(); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); TestResult result = reporter.results.get(0); assertEquals("my_test", result.name()); assertFalse(result.succeeded()); assertTrue(result.failed()); assertNotNull(result.failure()); assertSame(failure.get().getMessage(), result.failure().message()); assertSame(failure.get(), result.failure().cause()); }
void handleChunk(Buffer data) { synchronized (conn) { if (paused) { if (pausedChunk == null) { pausedChunk = data.copy(); } else { pausedChunk.appendBuffer(data); } } else { request.dataReceived(); if (pausedChunk != null) { data = pausedChunk.appendBuffer(data); pausedChunk = null; } bytesRead += data.length(); if (dataHandler != null) { try { dataHandler.handle(data); } catch (Throwable t) { handleException(t); } } } } }
void handleException(Throwable e) { synchronized (conn) { if (exceptionHandler != null) { exceptionHandler.handle(e); } } }
private synchronized void callDrainHandler() { if (drainHandler != null) { if (!writeQueueFull()) { drainHandler.handle(null); } } }
void start() { connection.setErrorHandler( th -> { log.debug("QUIT failed, ignoring exception", th); resultHandler.handle(null); }); connection.write( "QUIT", message -> { log.debug("QUIT result: " + message); if (!StatusCode.isStatusOk(message)) { log.warn("quit failed: " + message); } resultHandler.handle(null); }); }
@Override protected synchronized void handleClosed() { checkContext(); if (endHandler != null) { endHandler.handle(null); } super.handleClosed(); if (vertx.eventBus() != null) { registration.unregister(); } }
void handleDrained() { synchronized (getLock()) { if (drainHandler != null) { try { drainHandler.handle(null); } catch (Throwable t) { handleException(t); } } } }
void handleResponse(HttpClientResponseImpl resp) { synchronized (getLock()) { // If an exception occurred (e.g. a timeout fired) we won't receive the response. if (!exceptionOccurred) { cancelOutstandingTimeoutTimer(); try { if (resp.statusCode() == 100) { if (continueHandler != null) { continueHandler.handle(null); } } else { if (respHandler != null) { respHandler.handle(resp); } if (endHandler != null) { endHandler.handle(null); } } } catch (Throwable t) { handleException(t); } } } }
@Override public void findUser(Handler<User> handler, String userName) { findUsers( result -> { User user; if (result.size() == 0) { user = null; } else { // Should only be one with this name user = result.get(0); } handler.handle(user); }, new JsonObject().put("userName", userName)); }
@Override public HttpClientRequest exceptionHandler(Handler<Throwable> handler) { synchronized (getLock()) { if (handler != null) { checkComplete(); this.exceptionHandler = t -> { cancelOutstandingTimeoutTimer(); handler.handle(t); }; } else { this.exceptionHandler = null; } return this; } }
private void getRegistry( Lock lock, Handler resultHandler, Handler<Map<String, JsonObject>> next) { vertx.<Map<String, JsonObject>>executeBlocking( future -> { Map<String, JsonObject> map = ((VertxInternal) vertx).getClusterManager().getSyncMap("service.registry"); future.complete(map); }, map -> { if (map.succeeded()) { next.handle(map.result()); } else { resultHandler.handle(Future.failedFuture(map.cause())); lock.release(); } }); }
synchronized void handleDataReceived(Buffer data) { checkContext(); if (paused) { if (pendingData == null) { pendingData = data.copy(); } else { pendingData.appendBuffer(data); } return; } if (pendingData != null) { data = pendingData.appendBuffer(data); } reportBytesRead(data.length()); if (dataHandler != null) { dataHandler.handle(data); } }
@Override public void addUser(Handler<String> handler, User user) { String json = new Gson().toJson(user); JsonObject document = new JsonObject(json); mongo.insert( USER, document, res -> { // TODO // Check if the username already exists if (res.succeeded()) { handler.handle(res.result()); } else { res.cause().printStackTrace(); } }); }
void handleEnd(LastHttpContent trailer) { synchronized (conn) { conn.reportBytesRead(bytesRead); bytesRead = 0; request.reportResponseEnd(this); if (paused) { hasPausedEnd = true; pausedTrailer = trailer; } else { this.trailer = trailer; trailers = new HeadersAdaptor(trailer.trailingHeaders()); if (endHandler != null) { try { endHandler.handle(null); } catch (Throwable t) { handleException(t); } } } } }
@Override public synchronized NetSocket upgradeToSsl(final Handler<Void> handler) { SslHandler sslHandler = channel.pipeline().get(SslHandler.class); if (sslHandler == null) { sslHandler = helper.createSslHandler(vertx); channel.pipeline().addFirst("ssl", sslHandler); } sslHandler .handshakeFuture() .addListener( future -> context.executeFromIO( () -> { if (future.isSuccess()) { handler.handle(null); } else { log.error(future.cause()); } })); return this; }
synchronized void handleFailure(RoutingContext context) { if (failureHandler != null) { failureHandler.handle(context); } }
synchronized void handleContext(RoutingContext context) { if (contextHandler != null) { contextHandler.handle(context); } }
void notifyHandler(Handler<Buffer> bodyHandler) { bodyHandler.handle(body()); // reset body so it can get GC'ed body = null; }
private Handler<HttpClientResponse> connectHandler(Handler<HttpClientResponse> responseHandler) { Objects.requireNonNull(responseHandler, "no null responseHandler accepted"); return resp -> { HttpClientResponse response; if (resp.statusCode() == 200) { // connect successful force the modification of the ChannelPipeline // beside this also pause the socket for now so the user has a chance to register its // dataHandler // after received the NetSocket NetSocket socket = resp.netSocket(); socket.pause(); response = new HttpClientResponse() { private boolean resumed; @Override public int statusCode() { return resp.statusCode(); } @Override public String statusMessage() { return resp.statusMessage(); } @Override public MultiMap headers() { return resp.headers(); } @Override public String getHeader(String headerName) { return resp.getHeader(headerName); } @Override public String getHeader(CharSequence headerName) { return resp.getHeader(headerName); } @Override public String getTrailer(String trailerName) { return resp.getTrailer(trailerName); } @Override public MultiMap trailers() { return resp.trailers(); } @Override public List<String> cookies() { return resp.cookies(); } @Override public HttpClientResponse bodyHandler(Handler<Buffer> bodyHandler) { resp.bodyHandler(bodyHandler); return this; } @Override public synchronized NetSocket netSocket() { if (!resumed) { resumed = true; vertx .getContext() .runOnContext( (v) -> socket .resume()); // resume the socket now as the user had the chance to // register a dataHandler } return socket; } @Override public HttpClientResponse endHandler(Handler<Void> endHandler) { resp.endHandler(endHandler); return this; } @Override public HttpClientResponse handler(Handler<Buffer> handler) { resp.handler(handler); return this; } @Override public HttpClientResponse pause() { resp.pause(); return this; } @Override public HttpClientResponse resume() { resp.resume(); return this; } @Override public HttpClientResponse exceptionHandler(Handler<Throwable> handler) { resp.exceptionHandler(handler); return this; } }; } else { response = resp; } responseHandler.handle(response); }; }
@Override public void handle(AsyncResult<DatagramSocket> event) { if (event.failed() && exceptionHandler != null) { exceptionHandler.handle(event.cause()); } }