@Override public void start() throws Exception { System.out.println("StatsProducerApp start()"); vertx.setPeriodic(2000, x -> { int stats = ThreadLocalRandom.current().nextInt(1, 10); System.out.println("Sending data to 'stats'"); vertx.eventBus().send("stats", stats); }); }
@Override public void start() throws Exception { System.out.println("DisplayStatsApp start()"); vertx.setPeriodic(2000, x -> vertx.sharedData().getCounter("globalStatsCounter", ar -> { if (ar.succeeded()) { Counter counter = ar.result(); counter.get(stats -> System.out.println("Global stats counter value is " + stats.result())); } else { System.out.println(ar.cause()); } })); }
@Override public void start() throws Exception { vertx.setPeriodic(3000, x -> { System.out.println("Sending data to 'news'"); vertx.eventBus().send("news", "hello vert.x"); }); }
@Override public void start() throws Exception { EventBus eb = vertx.eventBus(); // Send a message every second vertx.setPeriodic(1000, v -> eb.publish("news-feed", "Some news!")); } }
@Override public void start() throws Exception { EventBus eb = vertx.eventBus(); // Send a message every second vertx.setPeriodic(1000, v -> { eb.send("ping-address", "ping!", reply -> { if (reply.succeeded()) { System.out.println("Received reply " + reply.result().body()); } else { System.out.println("No reply"); } }); }); } }
@Override public void start() throws Exception { EventBus eb = vertx.eventBus(); // Send a message every second vertx.setPeriodic(1000, v -> { eb.send("ping-address", "ping!", reply -> { if (reply.succeeded()) { System.out.println("Received reply " + reply.result().body()); } else { System.out.println("No reply"); } }); }); } }
@Override public void start() throws Exception { EventBus eb = vertx.eventBus(); // Send a message every second vertx.setPeriodic(1000, v -> eb.publish("news-feed", "Some news!")); } }
@Override public void start() throws Exception { EventBus eb = vertx.eventBus(); // Send a message every second vertx.setPeriodic(1000, v -> eb.publish("news-feed", "Some news!")); } }
private void periodic(long delay) throws Exception { final int numFires = 10; final AtomicLong id = new AtomicLong(-1); id.set(vertx.setPeriodic(delay, new Handler<Long>() { int count; public void handle(Long timerID) { assertEquals(id.get(), timerID.longValue()); count++; if (count == numFires) { vertx.cancelTimer(timerID); setEndTimer(); } if (count > numFires) { fail("Fired too many times"); } } })); await(); }
@Override public void start() throws Exception { systemMBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class); // A random identifier String pid = UUID.randomUUID().toString(); // Get the kafka producer config JsonObject config = config(); // Create the producer producer = KafkaWriteStream.create(vertx, config.getMap(), String.class, JsonObject.class); // Publish the metircs in Kafka vertx.setPeriodic(1000, id -> { JsonObject metrics = new JsonObject(); metrics.put("CPU", systemMBean.getProcessCpuLoad()); metrics.put("Mem", systemMBean.getTotalPhysicalMemorySize() - systemMBean.getFreePhysicalMemorySize()); producer.write(new ProducerRecord<>("the_topic", new JsonObject().put(pid, metrics))); }); }
@Override public void start() throws Exception { vertx.setPeriodic(1000, (l) -> { vertx.createHttpClient().getNow(8080, "localhost", "/", resp -> { resp.bodyHandler(body -> { System.out.println(body.toString("ISO-8859-1")); }); }); } ); } }
@Override public void streamingOutputCall( Messages.StreamingOutputCallRequest request, GrpcWriteStream<Messages.StreamingOutputCallResponse> response ) { final AtomicInteger counter = new AtomicInteger(); vertx.setPeriodic(1000L, t -> { response.write(Messages.StreamingOutputCallResponse.newBuilder().setPayload( Messages.Payload.newBuilder() .setTypeValue(PayloadType.COMPRESSABLE.getNumber()) .setBody(ByteString.copyFrom( String.valueOf(counter.incrementAndGet()), Charset.forName("UTF-8"))) ).build()); }); } };
@Override public void start() throws Exception { Router router = Router.router(vertx); // Allow events for the designated addresses in/out of the event bus bridge BridgeOptions opts = new BridgeOptions() .addOutboundPermitted(new PermittedOptions().setAddress("feed")); // Create the event bus bridge and add it to the router. SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts); router.route("/eventbus/*").handler(ebHandler); // Start the web server and tell it to use the router to handle requests. vertx.createHttpServer().requestHandler(router).listen(8080); EventBus eb = vertx.eventBus(); vertx.setPeriodic(1000l, t -> { // Create a timestamp string String timestamp = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(Date.from(Instant.now())); eb.send("feed", new JsonObject().put("now", timestamp)); }); } }
@Test public void testClientRequestExceptionHandlerCalledWhenExceptionOnDrainHandler() { pausingServer(resumeFuture -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler()); req.setChunked(true); assertFalse(req.writeQueueFull()); req.setWriteQueueMaxSize(1000); Buffer buff = TestUtils.randomBuffer(10000); AtomicBoolean failed = new AtomicBoolean(); vertx.setPeriodic(1, id -> { req.write(buff); if (req.writeQueueFull()) { vertx.cancelTimer(id); req.drainHandler(v -> { throw new RuntimeException("error"); }) .exceptionHandler(t -> { // Called a second times when testComplete is called and close the http client if (failed.compareAndSet(false, true)) { testComplete(); } }); // Tell the server to resume resumeFuture.complete(); } }); }); await(); }
@Test public void testRequestTimeoutExtendedWhenResponseChunksReceived() { long timeout = 2000; int numChunks = 100; AtomicInteger count = new AtomicInteger(0); long interval = timeout * 2 / numChunks; server.requestHandler(req -> { req.response().setChunked(true); vertx.setPeriodic(interval, timerID -> { req.response().write("foo"); if (count.incrementAndGet() == numChunks) { req.response().end(); vertx.cancelTimer(timerID); } }); }); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(200, resp.statusCode()); resp.endHandler(v -> testComplete()); })); req.exceptionHandler(t -> fail("Should not be called")); req.setTimeout(timeout); req.end(); })); await(); }
@Test public void testIdleTimeoutInfiniteSkipOfControlCharactersState() throws Exception { server.close(); server = vertx.createHttpServer(new HttpServerOptions() .setPort(DEFAULT_HTTP_PORT) .setHost(DEFAULT_HTTP_HOST) .setIdleTimeout(1)); server.requestHandler(req -> { testComplete(); }); startServer(); NetClient client = vertx.createNetClient(); client.connect(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(so -> { long id = vertx.setPeriodic(1, timerID -> { so.write(Buffer.buffer().setInt(0, 0xD)); }); so.closeHandler(v -> { vertx.cancelTimer(id); testComplete(); }); })); await(); }
private void drainingServer(Consumer<Future<Void>> consumer) { Future<Void> resumeFuture = Future.future(); server.requestHandler(req -> { req.response().setChunked(true); assertFalse(req.response().writeQueueFull()); req.response().setWriteQueueMaxSize(1000); Buffer buff = TestUtils.randomBuffer(10000); //Send data until the buffer is full vertx.setPeriodic(1, id -> { req.response().write(buff); if (req.response().writeQueueFull()) { vertx.cancelTimer(id); req.response().drainHandler(v -> { assertFalse(req.response().writeQueueFull()); testComplete(); }); // Tell the client to resume resumeFuture.complete(); } }); }); server.listen(onSuccess(s -> consumer.accept(resumeFuture))); }
@Test public void testClientDrainHandler() { pausingServer(resumeFuture -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler()); req.setChunked(true); assertFalse(req.writeQueueFull()); req.setWriteQueueMaxSize(1000); Buffer buff = TestUtils.randomBuffer(10000); vertx.setPeriodic(1, id -> { req.write(buff); if (req.writeQueueFull()) { vertx.cancelTimer(id); req.drainHandler(v -> { assertFalse(req.writeQueueFull()); testComplete(); }); // Tell the server to resume resumeFuture.complete(); } }); }); await(); }
void drainingServer(Handler<AsyncResult<NetServer>> listenHandler) { server.connectHandler(sock -> { assertFalse(sock.writeQueueFull()); sock.setWriteQueueMaxSize(1000); Buffer buff = TestUtils.randomBuffer(10000); //Send data until the buffer is full vertx.setPeriodic(1, id -> { sock.write(buff.copy()); if (sock.writeQueueFull()) { vertx.cancelTimer(id); sock.drainHandler(v -> { assertFalse(sock.writeQueueFull()); // End test after a short delay to give the client some time to read the data vertx.setTimer(100, id2 -> testComplete()); }); // Tell the client to resume vertx.eventBus().send("client_resume", ""); } }); }).listen(testAddress, listenHandler); }
@Test public void testClientDrainHandler() { pausingServer((s) -> { client.connect(testAddress, onSuccess(sock -> { assertFalse(sock.writeQueueFull()); sock.setWriteQueueMaxSize(1000); Buffer buff = TestUtils.randomBuffer(10000); vertx.setPeriodic(1, id -> { sock.write(buff.copy()); if (sock.writeQueueFull()) { vertx.cancelTimer(id); sock.drainHandler(v -> { assertFalse(sock.writeQueueFull()); testComplete(); }); // Tell the server to resume vertx.eventBus().send("server_resume", ""); } }); })); }); await(); }