@Override public void start() throws Exception { EventBus eb = vertx.eventBus(); eb.consumer("news-feed", message -> System.out.println("Received news on consumer 1: " + message.body())); eb.consumer("news-feed", message -> System.out.println("Received news on consumer 2: " + message.body())); eb.consumer("news-feed", message -> System.out.println("Received news on consumer 3: " + message.body())); System.out.println("Ready!"); } }
@Suspendable @Override public void start() throws Exception { EventBus eb = vertx.eventBus(); eb.consumer(ADDRESS).handler(msg -> msg.reply("pong")); // This runs on an event loop but the event loop is at no time blocked! for (int i = 0; i < 10; i++) { System.out.println("Thread is " + Thread.currentThread()); Message<String> reply = awaitResult(h -> eb.send(ADDRESS, "ping", h)); System.out.println("got reply: " + reply.body()); // Like Thread.sleep but doesn't block the OS thread Strand.sleep(1000); } }
@Test public void testExceptionInInboundInterceptor() { AtomicInteger cnt = new AtomicInteger(); Handler<DeliveryContext<Object>> eb1 = dc -> { cnt.incrementAndGet(); vertx.runOnContext(v -> dc.next()); throw new RuntimeException("foo"); }; Handler<DeliveryContext<Object>> eb2 = dc -> { cnt.incrementAndGet(); dc.next(); }; eb.addInboundInterceptor(eb1).addInboundInterceptor(eb2); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); assertEquals(2, cnt.get()); testComplete(); }); eb.send("some-address", "armadillo"); await(); }
@Test public void testReplyFailureTimeout1() throws Exception { CountDownLatch latch = new CountDownLatch(1); EventBus eb = vertx.eventBus(); FakeEventBusMetrics metrics = FakeMetricsBase.getMetrics(eb); eb.consumer(ADDRESS1, msg -> { // Do not reply }); eb.send(ADDRESS1, "bar", new DeliveryOptions().setSendTimeout(10), ar -> { assertTrue(ar.failed()); latch.countDown(); }); awaitLatch(latch); assertEquals(1, metrics.getReplyFailureAddresses().size()); assertEquals(Collections.singletonList(ReplyFailure.TIMEOUT), metrics.getReplyFailures()); }
@Test public void testPublishSameHandlerRegisteredTwice() { String str = TestUtils.randomUnicodeString(1000); AtomicInteger count = new AtomicInteger(); Handler<Message<String>> handler = (Message<String> msg) -> { assertEquals(str, msg.body()); if (count.incrementAndGet() == 2) { testComplete(); } }; eb.<String>consumer(ADDRESS1).handler(handler); eb.<String>consumer(ADDRESS1).handler(handler); eb.publish(ADDRESS1, str); await(); }
@Test public void testExceptionInOutboundInterceptor() { AtomicInteger cnt = new AtomicInteger(); Handler<DeliveryContext<Object>> eb1 = sc -> { cnt.incrementAndGet(); vertx.runOnContext(v -> sc.next()); throw new RuntimeException("foo"); }; Handler<DeliveryContext<Object>> eb2 = sc -> { cnt.incrementAndGet(); sc.next(); }; eb.addOutboundInterceptor(eb1).addOutboundInterceptor(eb2); eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); assertEquals(2, cnt.get()); testComplete(); }); eb.send("some-address", "armadillo"); await(); }
@Test public void testHandlerProcessMessageFailure() throws Exception { FakeEventBusMetrics metrics = FakeMetricsBase.getMetrics(vertx.eventBus()); MessageConsumer<Object> consumer = vertx.eventBus().consumer(ADDRESS1, msg -> { assertEquals(1, metrics.getReceivedMessages().size()); HandlerMetric registration = metrics.getRegistrations().get(0); assertEquals(1, registration.scheduleCount.get()); assertEquals(1, registration.beginCount.get()); assertEquals(0, registration.endCount.get()); assertEquals(0, registration.failureCount.get()); throw new RuntimeException(); }); CountDownLatch latch = new CountDownLatch(1); consumer.completionHandler(ar -> { assertTrue(ar.succeeded()); latch.countDown(); }); awaitLatch(latch); vertx.eventBus().send(ADDRESS1, "ping"); assertEquals(1, metrics.getReceivedMessages().size()); HandlerMetric registration = metrics.getRegistrations().get(0); long now = System.currentTimeMillis(); while (registration.failureCount.get() < 1 && (System.currentTimeMillis() - now) < 10 * 1000) { Thread.sleep(10); } assertEquals(1, registration.scheduleCount.get()); assertEquals(1, registration.beginCount.get()); assertEquals(1, registration.endCount.get()); assertEquals(1, registration.failureCount.get()); }
@Test public void testPublishMultipleHandlersDifferentAddresses() { String str = TestUtils.randomUnicodeString(1000); eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> { assertEquals(str, msg.body()); testComplete(); }); eb.<String>consumer(ADDRESS2).handler((Message<String> msg) -> { fail("Should not receive message"); }); eb.publish(ADDRESS1, str); await(); }
@Test public void testMultipleOutboundInterceptors() { AtomicInteger cnt = new AtomicInteger(); int interceptorNum = 10; for (int i = 0; i < interceptorNum; i++) { final int expectedCount = i; eb.addOutboundInterceptor(sc -> { assertEquals("armadillo", sc.message().body()); int count = cnt.getAndIncrement(); assertEquals(expectedCount, count); sc.next(); }); } eb.consumer("some-address", msg -> { assertEquals("armadillo", msg.body()); assertEquals(interceptorNum, cnt.get()); testComplete(); }); eb.send("some-address", "armadillo"); await(); }