Java Code Examples for io.vertx.core.eventbus.MessageConsumer#unregister()

The following examples show how to use io.vertx.core.eventbus.MessageConsumer#unregister() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VertxEventChannel.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public void unregisterMonitor(Set<Class> types, EventMonitor monitor) {
    try {
        for (Class type : types) {
            EventManager manager = managers.get(type);
            if (manager != null) {
                manager.detachMonitor(monitor);
                if (manager.getSize() == 0) {
                    managers.remove(type);
                    MessageConsumer<byte[]> consumer = consumers.remove(type);
                    CountDownLatch latch = new CountDownLatch(1);
                    consumer.unregister((unregister) -> {
                        latch.countDown();
                    });
                    latch.await();
                }
            }
        }
    } catch (Exception exception) {
        throw new RuntimeException(exception);
    }
}
 
Example 2
Source File: VertxRecorder.java    From quarkus with Apache License 2.0 6 votes vote down vote up
void unregisterMessageConsumers() {
    CountDownLatch latch = new CountDownLatch(messageConsumers.size());
    for (MessageConsumer<?> messageConsumer : messageConsumers) {
        messageConsumer.unregister(ar -> {
            if (ar.succeeded()) {
                latch.countDown();
            }
        });
    }
    try {
        latch.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new IllegalStateException("Unable to unregister all message consumer methods", e);
    }
    messageConsumers.clear();
}
 
Example 3
Source File: EventBusBridge.java    From vertx-stomp with Apache License 2.0 6 votes vote down vote up
/**
 * Handles a un-subscription request to the current {@link Destination}.
 *
 * @param connection the connection
 * @param frame      the {@code UNSUBSCRIBE} frame
 * @return {@code true} if the un-subscription has been handled, {@code false} otherwise.
 */
@Override
public synchronized boolean unsubscribe(StompServerConnection connection, Frame frame) {
  for (Subscription subscription : new ArrayList<>(subscriptions)) {
    if (subscription.connection.equals(connection)
        && subscription.id.equals(frame.getId())) {

      boolean r = subscriptions.remove(subscription);
      Optional<Subscription> any = subscriptions.stream().filter(s -> s.destination.equals(subscription.destination)).findAny();
      // We unregister the event bus consumer if there are no subscription on this address anymore.
      if (!any.isPresent()) {
        MessageConsumer<?> consumer = registry.remove(subscription.destination);
        if (consumer != null) {
          consumer.unregister();
        }
      }
      return r;
    }
  }
  return false;
}
 
Example 4
Source File: VertxEventBusMetricsTest.java    From vertx-micrometer-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDiscardMessages(TestContext context) {
  vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(new MicrometerMetricsOptions()
    .setPrometheusOptions(new VertxPrometheusOptions().setEnabled(true))
    .setEnabled(true)))
    .exceptionHandler(t -> context.exceptionHandler().handle(t));

  int num = 10;
  EventBus eb = vertx.eventBus();
  MessageConsumer<Object> consumer = eb.consumer("foo");
  consumer.setMaxBufferedMessages(num);
  consumer.pause();
  consumer.handler(msg -> fail("should not be called"));
  for (int i = 0; i < num; i++) {
    eb.send("foo", "the_message-" + i);
  }
  eb.send("foo", "last");

  waitForValue(vertx, context, "vertx.eventbus.discarded[side=local]$COUNT", value -> value.intValue() == 1);
  List<RegistryInspector.Datapoint> datapoints = listDatapoints(startsWith("vertx.eventbus"));
  assertThat(datapoints).contains(dp("vertx.eventbus.pending[side=local]$VALUE", 10));

  // Unregister => discard all remaining
  consumer.unregister();
  waitForValue(vertx, context, "vertx.eventbus.discarded[side=local]$COUNT", value -> value.intValue() == 11);
  datapoints = listDatapoints(startsWith("vertx.eventbus"));
  assertThat(datapoints).contains(dp("vertx.eventbus.pending[side=local]$VALUE", 0));
}
 
Example 5
Source File: SchemaMessageConsumers.java    From vertx-graphql-service-discovery with Apache License 2.0 5 votes vote down vote up
public void close() {
    for (Iterator<Map.Entry<String,
            MessageConsumer<JsonObject>>> it = messageConsumers.entrySet().iterator(); it.hasNext();) {
        MessageConsumer consumer = it.next().getValue();
        if (consumer.isRegistered()) {
            consumer.unregister();
        }
        it.remove();
    }
    consumerRegistrations.clear();
}
 
Example 6
Source File: EventBusTest.java    From vertx-unit with Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void testEndToEnd() {
  String testSuiteName = TestUtils.randomAlphaString(10);
  String testCaseName1 = TestUtils.randomAlphaString(10);
  String testCaseName2 = TestUtils.randomAlphaString(10);
  TestReporter testReporter = new TestReporter();
  EventBusCollector collector = EventBusCollector.create(vertx, testReporter);
  MessageConsumer<JsonObject> consumer = vertx.eventBus().consumer("the-address", collector.asMessageHandler());
  consumer.completionHandler(ar -> {
    assertTrue(ar.succeeded());
    TestSuite suite = TestSuite.create(testSuiteName).
        test(testCaseName1, context -> {
        }).test(testCaseName2, context -> context.fail("the_failure"));
    suite.run(vertx, new TestOptions().addReporter(new ReportOptions().setTo("bus:the-address")));
  });
  testReporter.await();
  assertEquals(0, testReporter.exceptions.size());
  assertEquals(2, testReporter.results.size());
  TestResult result1 = testReporter.results.get(0);
  assertEquals(testCaseName1, result1.name());
  assertTrue(result1.succeeded());
  TestResult result2 = testReporter.results.get(1);
  assertEquals(testCaseName2, result2.name());
  assertTrue(result2.failed());
  assertEquals("the_failure", result2.failure().message());
  consumer.unregister();
}
 
Example 7
Source File: EventBusTest.java    From vertx-unit with Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void testEndToEndAfterFailure() {
  long now = System.currentTimeMillis();
  String address = TestUtils.randomAlphaString(10);
  String testSuiteName = TestUtils.randomAlphaString(10);
  String testCaseName = TestUtils.randomAlphaString(10);
  TestReporter testReporter = new TestReporter();
  MessageConsumer<JsonObject> consumer = vertx.eventBus().consumer(address, EventBusCollector.create(vertx, testReporter).asMessageHandler());
  RuntimeException error = new RuntimeException("the_runtime_exception");
  consumer.completionHandler(ar -> {
    assertTrue(ar.succeeded());
    TestSuite suite = TestSuite.create(testSuiteName).
        test(testCaseName, context -> {
          try {
            Thread.sleep(10);
          } catch (InterruptedException ignore) {
          }
        }).after(context -> {
      throw error;
    });
    suite.run(vertx, new TestOptions().addReporter(new ReportOptions().setTo("bus:" + address)));
  });
  testReporter.await();
  assertEquals(1, testReporter.results.size());
  TestResult result1 = testReporter.results.get(0);
  assertEquals(testCaseName, result1.name());
  assertTrue(result1.succeeded());
  assertTrue(result1.beginTime() >= now);
  assertTrue(result1.durationTime() >= 10);
  assertEquals(1, testReporter.exceptions.size());
  Throwable cause = testReporter.exceptions.get(0);
  assertTrue(cause instanceof RuntimeException);
  assertEquals(error.getMessage(), cause.getMessage());
  assertTrue(Arrays.equals(error.getStackTrace(), cause.getStackTrace()));
  consumer.unregister();
}
 
Example 8
Source File: ProxyHelper.java    From vertx-service-proxy with Apache License 2.0 5 votes vote down vote up
/**
 * Unregisters a published service.
 *
 * @param consumer the consumer returned by {@link #registerService(Class, Vertx, Object, String)}.
 */
public static void unregisterService(MessageConsumer<JsonObject> consumer) {
  Objects.requireNonNull(consumer);
  if (consumer instanceof ProxyHandler) {
    ((ProxyHandler) consumer).close();
  } else {
    // Fall back to plain unregister.
    consumer.unregister();
  }
}
 
Example 9
Source File: ServiceBinder.java    From vertx-service-proxy with Apache License 2.0 5 votes vote down vote up
/**
 * Unregisters a published service.
 *
 * @param consumer the consumer returned by {@link #register(Class, Object)}.
 */
public void unregister(MessageConsumer<JsonObject> consumer) {
  Objects.requireNonNull(consumer);

  if (consumer instanceof ProxyHandler) {
    ((ProxyHandler) consumer).close();
  } else {
    // Fall back to plain unregister.
    consumer.unregister();
  }
}
 
Example 10
Source File: ModuleEventManager.java    From kyoko with MIT License 4 votes vote down vote up
@Override
public void unregisterEventHandler(MessageConsumer listener) {
    listener.unregister();
    consumers.remove(listener);
}