Java Code Examples for io.vertx.core.eventbus.Message#reply()

The following examples show how to use io.vertx.core.eventbus.Message#reply() . 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: VerticleHelper.java    From vertx-swagger with Apache License 2.0 6 votes vote down vote up
public <T> Handler<AsyncResult<ResourceResponse<T>>> getAsyncResultHandler(Message<JsonObject> message, String serviceName, boolean withJsonEncode, TypeReference<T> type) {
    return result -> {
        if (result.succeeded()) {
            DeliveryOptions deliveryOptions = new DeliveryOptions();
            deliveryOptions.setHeaders(result.result().getHeaders());
            if(withJsonEncode) {
                message.reply(result.result().toJson(), deliveryOptions);
            } else {
                message.reply(result.result().getResponse(), deliveryOptions);
            }
        } else {
            Throwable cause = result.cause();
            manageError(message, cause, serviceName);
        }
    };
}
 
Example 2
Source File: EchoServiceVertxProxyHandler.java    From weld-vertx with Apache License 2.0 6 votes vote down vote up
private Handler<AsyncResult<Set<Character>>> createSetCharHandler(Message msg) {
  return res -> {
    if (res.failed()) {
      if (res.cause() instanceof ServiceException) {
        msg.reply(res.cause());
      } else {
        msg.reply(new ServiceException(-1, res.cause().getMessage()));
      }
    } else {
      JsonArray arr = new JsonArray();
      for (Character chr: res.result()) {
        arr.add((int) chr);
      }
      msg.reply(arr);
    }
  };
}
 
Example 3
Source File: JobServiceVertxProxyHandler.java    From vertx-kue with Apache License 2.0 6 votes vote down vote up
private Handler<AsyncResult<Set<Character>>> createSetCharHandler(Message msg) {
  return res -> {
    if (res.failed()) {
      if (res.cause() instanceof ServiceException) {
        msg.reply(res.cause());
      } else {
        msg.reply(new ServiceException(-1, res.cause().getMessage()));
      }
    } else {
      JsonArray arr = new JsonArray();
      for (Character chr : res.result()) {
        arr.add((int) chr);
      }
      msg.reply(arr);
    }
  };
}
 
Example 4
Source File: PaymentQueryServiceVertxProxyHandler.java    From vertx-blueprint-microservice with Apache License 2.0 6 votes vote down vote up
private <T> Handler<AsyncResult<T>> createHandler(Message msg) {
  return res -> {
    if (res.failed()) {
      if (res.cause() instanceof ServiceException) {
        msg.reply(res.cause());
      } else {
        msg.reply(new ServiceException(-1, res.cause().getMessage()));
      }
    } else {
      if (res.result() != null  && res.result().getClass().isEnum()) {
        msg.reply(((Enum) res.result()).name());
      } else {
        msg.reply(res.result());
      }
    }
  };
}
 
Example 5
Source File: PortfolioServiceVertxProxyHandler.java    From vertx-microservices-workshop with Apache License 2.0 6 votes vote down vote up
private Handler<AsyncResult<List<Character>>> createListCharHandler(Message msg) {
  return res -> {
    if (res.failed()) {
      if (res.cause() instanceof ServiceException) {
        msg.reply(res.cause());
      } else {
        msg.reply(new ServiceException(-1, res.cause().getMessage()));
      }
    } else {
      JsonArray arr = new JsonArray();
      for (Character chr: res.result()) {
        arr.add((int) chr);
      }
      msg.reply(arr);
    }
  };
}
 
Example 6
Source File: EchoServiceVertxProxyHandler.java    From weld-vertx with Apache License 2.0 6 votes vote down vote up
private <T> Handler<AsyncResult<T>> createHandler(Message msg) {
  return res -> {
    if (res.failed()) {
      if (res.cause() instanceof ServiceException) {
        msg.reply(res.cause());
      } else {
        msg.reply(new ServiceException(-1, res.cause().getMessage()));
      }
    } else {
      if (res.result() != null  && res.result().getClass().isEnum()) {
        msg.reply(((Enum) res.result()).name());
      } else {
        msg.reply(res.result());
      }
    }
  };
}
 
Example 7
Source File: VerticleHelper.java    From vertx-swagger with Apache License 2.0 6 votes vote down vote up
public void manageError(Message<JsonObject> message, Throwable cause, String serviceName) {
    int code = MainApiException.INTERNAL_SERVER_ERROR.getStatusCode();
    String statusMessage = MainApiException.INTERNAL_SERVER_ERROR.getStatusMessage();
    DeliveryOptions deliveryOptions = new DeliveryOptions();
    if (cause instanceof MainApiException) {
        code = ((MainApiException)cause).getStatusCode();
        statusMessage = ((MainApiException)cause).getStatusMessage();
        deliveryOptions.setHeaders(((MainApiException)cause).getHeaders());
    } else {
        logUnexpectedError(serviceName, cause);
    }
    deliveryOptions.addHeader(SwaggerRouter.CUSTOM_STATUS_CODE_HEADER_KEY, String.valueOf(code));
    deliveryOptions.addHeader(SwaggerRouter.CUSTOM_STATUS_MESSAGE_HEADER_KEY, statusMessage);

    message.reply(null, deliveryOptions);
}
 
Example 8
Source File: ShoppingCartServiceVertxProxyHandler.java    From vertx-blueprint-microservice with Apache License 2.0 6 votes vote down vote up
private <T> Handler<AsyncResult<T>> createHandler(Message msg) {
  return res -> {
    if (res.failed()) {
      if (res.cause() instanceof ServiceException) {
        msg.reply(res.cause());
      } else {
        msg.reply(new ServiceException(-1, res.cause().getMessage()));
      }
    } else {
      if (res.result() != null  && res.result().getClass().isEnum()) {
        msg.reply(((Enum) res.result()).name());
      } else {
        msg.reply(res.result());
      }
    }
  };
}
 
Example 9
Source File: DeploymentVerticle.java    From VX-API-Gateway with MIT License 6 votes vote down vote up
/**
 * 查看在线APP与API的数量
 * 
 * @param msg
 */
public void applicationCount(Message<JsonObject> msg) {
	JsonObject result = new JsonObject();
	if (applicationMaps != null) {
		result.put("app", applicationMaps.size());
	} else {
		result.put("app", 0);
	}
	if (applicationApiMaps != null) {
		int sum = applicationApiMaps.values().stream().mapToInt(Set::size).sum();
		result.put("api", sum);
	} else {
		result.put("api", 0);
	}
	msg.reply(result);
}
 
Example 10
Source File: VerticleHelper.java    From vertx-swagger with Apache License 2.0 6 votes vote down vote up
public void manageError(Message<JsonObject> message, Throwable cause, String serviceName) {
    int code = MainApiException.INTERNAL_SERVER_ERROR.getStatusCode();
    String statusMessage = MainApiException.INTERNAL_SERVER_ERROR.getStatusMessage();
    DeliveryOptions deliveryOptions = new DeliveryOptions();
    if (cause instanceof MainApiException) {
        code = ((MainApiException)cause).getStatusCode();
        statusMessage = ((MainApiException)cause).getStatusMessage();
        deliveryOptions.setHeaders(((MainApiException)cause).getHeaders());
    } else {
        logUnexpectedError(serviceName, cause);
    }
    deliveryOptions.addHeader(SwaggerRouter.CUSTOM_STATUS_CODE_HEADER_KEY, String.valueOf(code));
    deliveryOptions.addHeader(SwaggerRouter.CUSTOM_STATUS_MESSAGE_HEADER_KEY, statusMessage);

    message.reply(null, deliveryOptions);
}
 
Example 11
Source File: QueryableVertxProxyHandler.java    From vertx-graphql-service-discovery with Apache License 2.0 5 votes vote down vote up
private <T> Handler<AsyncResult<List<T>>> createListHandler(Message msg) {
  return res -> {
    if (res.failed()) {
      if (res.cause() instanceof ServiceException) {
        msg.reply(res.cause());
      } else {
        msg.reply(new ServiceException(-1, res.cause().getMessage()));
      }
    } else {
      msg.reply(new JsonArray(res.result()));
    }
  };
}
 
Example 12
Source File: OrderServiceVertxProxyHandler.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
private <T> Handler<AsyncResult<List<T>>> createListHandler(Message msg) {
  return res -> {
    if (res.failed()) {
      if (res.cause() instanceof ServiceException) {
        msg.reply(res.cause());
      } else {
        msg.reply(new ServiceException(-1, res.cause().getMessage()));
      }
    } else {
      msg.reply(new JsonArray(res.result()));
    }
  };
}
 
Example 13
Source File: ProductServiceVertxProxyHandler.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
private <T> Handler<AsyncResult<List<T>>> createListHandler(Message msg) {
  return res -> {
    if (res.failed()) {
      if (res.cause() instanceof ServiceException) {
        msg.reply(res.cause());
      } else {
        msg.reply(new ServiceException(-1, res.cause().getMessage()));
      }
    } else {
      msg.reply(new JsonArray(res.result()));
    }
  };
}
 
Example 14
Source File: ProductServiceVertxProxyHandler.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
private <T> Handler<AsyncResult<Set<T>>> createSetHandler(Message msg) {
  return res -> {
    if (res.failed()) {
      if (res.cause() instanceof ServiceException) {
        msg.reply(res.cause());
      } else {
        msg.reply(new ServiceException(-1, res.cause().getMessage()));
      }
    } else {
      msg.reply(new JsonArray(new ArrayList<>(res.result())));
    }
  };
}
 
Example 15
Source File: CheckoutServiceVertxProxyHandler.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
private <T> Handler<AsyncResult<Set<T>>> createSetHandler(Message msg) {
  return res -> {
    if (res.failed()) {
      if (res.cause() instanceof ServiceException) {
        msg.reply(res.cause());
      } else {
        msg.reply(new ServiceException(-1, res.cause().getMessage()));
      }
    } else {
      msg.reply(new JsonArray(new ArrayList<>(res.result())));
    }
  };
}
 
Example 16
Source File: AccountServiceVertxProxyHandler.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
private <T> Handler<AsyncResult<Set<T>>> createSetHandler(Message msg) {
  return res -> {
    if (res.failed()) {
      if (res.cause() instanceof ServiceException) {
        msg.reply(res.cause());
      } else {
        msg.reply(new ServiceException(-1, res.cause().getMessage()));
      }
    } else {
      msg.reply(new JsonArray(new ArrayList<>(res.result())));
    }
  };
}
 
Example 17
Source File: ProductServiceVertxProxyHandler.java    From vertx-blueprint-microservice with Apache License 2.0 4 votes vote down vote up
public void handle(Message<JsonObject> msg) {
  try {
    JsonObject json = msg.body();
    String action = msg.headers().get("action");
    if (action == null) {
      throw new IllegalStateException("action not specified");
    }
    accessed();
    switch (action) {
      case "initializePersistence": {
        service.initializePersistence(createHandler(msg));
        break;
      }
      case "addProduct": {
        service.addProduct(json.getJsonObject("product") == null ? null : new io.vertx.blueprint.microservice.product.Product(json.getJsonObject("product")), createHandler(msg));
        break;
      }
      case "retrieveProduct": {
        service.retrieveProduct((java.lang.String)json.getValue("productId"), res -> {
          if (res.failed()) {
            if (res.cause() instanceof ServiceException) {
              msg.reply(res.cause());
            } else {
              msg.reply(new ServiceException(-1, res.cause().getMessage()));
            }
          } else {
            msg.reply(res.result() == null ? null : res.result().toJson());
          }
       });
        break;
      }
      case "retrieveProductPrice": {
        service.retrieveProductPrice((java.lang.String)json.getValue("productId"), createHandler(msg));
        break;
      }
      case "retrieveAllProducts": {
        service.retrieveAllProducts(res -> {
          if (res.failed()) {
            if (res.cause() instanceof ServiceException) {
              msg.reply(res.cause());
            } else {
              msg.reply(new ServiceException(-1, res.cause().getMessage()));
            }
          } else {
            msg.reply(new JsonArray(res.result().stream().map(Product::toJson).collect(Collectors.toList())));
          }
       });
        break;
      }
      case "retrieveProductsByPage": {
        service.retrieveProductsByPage(json.getValue("page") == null ? null : (json.getLong("page").intValue()), res -> {
          if (res.failed()) {
            if (res.cause() instanceof ServiceException) {
              msg.reply(res.cause());
            } else {
              msg.reply(new ServiceException(-1, res.cause().getMessage()));
            }
          } else {
            msg.reply(new JsonArray(res.result().stream().map(Product::toJson).collect(Collectors.toList())));
          }
       });
        break;
      }
      case "deleteProduct": {
        service.deleteProduct((java.lang.String)json.getValue("productId"), createHandler(msg));
        break;
      }
      case "deleteAllProducts": {
        service.deleteAllProducts(createHandler(msg));
        break;
      }
      default: {
        throw new IllegalStateException("Invalid action: " + action);
      }
    }
  } catch (Throwable t) {
    msg.reply(new ServiceException(500, t.getMessage()));
    throw t;
  }
}
 
Example 18
Source File: CLIVerticle.java    From VX-API-Gateway with MIT License 4 votes vote down vote up
/**
 * 启动指定网关应用并启动所有的API
 * 
 * @param msg
 */
public void startAPPEverything(Message<JsonArray> msg) {
	LOG.info("cli->执行启动指定网关应用并启动所有的API...");
	JsonArray body = msg.body();
	if (body == null || body.size() < 1) {
		msg.reply("1400");
		if (LOG.isDebugEnabled()) {
			LOG.debug("cli->执行启动指定网关应用并启动所有的API-->失败:缺少参数,系统接收到信息:" + body);
		}
		return;
	}
	vertx.executeBlocking(futrue -> {
		vertx.eventBus().<JsonArray>send(thisVertxName + VxApiEventBusAddressConstant.FIND_APP, null, reply -> {
			if (reply.succeeded()) {
				JsonArray allAPP = reply.result().body();
				if (allAPP != null && allAPP.size() > 0) {
					JsonArray apps = new JsonArray();
					allAPP.forEach(va -> {
						JsonObject json = (JsonObject) va;
						String key = json.getString("appName", "");
						if (body.contains(key)) {
							apps.add(json);
						}
					});
					if (apps.size() < 1) {
						LOG.info("cli->执行启动指定网关应用并启动所有的API-->结果:没有查询到应用网关,请查看是否输入了不存在的网关应用名字或者是否存在空格或者大小写");
						futrue.complete();
					} else {
						startAppService(apps, 0, 0, true, handler -> {
							Integer success = handler.result().getInteger("success", 0);
							Integer fail = handler.result().getInteger("fail", 0);
							LOG.info("cli->执行启动指定网关应用并启动所有的API-->结果:成功数量:" + success + ",失败数量:" + fail);
							futrue.complete();
						});
					}
				} else {
					futrue.complete();
					LOG.info("cli->执行启动指定网关应用并启动所有的API-->失败:没有查询到应用网关");
				}
			} else {
				futrue.fail(reply.cause());
				LOG.error("cli->执行启动指定网关应用并启动所有的API-->失败:" + reply.cause());
			}
		});
	}, result -> {
		if (result.succeeded()) {
			msg.reply("ok");
		} else {
			msg.reply("500");
			LOG.error("cli->执行启动指定网关应用并启动所有的API-->失败:" + result.cause());
		}
	});
}
 
Example 19
Source File: SensorData.java    From vertx-in-action with MIT License 4 votes vote down vote up
private void average(Message<JsonObject> message) {
  double avg = lastValues.values().stream()
    .collect(Collectors.averagingDouble(Double::doubleValue));
  JsonObject json = new JsonObject().put("average", avg);
  message.reply(json);
}
 
Example 20
Source File: DeploymentVerticle.java    From VX-API-Gateway with MIT License 2 votes vote down vote up
/**
 * 查看APP是否在线
 * 
 * @param msg
 */
public void getAppIsOnline(Message<String> msg) {
	msg.reply(applicationMaps.get(msg.body()) != null);
}