本文整理了Java中io.vertx.core.eventbus.Message.fail()
方法的一些代码示例,展示了Message.fail()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message.fail()
方法的具体详情如下:
包路径:io.vertx.core.eventbus.Message
类名称: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
private void placeOrder(Message<JsonObject> msg) {
mongo.save("orders", msg.body(), save -> {
// error handling
if (save.failed()) {
msg.fail(500, save.cause().getMessage());
return;
}
msg.reply(new JsonObject());
});
}
代码示例来源:origin: vert-x3/vertx-examples
private void listAlbums(Message<JsonObject> msg) {
// issue a find command to mongo to fetch all documents from the "albums" collection.
mongo.find("albums", new JsonObject(), lookup -> {
// error handling
if (lookup.failed()) {
msg.fail(500, lookup.cause().getMessage());
return;
}
// now convert the list to a JsonArray because it will be easier to encode the final object as the response.
final JsonArray json = new JsonArray();
for (JsonObject o : lookup.result()) {
json.add(o);
}
msg.reply(json);
});
}
代码示例来源:origin: vert-x3/vertx-examples
@Override
public void start() throws Exception {
// A simple backend
vertx.eventBus().<JsonObject>consumer("backend", msg -> {
JsonObject json = msg.body();
switch (json.getString("op", "")) {
case "get": {
String productID = json.getString("id");
msg.reply(products.get(productID));
break;
}
case "add": {
String productID = json.getString("id");
JsonObject product = json.getJsonObject("product");
product.put("id", productID);
msg.reply(addProduct(product));
break;
}
case "list": {
JsonArray arr = new JsonArray();
products.forEach((k, v) -> arr.add(v));
msg.reply(arr);
break;
}
default: {
msg.fail(0, "operation not permitted");
}
}
});
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testReplyFailureRecipientFailure() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
EventBus eb = vertx.eventBus();
FakeEventBusMetrics metrics = FakeMetricsBase.getMetrics(eb);
AtomicReference<String> replyAddress = new AtomicReference<>();
CountDownLatch regLatch = new CountDownLatch(1);
eb.consumer("foo", msg -> {
replyAddress.set(msg.replyAddress());
msg.fail(0, "whatever");
}).completionHandler(onSuccess(v -> {
regLatch.countDown();
}));
awaitLatch(regLatch);
eb.send("foo", "bar", new DeliveryOptions(), ar -> {
assertTrue(ar.failed());
latch.countDown();
});
awaitLatch(latch);
assertEquals(Collections.singletonList(replyAddress.get()), metrics.getReplyFailureAddresses());
assertEquals(Collections.singletonList(ReplyFailure.RECIPIENT_FAILURE), metrics.getReplyFailures());
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testFailAfterPublish() {
eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> {
msg.fail(0, "a failure");
testComplete();
});
eb.publish(ADDRESS1, "whatever");
await();
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testFailAfterSend() {
eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> {
msg.fail(0, "a failure");
testComplete();
});
eb.publish(ADDRESS1, "whatever");
await();
}
代码示例来源:origin: eclipse-vertx/vert.x
@Test
public void testSendWithTimeoutRecipientFailure() {
String str = TestUtils.randomUnicodeString(1000);
String failureMsg = TestUtils.randomUnicodeString(1000);
int failureCode = 123;
eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> {
assertEquals(str, msg.body());
msg.fail(failureCode, failureMsg);
});
long timeout = 1000;
eb.send(ADDRESS1, str, new DeliveryOptions().setSendTimeout(timeout), (AsyncResult<Message<Integer>> ar) -> {
assertFalse(ar.succeeded());
Throwable cause = ar.cause();
assertTrue(cause instanceof ReplyException);
ReplyException re = (ReplyException) cause;
assertEquals(failureCode, re.failureCode());
assertEquals(failureMsg, re.getMessage());
assertEquals(ReplyFailure.RECIPIENT_FAILURE, re.failureType());
testComplete();
});
await();
}
代码示例来源:origin: xenv/gushici
private void getHelpFromRedis(Message message) {
redisClient.lrange(Key.REDIS_HELP_LIST, 0, -1, res -> {
if (res.succeeded()) {
JsonArray array = res.result();
JsonArray newArray = array.stream()
.map(text -> {
String prefix = config().getString("api.url", "http://localhost/");
return new JsonObject((String) text).stream()
.collect(Collectors.toMap(Map.Entry::getKey,
v -> prefix + v.getValue().toString().replace(":", "/")));
})
.collect(JsonCollector.toJsonArray());
message.reply(newArray);
} else {
log.error("Fail to get data from Redis", res.cause());
message.fail(500, res.cause().getMessage());
}
});
}
代码示例来源:origin: xenv/gushici
private void getHistoryFromRedis(Message<JsonObject> message) {
Future<String> total = Future.future(f -> redisClient.hget(Key.REDIS_CLICKS_TOTAL_HASH, "total", f));
// 7天的历史点击量
LocalDate localDate = LocalDate.now();
List<String> keys = new ArrayList<>();
for (int i = 0; i < 7; i++) {
keys.add(localDate.toString());
localDate = localDate.minusDays(1);
}
Future<JsonArray> history = Future.future(f -> redisClient.hmget(Key.REDIS_CLICKS_HISTORY_HASH, keys, f));
CompositeFuture.all(Arrays.asList(total, history)).setHandler(v -> {
if (v.succeeded()) {
JsonObject result = new JsonObject();
result.put("总点击量", total.result());
result.put("最近七天点击量", history.result());
message.reply(result);
} else {
log.error(v.cause());
message.fail(500, v.cause().getMessage());
}
});
}
代码示例来源:origin: xenv/gushici
/**
* @param message example: {format: "png", categories: [shenghuo, buyi]}
*/
private void getGushiciFromRedis(Message<JsonObject> message) {
JsonArray realCategory = new JsonArray()
.add("png".equals(message.body().getString("format")) ? "img" : "json")
.addAll(message.body().getJsonArray("categories"));
checkAndGetKey(realCategory)
.compose(key -> Future.<String>future(s -> redisClient.srandmember(key, s))) // 从 set 随机返回一个对象
.setHandler(res -> {
if (res.succeeded()) {
message.reply(res.result());
} else {
if (res.cause() instanceof ReplyException) {
ReplyException exception = (ReplyException) res.cause();
message.fail(exception.failureCode(), exception.getMessage());
}
message.fail(500, res.cause().getMessage());
}
});
}
代码示例来源:origin: advantageous/qbit
message.fail(500, errors.toString());
return;
callbackBuilder.setOnError(throwable -> {
logger.error("Error from calling " + address, throwable);
message.fail(500, throwable.getMessage());
});
callbackBuilder.setCallback(returnedValue -> message.reply(encodeOutput(returnedValue)));
callbackBuilder.setOnTimeout(() -> {
logger.error("Timed out call to " + address + " method " + method);
message.fail(408, "Timed out call to " + address + " method " + method);
});
message.fail(500, "IllegalArgumentException");
} catch (Exception ex) {
logger.error("Error marshaling message body to method call to service", ex);
message.fail(500, ex.getClass().getSimpleName() + ": " + ex.getMessage());
代码示例来源:origin: amoAHCP/vxms
@Override
protected void fail(String result, int statuscode) {
if (result != null) {
message.fail(statuscode, result);
}
}
代码示例来源:origin: io.vertx/vertx-core
@Test
public void testReplyFailureRecipientFailure() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
EventBus eb = vertx.eventBus();
FakeEventBusMetrics metrics = FakeMetricsBase.getMetrics(eb);
AtomicReference<String> replyAddress = new AtomicReference<>();
CountDownLatch regLatch = new CountDownLatch(1);
eb.consumer("foo", msg -> {
replyAddress.set(msg.replyAddress());
msg.fail(0, "whatever");
}).completionHandler(onSuccess(v -> {
regLatch.countDown();
}));
awaitLatch(regLatch);
eb.send("foo", "bar", new DeliveryOptions(), ar -> {
assertTrue(ar.failed());
latch.countDown();
});
awaitLatch(latch);
assertEquals(Collections.singletonList(replyAddress.get()), metrics.getReplyFailureAddresses());
assertEquals(Collections.singletonList(ReplyFailure.RECIPIENT_FAILURE), metrics.getReplyFailures());
}
代码示例来源:origin: io.vertx/vertx-core
@Test
public void testFailAfterSend() {
eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> {
msg.fail(0, "a failure");
testComplete();
});
eb.publish(ADDRESS1, "whatever");
await();
}
代码示例来源:origin: io.vertx/vertx-core
@Test
public void testFailAfterPublish() {
eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> {
msg.fail(0, "a failure");
testComplete();
});
eb.publish(ADDRESS1, "whatever");
await();
}
代码示例来源:origin: io.vertx/vertx-mysql-postgresql-service
private <T> Handler<AsyncResult<List<T>>> createListHandler(Message msg) {
return res -> {
if (res.failed()) {
msg.fail(-1, res.cause().getMessage());
} else {
msg.reply(new JsonArray(res.result()));
}
};
}
private <T> Handler<AsyncResult<Set<T>>> createSetHandler(Message msg) {
代码示例来源:origin: de.braintags/vertx-key-generator
@Override
public void generateKey(Message<?> message) {
String key = (String) message.body();
if (key == null || key.hashCode() == 0) {
message.fail(-1, "no keyname sent!");
}
generateKey(key, message);
}
代码示例来源:origin: io.vertx/vertx-core
@Test
public void testSendWithTimeoutRecipientFailure() {
String str = TestUtils.randomUnicodeString(1000);
String failureMsg = TestUtils.randomUnicodeString(1000);
int failureCode = 123;
eb.<String>consumer(ADDRESS1).handler((Message<String> msg) -> {
assertEquals(str, msg.body());
msg.fail(failureCode, failureMsg);
});
long timeout = 1000;
eb.send(ADDRESS1, str, new DeliveryOptions().setSendTimeout(timeout), (AsyncResult<Message<Integer>> ar) -> {
assertFalse(ar.succeeded());
Throwable cause = ar.cause();
assertTrue(cause instanceof ReplyException);
ReplyException re = (ReplyException) cause;
assertEquals(failureCode, re.failureCode());
assertEquals(failureMsg, re.getMessage());
assertEquals(ReplyFailure.RECIPIENT_FAILURE, re.failureType());
testComplete();
});
await();
}
代码示例来源:origin: de.braintags/vertx-key-generator
@Override
public void generateKey(Message<?> message) {
String key = (String) message.body();
if (key == null || key.hashCode() == 0) {
message.fail(-1, "no keyname sent!");
}
message.reply(generateKey(key));
}
代码示例来源:origin: Azure/azure-iot-sdk-java
private void manageError(Message<JsonObject> message, Throwable cause, String serviceName) {
int code = MainApiException.INTERNAL_SERVER_ERROR.getStatusCode();
String statusMessage = MainApiException.INTERNAL_SERVER_ERROR.getStatusMessage();
if (cause instanceof MainApiException) {
code = ((MainApiException)cause).getStatusCode();
statusMessage = ((MainApiException)cause).getStatusMessage();
} else {
logUnexpectedError(serviceName, cause);
}
message.fail(code, statusMessage);
}
内容来源于网络,如有侵权,请联系作者删除!