io.vertx.core.eventbus.Message.fail()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(218)

本文整理了Java中io.vertx.core.eventbus.Message.fail()方法的一些代码示例,展示了Message.fail()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message.fail()方法的具体详情如下:
包路径:io.vertx.core.eventbus.Message
类名称:Message
方法名:fail

Message.fail介绍

[英]Signal to the sender that processing of this message failed.

If the message was sent specifying a result handler the handler will be called with a failure corresponding to the failure code and message specified here.
[中]向发件人发出处理此邮件失败的信号。
如果发送的消息指定了一个结果处理程序,那么将使用与此处指定的故障代码和消息对应的故障调用该处理程序。

代码示例

代码示例来源:origin: vert-x3/vertx-examples

  1. private void placeOrder(Message<JsonObject> msg) {
  2. mongo.save("orders", msg.body(), save -> {
  3. // error handling
  4. if (save.failed()) {
  5. msg.fail(500, save.cause().getMessage());
  6. return;
  7. }
  8. msg.reply(new JsonObject());
  9. });
  10. }

代码示例来源:origin: vert-x3/vertx-examples

  1. private void listAlbums(Message<JsonObject> msg) {
  2. // issue a find command to mongo to fetch all documents from the "albums" collection.
  3. mongo.find("albums", new JsonObject(), lookup -> {
  4. // error handling
  5. if (lookup.failed()) {
  6. msg.fail(500, lookup.cause().getMessage());
  7. return;
  8. }
  9. // now convert the list to a JsonArray because it will be easier to encode the final object as the response.
  10. final JsonArray json = new JsonArray();
  11. for (JsonObject o : lookup.result()) {
  12. json.add(o);
  13. }
  14. msg.reply(json);
  15. });
  16. }

代码示例来源:origin: vert-x3/vertx-examples

  1. @Override
  2. public void start() throws Exception {
  3. // A simple backend
  4. vertx.eventBus().<JsonObject>consumer("backend", msg -> {
  5. JsonObject json = msg.body();
  6. switch (json.getString("op", "")) {
  7. case "get": {
  8. String productID = json.getString("id");
  9. msg.reply(products.get(productID));
  10. break;
  11. }
  12. case "add": {
  13. String productID = json.getString("id");
  14. JsonObject product = json.getJsonObject("product");
  15. product.put("id", productID);
  16. msg.reply(addProduct(product));
  17. break;
  18. }
  19. case "list": {
  20. JsonArray arr = new JsonArray();
  21. products.forEach((k, v) -> arr.add(v));
  22. msg.reply(arr);
  23. break;
  24. }
  25. default: {
  26. msg.fail(0, "operation not permitted");
  27. }
  28. }
  29. });
  30. }

代码示例来源:origin: eclipse-vertx/vert.x

  1. @Test
  2. public void testReplyFailureRecipientFailure() throws Exception {
  3. CountDownLatch latch = new CountDownLatch(1);
  4. EventBus eb = vertx.eventBus();
  5. FakeEventBusMetrics metrics = FakeMetricsBase.getMetrics(eb);
  6. AtomicReference<String> replyAddress = new AtomicReference<>();
  7. CountDownLatch regLatch = new CountDownLatch(1);
  8. eb.consumer("foo", msg -> {
  9. replyAddress.set(msg.replyAddress());
  10. msg.fail(0, "whatever");
  11. }).completionHandler(onSuccess(v -> {
  12. regLatch.countDown();
  13. }));
  14. awaitLatch(regLatch);
  15. eb.send("foo", "bar", new DeliveryOptions(), ar -> {
  16. assertTrue(ar.failed());
  17. latch.countDown();
  18. });
  19. awaitLatch(latch);
  20. assertEquals(Collections.singletonList(replyAddress.get()), metrics.getReplyFailureAddresses());
  21. assertEquals(Collections.singletonList(ReplyFailure.RECIPIENT_FAILURE), metrics.getReplyFailures());
  22. }

代码示例来源:origin: eclipse-vertx/vert.x

  1. @Test
  2. public void testFailAfterPublish() {
  3. eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> {
  4. msg.fail(0, "a failure");
  5. testComplete();
  6. });
  7. eb.publish(ADDRESS1, "whatever");
  8. await();
  9. }

代码示例来源:origin: eclipse-vertx/vert.x

  1. @Test
  2. public void testFailAfterSend() {
  3. eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> {
  4. msg.fail(0, "a failure");
  5. testComplete();
  6. });
  7. eb.publish(ADDRESS1, "whatever");
  8. await();
  9. }

代码示例来源:origin: eclipse-vertx/vert.x

  1. @Test
  2. public void testSendWithTimeoutRecipientFailure() {
  3. String str = TestUtils.randomUnicodeString(1000);
  4. String failureMsg = TestUtils.randomUnicodeString(1000);
  5. int failureCode = 123;
  6. eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> {
  7. assertEquals(str, msg.body());
  8. msg.fail(failureCode, failureMsg);
  9. });
  10. long timeout = 1000;
  11. eb.send(ADDRESS1, str, new DeliveryOptions().setSendTimeout(timeout), (AsyncResult<Message<Integer>> ar) -> {
  12. assertFalse(ar.succeeded());
  13. Throwable cause = ar.cause();
  14. assertTrue(cause instanceof ReplyException);
  15. ReplyException re = (ReplyException) cause;
  16. assertEquals(failureCode, re.failureCode());
  17. assertEquals(failureMsg, re.getMessage());
  18. assertEquals(ReplyFailure.RECIPIENT_FAILURE, re.failureType());
  19. testComplete();
  20. });
  21. await();
  22. }

代码示例来源:origin: xenv/gushici

  1. private void getHelpFromRedis(Message message) {
  2. redisClient.lrange(Key.REDIS_HELP_LIST, 0, -1, res -> {
  3. if (res.succeeded()) {
  4. JsonArray array = res.result();
  5. JsonArray newArray = array.stream()
  6. .map(text -> {
  7. String prefix = config().getString("api.url", "http://localhost/");
  8. return new JsonObject((String) text).stream()
  9. .collect(Collectors.toMap(Map.Entry::getKey,
  10. v -> prefix + v.getValue().toString().replace(":", "/")));
  11. })
  12. .collect(JsonCollector.toJsonArray());
  13. message.reply(newArray);
  14. } else {
  15. log.error("Fail to get data from Redis", res.cause());
  16. message.fail(500, res.cause().getMessage());
  17. }
  18. });
  19. }

代码示例来源:origin: xenv/gushici

  1. private void getHistoryFromRedis(Message<JsonObject> message) {
  2. Future<String> total = Future.future(f -> redisClient.hget(Key.REDIS_CLICKS_TOTAL_HASH, "total", f));
  3. // 7天的历史点击量
  4. LocalDate localDate = LocalDate.now();
  5. List<String> keys = new ArrayList<>();
  6. for (int i = 0; i < 7; i++) {
  7. keys.add(localDate.toString());
  8. localDate = localDate.minusDays(1);
  9. }
  10. Future<JsonArray> history = Future.future(f -> redisClient.hmget(Key.REDIS_CLICKS_HISTORY_HASH, keys, f));
  11. CompositeFuture.all(Arrays.asList(total, history)).setHandler(v -> {
  12. if (v.succeeded()) {
  13. JsonObject result = new JsonObject();
  14. result.put("总点击量", total.result());
  15. result.put("最近七天点击量", history.result());
  16. message.reply(result);
  17. } else {
  18. log.error(v.cause());
  19. message.fail(500, v.cause().getMessage());
  20. }
  21. });
  22. }

代码示例来源:origin: xenv/gushici

  1. /**
  2. * @param message example: {format: "png", categories: [shenghuo, buyi]}
  3. */
  4. private void getGushiciFromRedis(Message<JsonObject> message) {
  5. JsonArray realCategory = new JsonArray()
  6. .add("png".equals(message.body().getString("format")) ? "img" : "json")
  7. .addAll(message.body().getJsonArray("categories"));
  8. checkAndGetKey(realCategory)
  9. .compose(key -> Future.<String>future(s -> redisClient.srandmember(key, s))) // 从 set 随机返回一个对象
  10. .setHandler(res -> {
  11. if (res.succeeded()) {
  12. message.reply(res.result());
  13. } else {
  14. if (res.cause() instanceof ReplyException) {
  15. ReplyException exception = (ReplyException) res.cause();
  16. message.fail(exception.failureCode(), exception.getMessage());
  17. }
  18. message.fail(500, res.cause().getMessage());
  19. }
  20. });
  21. }

代码示例来源:origin: advantageous/qbit

  1. message.fail(500, errors.toString());
  2. return;
  3. callbackBuilder.setOnError(throwable -> {
  4. logger.error("Error from calling " + address, throwable);
  5. message.fail(500, throwable.getMessage());
  6. });
  7. callbackBuilder.setCallback(returnedValue -> message.reply(encodeOutput(returnedValue)));
  8. callbackBuilder.setOnTimeout(() -> {
  9. logger.error("Timed out call to " + address + " method " + method);
  10. message.fail(408, "Timed out call to " + address + " method " + method);
  11. });
  12. message.fail(500, "IllegalArgumentException");
  13. } catch (Exception ex) {
  14. logger.error("Error marshaling message body to method call to service", ex);
  15. message.fail(500, ex.getClass().getSimpleName() + ": " + ex.getMessage());

代码示例来源:origin: amoAHCP/vxms

  1. @Override
  2. protected void fail(String result, int statuscode) {
  3. if (result != null) {
  4. message.fail(statuscode, result);
  5. }
  6. }

代码示例来源:origin: io.vertx/vertx-core

  1. @Test
  2. public void testReplyFailureRecipientFailure() throws Exception {
  3. CountDownLatch latch = new CountDownLatch(1);
  4. EventBus eb = vertx.eventBus();
  5. FakeEventBusMetrics metrics = FakeMetricsBase.getMetrics(eb);
  6. AtomicReference<String> replyAddress = new AtomicReference<>();
  7. CountDownLatch regLatch = new CountDownLatch(1);
  8. eb.consumer("foo", msg -> {
  9. replyAddress.set(msg.replyAddress());
  10. msg.fail(0, "whatever");
  11. }).completionHandler(onSuccess(v -> {
  12. regLatch.countDown();
  13. }));
  14. awaitLatch(regLatch);
  15. eb.send("foo", "bar", new DeliveryOptions(), ar -> {
  16. assertTrue(ar.failed());
  17. latch.countDown();
  18. });
  19. awaitLatch(latch);
  20. assertEquals(Collections.singletonList(replyAddress.get()), metrics.getReplyFailureAddresses());
  21. assertEquals(Collections.singletonList(ReplyFailure.RECIPIENT_FAILURE), metrics.getReplyFailures());
  22. }

代码示例来源:origin: io.vertx/vertx-core

  1. @Test
  2. public void testFailAfterSend() {
  3. eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> {
  4. msg.fail(0, "a failure");
  5. testComplete();
  6. });
  7. eb.publish(ADDRESS1, "whatever");
  8. await();
  9. }

代码示例来源:origin: io.vertx/vertx-core

  1. @Test
  2. public void testFailAfterPublish() {
  3. eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> {
  4. msg.fail(0, "a failure");
  5. testComplete();
  6. });
  7. eb.publish(ADDRESS1, "whatever");
  8. await();
  9. }

代码示例来源:origin: io.vertx/vertx-mysql-postgresql-service

  1. private <T> Handler<AsyncResult<List<T>>> createListHandler(Message msg) {
  2. return res -> {
  3. if (res.failed()) {
  4. msg.fail(-1, res.cause().getMessage());
  5. } else {
  6. msg.reply(new JsonArray(res.result()));
  7. }
  8. };
  9. }
  10. private <T> Handler<AsyncResult<Set<T>>> createSetHandler(Message msg) {

代码示例来源:origin: de.braintags/vertx-key-generator

  1. @Override
  2. public void generateKey(Message<?> message) {
  3. String key = (String) message.body();
  4. if (key == null || key.hashCode() == 0) {
  5. message.fail(-1, "no keyname sent!");
  6. }
  7. generateKey(key, message);
  8. }

代码示例来源:origin: io.vertx/vertx-core

  1. @Test
  2. public void testSendWithTimeoutRecipientFailure() {
  3. String str = TestUtils.randomUnicodeString(1000);
  4. String failureMsg = TestUtils.randomUnicodeString(1000);
  5. int failureCode = 123;
  6. eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> {
  7. assertEquals(str, msg.body());
  8. msg.fail(failureCode, failureMsg);
  9. });
  10. long timeout = 1000;
  11. eb.send(ADDRESS1, str, new DeliveryOptions().setSendTimeout(timeout), (AsyncResult<Message<Integer>> ar) -> {
  12. assertFalse(ar.succeeded());
  13. Throwable cause = ar.cause();
  14. assertTrue(cause instanceof ReplyException);
  15. ReplyException re = (ReplyException) cause;
  16. assertEquals(failureCode, re.failureCode());
  17. assertEquals(failureMsg, re.getMessage());
  18. assertEquals(ReplyFailure.RECIPIENT_FAILURE, re.failureType());
  19. testComplete();
  20. });
  21. await();
  22. }

代码示例来源:origin: de.braintags/vertx-key-generator

  1. @Override
  2. public void generateKey(Message<?> message) {
  3. String key = (String) message.body();
  4. if (key == null || key.hashCode() == 0) {
  5. message.fail(-1, "no keyname sent!");
  6. }
  7. message.reply(generateKey(key));
  8. }

代码示例来源:origin: Azure/azure-iot-sdk-java

  1. private void manageError(Message<JsonObject> message, Throwable cause, String serviceName) {
  2. int code = MainApiException.INTERNAL_SERVER_ERROR.getStatusCode();
  3. String statusMessage = MainApiException.INTERNAL_SERVER_ERROR.getStatusMessage();
  4. if (cause instanceof MainApiException) {
  5. code = ((MainApiException)cause).getStatusCode();
  6. statusMessage = ((MainApiException)cause).getStatusMessage();
  7. } else {
  8. logUnexpectedError(serviceName, cause);
  9. }
  10. message.fail(code, statusMessage);
  11. }

相关文章