當前位置: 首頁>>代碼示例>>Java>>正文


Java MessageConsumer.completionHandler方法代碼示例

本文整理匯總了Java中io.vertx.core.eventbus.MessageConsumer.completionHandler方法的典型用法代碼示例。如果您正苦於以下問題:Java MessageConsumer.completionHandler方法的具體用法?Java MessageConsumer.completionHandler怎麽用?Java MessageConsumer.completionHandler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在io.vertx.core.eventbus.MessageConsumer的用法示例。


在下文中一共展示了MessageConsumer.completionHandler方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: registerConsumers

import io.vertx.core.eventbus.MessageConsumer; //導入方法依賴的package包/類
public void registerConsumers(Vertx vertx, Event<Object> event) {
    CountDownLatch latch = new CountDownLatch(consumerAddresses.size());
    for (String address : consumerAddresses) {
        MessageConsumer<?> consumer = vertx.eventBus().consumer(address, VertxHandler.from(vertx, event, address));
        consumer.completionHandler(ar -> {
            if (ar.succeeded()) {
                LOGGER.debug("Sucessfully registered event consumer for {0}", address);
                latch.countDown();
            } else {
                LOGGER.error("Cannot register event consumer for {0}", ar.cause(), address);
            }
        });
    }
    Context context = this.context;
    if (context == null && vertx != null) {
        context = vertx.getOrCreateContext();
    }
    long timeout = context != null ? context.config().getLong(CONSUMER_REGISTRATION_TIMEOUT_KEY, DEFAULT_CONSUMER_REGISTRATION_TIMEOUT)
            : DEFAULT_CONSUMER_REGISTRATION_TIMEOUT;
    try {
        if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
            throw new IllegalStateException(String.format("Message consumers not registered within %s ms [registered: %s, total: %s]", timeout,
                    latch.getCount(), consumerAddresses.size()));
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException(e);
    }
}
 
開發者ID:weld,項目名稱:weld-vertx,代碼行數:30,代碼來源:VertxExtension.java

示例2: registerMessageConsumer

import io.vertx.core.eventbus.MessageConsumer; //導入方法依賴的package包/類
/**
 *
 * @param <REQ>
 * @param <RESP>
 * @param config
 * @return MessageConsumer
 */
protected <REQ extends Message, RESP extends Message> MessageConsumerRegistration<REQ, RESP> registerMessageConsumer(@NonNull final MessageConsumerConfig<REQ, RESP> config) {
    Preconditions.checkState(!messageConsumerRegistrations.containsKey(config.getAddressMessageMapping().getAddress()));
    final EventBus eventBus = vertx.eventBus();
    registerMessageCodecs(config);

    final String address = config.getAddressMessageMapping().getAddress();
    final MessageConsumer<REQ> consumer = config.isLocal() ? eventBus.localConsumer(address) : eventBus.consumer(address);
    consumer.completionHandler(config.getCompletionHandler().map(handler -> messageConsumerCompletionHandler(address, Optional.of(handler), config))
            .orElseGet(() -> messageConsumerCompletionHandler(address, Optional.empty(), config)));
    consumer.endHandler(config.getEndHandler().map(handler -> messageConsumerEndHandler(address, Optional.of(handler), config))
            .orElseGet(() -> messageConsumerEndHandler(address, Optional.empty(), config)));
    config.getExceptionHandler().ifPresent(consumer::exceptionHandler);
    consumer.handler(messageConsumerHandler(config));

    final String processSpecificAddress = config.getAddressMessageMapping().getProcessSpecificAddress();
    final MessageConsumer<REQ> processSpecificConsumer = config.isLocal() ? eventBus.localConsumer(processSpecificAddress) : eventBus.consumer(processSpecificAddress);
    processSpecificConsumer.completionHandler(config.getCompletionHandler().map(handler -> messageConsumerCompletionHandler(processSpecificAddress, Optional.of(handler), config))
            .orElseGet(() -> messageConsumerCompletionHandler(processSpecificAddress, Optional.empty(), config)));
    processSpecificConsumer.endHandler(config.getEndHandler().map(handler -> messageConsumerEndHandler(processSpecificAddress, Optional.of(handler), config))
            .orElseGet(() -> messageConsumerEndHandler(processSpecificAddress, Optional.empty(), config)));
    config.getExceptionHandler().ifPresent(processSpecificConsumer::exceptionHandler);
    processSpecificConsumer.handler(messageConsumerHandler(config));

    final MessageConsumerRegistration<REQ, RESP> messageConsumerRegistration = MessageConsumerRegistration.<REQ, RESP>builder()
            .messageConsumer(consumer)
            .processSpecificMessageConsumer(processSpecificConsumer)
            .config(config)
            .build();
    messageConsumerRegistrations = ImmutableMap.<String, MessageConsumerRegistration<?, ?>>builder().putAll(messageConsumerRegistrations).put(
            config.address(),
            messageConsumerRegistration
    ).build();
    return messageConsumerRegistration;
}
 
開發者ID:runrightfast,項目名稱:runrightfast-vertx,代碼行數:42,代碼來源:RunRightFastVerticle.java

示例3: testReportToEventBus

import io.vertx.core.eventbus.MessageConsumer; //導入方法依賴的package包/類
@org.junit.Test
public void testReportToEventBus() {
  MessageConsumer<JsonObject> consumer = vertx.eventBus().<JsonObject>consumer("the_address");
  consumer.handler(msg -> {
    if (msg.body().getString("type").equals(EventBusCollector.EVENT_TEST_SUITE_END)) {
      consumer.unregister();
      testComplete();
    }
  });
  consumer.completionHandler(ar -> {
    assertTrue(ar.succeeded());
    suite.run(vertx, new TestOptions().addReporter(new ReportOptions().setTo("bus:the_address")));
  });
  await();
}
 
開發者ID:vert-x3,項目名稱:vertx-unit,代碼行數:16,代碼來源:ReportingTest.java

示例4: testEndToEnd

import io.vertx.core.eventbus.MessageConsumer; //導入方法依賴的package包/類
@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();
}
 
開發者ID:vert-x3,項目名稱:vertx-unit,代碼行數:28,代碼來源:EventBusTest.java

示例5: testEndToEndAfterFailure

import io.vertx.core.eventbus.MessageConsumer; //導入方法依賴的package包/類
@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();
}
 
開發者ID:vert-x3,項目名稱:vertx-unit,代碼行數:37,代碼來源:EventBusTest.java

示例6: testEventBusReporterTestSuiteFailure

import io.vertx.core.eventbus.MessageConsumer; //導入方法依賴的package包/類
@org.junit.Test
public void testEventBusReporterTestSuiteFailure() throws Exception {
  String address = TestUtils.randomAlphaString(10);
  String testSuiteName = TestUtils.randomAlphaString(10);
  String testCaseName = TestUtils.randomAlphaString(10);
  AtomicInteger status = new AtomicInteger();
  MessageConsumer<JsonObject> consumer = vertx.eventBus().localConsumer(address);
  consumer.handler(msg -> {
    JsonObject body = msg.body();
    String type = body.getString("type");
    switch (status.get()) {
      case 0:
        assertEquals(EventBusCollector.EVENT_TEST_SUITE_BEGIN, type);
        assertEquals(testSuiteName, body.getString("name"));
        break;
      case 1:
        assertEquals(EventBusCollector.EVENT_TEST_CASE_BEGIN, type);
        assertEquals(testCaseName, body.getString("name"));
        break;
      case 2:
        assertEquals(EventBusCollector.EVENT_TEST_CASE_END, type);
        assertEquals(testCaseName, body.getString("name"));
        assertNotNull(body.getLong("beginTime"));
        assertNotNull(body.getLong("durationTime"));
        assertNull(testCaseName, body.getJsonObject("failure"));
        break;
      case 3:
        assertEquals(EventBusCollector.EVENT_TEST_SUITE_ERROR, type);
        JsonObject failure = body.getJsonObject("failure");
        assertNotNull(failure);
        assertEquals("the_after_failure", failure.getString("message"));
        assertNotNull(failure.getString("stackTrace"));
        assertNotNull(failure.getBinary("cause"));
        break;
      case 4:
        assertEquals(EventBusCollector.EVENT_TEST_SUITE_END, type);
        assertEquals(testSuiteName, body.getString("name"));
        assertNull(body.getLong("beginTime"));
        assertNull(body.getLong("durationTime"));
        consumer.unregister();
        testComplete();
        break;
      default:
        fail("Unexpected status " + status.get());
    }
    status.incrementAndGet();
  });
  consumer.completionHandler(ar -> {
    assertTrue(ar.succeeded());
    TestSuite.create(testSuiteName).test(testCaseName, context -> {
      // Ok
    }).after(context -> {
      throw new RuntimeException("the_after_failure");
    }).run(vertx, new TestOptions().addReporter(new ReportOptions().setTo("bus:" + address)));
  });
  await();
}
 
開發者ID:vert-x3,項目名稱:vertx-unit,代碼行數:58,代碼來源:EventBusTest.java

示例7: testEventBusReport

import io.vertx.core.eventbus.MessageConsumer; //導入方法依賴的package包/類
@org.junit.Test
public void testEventBusReport() throws Exception {
  long now = System.currentTimeMillis();
  String address = TestUtils.randomAlphaString(10);
  String testSuiteName = TestUtils.randomAlphaString(10);
  String testCaseName1 = TestUtils.randomAlphaString(10);
  String testCaseName2 = TestUtils.randomAlphaString(10);
  String testCaseName3 = TestUtils.randomAlphaString(10);
  MessageConsumer<JsonObject> consumer = vertx.eventBus().localConsumer(address);
  Handler<Message<JsonObject>> messageHandler = EventBusCollector.create(vertx, testSuite -> {
    Map<TestCaseReport, TestResult> results = new LinkedHashMap<>();
    testSuite.handler(testCase -> {
      testCase.endHandler(result -> {
        results.put(testCase, result);
      });
    });
    testSuite.endHandler(done -> {
      assertEquals(testSuiteName, testSuite.name());
      assertEquals(3, results.size());
      Iterator<Map.Entry<TestCaseReport, TestResult>> it = results.entrySet().iterator();
      Map.Entry<TestCaseReport, TestResult> entry1 = it.next();
      assertEquals(entry1.getKey().name(), entry1.getValue().name());
      assertEquals(testCaseName1, entry1.getValue().name());
      assertTrue(entry1.getValue().succeeded());
      assertTrue(entry1.getValue().beginTime() >= now);
      assertEquals(10, entry1.getValue().durationTime());
      assertNull(entry1.getValue().failure());
      Map.Entry<TestCaseReport, TestResult> entry2 = it.next();
      assertEquals(entry2.getKey().name(), entry2.getValue().name());
      assertEquals(testCaseName2, entry2.getValue().name());
      assertFalse(entry2.getValue().succeeded());
      assertTrue(entry2.getValue().beginTime() >= now);
      assertEquals(5, entry2.getValue().durationTime());
      assertNotNull(entry2.getValue().failure());
      assertEquals(false, entry2.getValue().failure().isError());
      assertEquals("the_failure_message", entry2.getValue().failure().message());
      assertEquals("the_failure_stackTrace", entry2.getValue().failure().stackTrace());
      assertTrue(entry2.getValue().failure().cause() instanceof IOException);
      Map.Entry<TestCaseReport, TestResult> entry3 = it.next();
      assertEquals(entry3.getKey().name(), entry3.getValue().name());
      assertEquals(testCaseName3, entry3.getValue().name());
      assertFalse(entry3.getValue().succeeded());
      assertTrue(entry3.getValue().beginTime() >= now);
      assertEquals(7, entry3.getValue().durationTime());
      assertNotNull(entry3.getValue().failure());
      assertEquals(false, entry3.getValue().failure().isError());
      assertEquals(null, entry3.getValue().failure().message());
      assertEquals("the_failure_stackTrace", entry3.getValue().failure().stackTrace());
      assertTrue(entry3.getValue().failure().cause() instanceof IOException);
      consumer.unregister();
      testComplete();
    });
  }).asMessageHandler();
  consumer.completionHandler(ar -> {
    assertTrue(ar.succeeded());
    vertx.eventBus().publish(address, new JsonObject().put("type", EventBusCollector.EVENT_TEST_SUITE_BEGIN).put("name", testSuiteName));
    vertx.eventBus().publish(address, new JsonObject().put("type", EventBusCollector.EVENT_TEST_CASE_BEGIN).put("name", testCaseName1));
    vertx.eventBus().publish(address, new JsonObject().put("type", EventBusCollector.EVENT_TEST_CASE_END).put("name", testCaseName1).put("beginTime", System.currentTimeMillis()).put("durationTime", 10L));
    vertx.eventBus().publish(address, new JsonObject().put("type", EventBusCollector.EVENT_TEST_CASE_BEGIN).put("name", testCaseName2));
    vertx.eventBus().publish(address, new JsonObject().put("type", EventBusCollector.EVENT_TEST_CASE_END).put("name", testCaseName2).put("beginTime", System.currentTimeMillis()).put("durationTime", 5L).
        put("failure", new FailureImpl(
            false, "the_failure_message", "the_failure_stackTrace", new IOException()).toJson()));
    vertx.eventBus().publish(address, new JsonObject().put("type", EventBusCollector.EVENT_TEST_CASE_BEGIN).put("name", testCaseName3));
    vertx.eventBus().publish(address, new JsonObject().put("type", EventBusCollector.EVENT_TEST_CASE_END).put("name", testCaseName3).put("beginTime", System.currentTimeMillis()).put("durationTime", 7L).
        put("failure", new FailureImpl(
            false, null, "the_failure_stackTrace", new IOException()).toJson()));
    vertx.eventBus().publish(address, new JsonObject().put("type", EventBusCollector.EVENT_TEST_SUITE_END));
  });
  consumer.handler(messageHandler);
  await();
}
 
開發者ID:vert-x3,項目名稱:vertx-unit,代碼行數:72,代碼來源:EventBusTest.java

示例8: testEventBusReportAfterFailure

import io.vertx.core.eventbus.MessageConsumer; //導入方法依賴的package包/類
@org.junit.Test
public void testEventBusReportAfterFailure() throws Exception {
  String address = TestUtils.randomAlphaString(10);
  String testSuiteName = TestUtils.randomAlphaString(10);
  String testCaseName = TestUtils.randomAlphaString(10);
  MessageConsumer<JsonObject> consumer = vertx.eventBus().localConsumer(address);
  LinkedList<Throwable> suiteFailures = new LinkedList<>();
  Handler<Message<JsonObject>> messageHandler = EventBusCollector.create(vertx, testSuite -> {
    Map<TestCaseReport, TestResult> results = new LinkedHashMap<>();
    testSuite.handler(testCase -> {
      testCase.endHandler(result -> {
        assertEquals(Collections.emptyList(), suiteFailures);
        results.put(testCase, result);
      });
    });
    testSuite.exceptionHandler(suiteFailures::add);
    testSuite.endHandler(done -> {
      assertEquals(testSuiteName, testSuite.name());
      assertEquals(1, results.size());
      Iterator<Map.Entry<TestCaseReport, TestResult>> it = results.entrySet().iterator();
      Map.Entry<TestCaseReport, TestResult> entry1 = it.next();
      assertEquals(entry1.getKey().name(), entry1.getValue().name());
      assertEquals(testCaseName, entry1.getValue().name());
      assertTrue(entry1.getValue().succeeded());
      assertNull(entry1.getValue().failure());
      assertEquals(1, suiteFailures.size());
      assertTrue(suiteFailures.get(0) instanceof IOException);
      consumer.unregister();
      testComplete();
    });
  }).asMessageHandler();
  consumer.completionHandler(ar -> {
    assertTrue(ar.succeeded());
    vertx.eventBus().publish(address, new JsonObject().put("type", EventBusCollector.EVENT_TEST_SUITE_BEGIN).put("name", testSuiteName));
    vertx.eventBus().publish(address, new JsonObject().put("type", EventBusCollector.EVENT_TEST_CASE_BEGIN).put("name", testCaseName));
    vertx.eventBus().publish(address, new JsonObject().put("type", EventBusCollector.EVENT_TEST_CASE_END).put("name", testCaseName));
    vertx.eventBus().publish(address, new JsonObject().put("type", EventBusCollector.EVENT_TEST_SUITE_ERROR).
        put("failure", new FailureImpl(
            false, "the_failure_message", "the_failure_stackTrace", new IOException()).toJson()));
    vertx.eventBus().publish(address, new JsonObject().put("type", EventBusCollector.EVENT_TEST_SUITE_END));
  });
  consumer.handler(messageHandler);
  await();
}
 
開發者ID:vert-x3,項目名稱:vertx-unit,代碼行數:45,代碼來源:EventBusTest.java


注:本文中的io.vertx.core.eventbus.MessageConsumer.completionHandler方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。