@Test public void testPeriodicPauseResume() throws Exception { ReadStream<Long> timer = vertx.periodicStream(200); AtomicInteger count = new AtomicInteger(); timer.handler(id -> { int cnt = count.incrementAndGet(); if (cnt == 2) { timer.pause(); vertx.setTimer(500, id2 -> { assertEquals(2, count.get()); timer.resume(); }); } else if (cnt == 3) { testComplete(); } }); await(); }
@Test public void testPeriodicStreamHandler() throws Exception { TimeoutStream timer = vertx.periodicStream(10); AtomicInteger count = new AtomicInteger(); timer.handler(l -> { int value = count.incrementAndGet(); switch (value) { case 0: break; case 1: throw new RuntimeException(); case 2: timer.cancel(); testComplete(); break; default: fail(); } }); timer.endHandler(v -> { fail(); }); await(); }
@Test public void testPeriodicSetHandlerTwice() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.periodicStream(200); timer.handler(l -> testComplete()); try { timer.handler(l -> fail()); fail(); } catch (IllegalStateException ignore) { } }); await(); }
private void start() { /* Flush. */ vertx.periodicStream(this.flushIntervalMS).handler(event -> flush()); /* Register the service addresses */ addressesToBridge.stream().forEach(address -> { /* register the consumer per service. */ logger.debug("Registering address {}", address); final MessageConsumer<String> consumer = vertxEventBus.consumer(address); consumer.handler(message -> handleIncomingMessage(address, message)); consumer.exceptionHandler(error -> logger.error("Error handling address " + address, error)); }); }
@Test public void testPeriodicPauseResume() throws Exception { ReadStream<Long> timer = vertx.periodicStream(200); AtomicInteger count = new AtomicInteger(); timer.handler(id -> { int cnt = count.incrementAndGet(); if (cnt == 2) { timer.pause(); vertx.setTimer(500, id2 -> { assertEquals(2, count.get()); timer.resume(); }); } else if (cnt == 3) { testComplete(); } }); await(); }
@Test public void testPeriodicStreamHandler() throws Exception { TimeoutStream timer = vertx.periodicStream(10); AtomicInteger count = new AtomicInteger(); timer.handler(l -> { int value = count.incrementAndGet(); switch (value) { case 0: break; case 1: throw new RuntimeException(); case 2: timer.cancel(); testComplete(); break; default: fail(); } }); timer.endHandler(v -> { fail(); }); await(); }
@Test public void testPeriodicSetHandlerTwice() throws Exception { vertx.runOnContext(v -> { ReadStream<Long> timer = vertx.periodicStream(200); timer.handler(l -> testComplete()); try { timer.handler(l -> fail()); fail(); } catch (IllegalStateException ignore) { } }); await(); }
/** * Returns a periodic timer as a read stream. The timer will be fired every <code>delay</code> milliseconds after * the has been called. * @param delay the delay in milliseconds, after which the timer will fire * @return the periodic stream */ public io.vertx.rxjava.core.TimeoutStream periodicStream(long delay) { io.vertx.rxjava.core.TimeoutStream ret = io.vertx.rxjava.core.TimeoutStream.newInstance(delegate.periodicStream(delay)); return ret; }
/** * Returns a periodic timer as a read stream. The timer will be fired every <code>delay</code> milliseconds after * the has been called. * @param delay the delay in milliseconds, after which the timer will fire * @return the periodic stream */ public io.vertx.rxjava.core.TimeoutStream periodicStream(long delay) { io.vertx.rxjava.core.TimeoutStream ret = io.vertx.rxjava.core.TimeoutStream.newInstance(delegate.periodicStream(delay)); return ret; }
private void start() { /* Flush. */ vertx.periodicStream(this.flushIntervalMS).handler(event -> flush()); /* Register the service addresses */ addressesToBridge.stream().forEach(address -> { /* register the consumer per service. */ logger.debug("Registering address {}", address); final MessageConsumer<String> consumer = vertxEventBus.consumer(address); consumer.handler(message -> handleIncomingMessage(address, message)); consumer.exceptionHandler(error -> logger.error("Error handling address " + address, error)); }); }
final TimeoutStream periodicStream = vertx.periodicStream(interval); periodicStream.handler(time -> { periodicStream.pause();
private void sse(HttpServerRequest request) { HttpServerResponse response = request.response(); response .putHeader("Content-Type", "text/event-stream") .putHeader("Cache-Control", "no-cache") .setChunked(true); MessageConsumer<JsonObject> consumer = vertx.eventBus().consumer("sensor.updates"); consumer.handler(msg -> { response.write("event: update\n"); response.write("data: " + msg.body().encode() + "\n\n"); }); TimeoutStream ticks = vertx.periodicStream(1000); ticks.handler(id -> { vertx.eventBus().<JsonObject>send("sensor.average", "", reply -> { if (reply.succeeded()) { response.write("event: average\n"); response.write("data: " + reply.result().body().encode() + "\n\n"); } }); }); response.endHandler(v -> { consumer.unregister(); ticks.cancel(); }); } }