List of usage examples for io.vertx.core.json JsonObject encodePrettily
public String encodePrettily()
From source file:org.hawkular.apm.examples.vertx.opentracing.ordermanager.PlaceOrderHandler.java
License:Apache License
private void checkStock(RoutingContext routingContext, JsonObject order, HttpServerResponse response, Span parentSpan) {//from w w w . j a va 2s .co m Span getItemSpan = tracer.buildSpan("GetItem").asChildOf(parentSpan).start(); tracer.inject(getItemSpan.context(), Format.Builtin.TEXT_MAP, new VertxMessageInjectAdapter(order)); // Check stock routingContext.vertx().eventBus().send("InventoryManager.getItem", order, invresp -> { logger.fine("Getting inventory item"); getItemSpan.finish(); if (invresp.succeeded()) { logger.finest("Inventory response succeeded"); JsonObject item = (JsonObject) invresp.result().body(); if (order.getInteger("quantity", 1) <= item.getInteger("quantity", 1)) { logger.fine("Will put order"); // Remove internal headers - necessary because we have had // to piggyback the APM state as a top level field in the // order JSON document - which now needs to be removed before // returning it to the end user. VertxMessageInjectAdapter.cleanup(order); // Assign id order.put("id", UUID.randomUUID().toString()); orders.put(order.getString("id"), order); response.putHeader("content-type", "application/json").setStatusCode(202) .end(order.encodePrettily()); parentSpan.setTag("orderId", order.getString("id")); parentSpan.setTag("itemId", order.getString("itemId")); parentSpan.setTag("accountId", order.getString("accountId")); parentSpan.finish(); orderConfirmed(routingContext, order, parentSpan); } else { logger.info("Out of stock"); sendError(500, "Out of stock", response, parentSpan); } } else { logger.warning("Application failed"); sendError(500, invresp.cause().getMessage(), response, parentSpan); } }); }
From source file:org.hawkular.apm.examples.vertx.opentracing.OrderManager.java
License:Apache License
protected void checkStock(JsonObject order, HttpServerResponse response, Span parentSpan) { Span getItemSpan = tracer.buildSpan("GetItem").asChildOf(parentSpan).start(); tracer.inject(getItemSpan.context(), Format.Builtin.TEXT_MAP, new VertxMessageInjectAdapter(order)); // Check stock eb.send("InventoryManager.getItem", order, invresp -> { getItemSpan.finish();// www . ja v a 2 s . c o m if (invresp.succeeded()) { JsonObject item = (JsonObject) invresp.result().body(); if (order.getInteger("quantity", 1) <= item.getInteger("quantity", 1)) { // Remove internal headers - necessary because we have had // to piggyback the APM state as a top level field in the // order JSON document - which now needs to be removed before // returning it to the end user. VertxMessageInjectAdapter.cleanup(order); // Assign id order.put("id", UUID.randomUUID().toString()); orders.put(order.getString("id"), order); response.putHeader("content-type", "application/json").setStatusCode(202) .end(order.encodePrettily()); parentSpan.setTag("orderId", order.getString("id")); parentSpan.setTag("itemId", order.getString("itemId")); parentSpan.setTag("accountId", order.getString("accountId")); parentSpan.finish(); orderConfirmed(order, parentSpan); } else { sendError(500, "Out of stock", response, parentSpan); } } else { sendError(500, invresp.cause().getMessage(), response, parentSpan); } }); }
From source file:org.jberet.vertx.rest.JBeretRouterConfig.java
License:Open Source License
private static void startJob(final RoutingContext routingContext) { final String jobXmlName = routingContext.pathParam("jobXmlName"); final Properties jobParams = getJobParameters(routingContext); final JobExecutionEntity jobExecutionEntity = JobService.getInstance().start(jobXmlName, jobParams); setJobExecutionEntityHref(routingContext, jobExecutionEntity); final JsonObject jsonObject = JsonObject.mapFrom(jobExecutionEntity); sendJsonResponse(routingContext, jsonObject.encodePrettily()); }
From source file:org.jberet.vertx.rest.JBeretRouterConfig.java
License:Open Source License
private static void scheduleJob(final RoutingContext routingContext) { final String jobXmlName = routingContext.pathParam("jobXmlName"); final HttpServerRequest request = routingContext.request(); final String delayString = request.getParam("delay"); final long delay = delayString == null ? DEFAULT_SCHEDULE_DELAY : Long.parseLong(delayString); final String periodicString = request.getParam("periodic"); final boolean periodic = periodicString != null && Boolean.parseBoolean(periodicString); final Properties jobParams = getJobParameters(routingContext); final JobSchedule jobSchedule = new JobSchedule(); jobSchedule.setDelay(delay);//from ww w. ja v a2 s . co m jobSchedule.setJobName(jobXmlName); jobSchedule.setJobParameters(jobParams); final long delayMillis = delay * 60 * 1000; final long timerId; final Vertx vertx = routingContext.vertx(); if (!periodic) { timerId = vertx.setTimer(delayMillis, timerId1 -> { final JobExecutionEntity jobExecutionEntity = JobService.getInstance().start(jobXmlName, jobParams); setJobExecutionEntityHref(routingContext, jobExecutionEntity); jobSchedule.addJobExecutionIds(jobExecutionEntity.getExecutionId()); jobSchedule.setStatus(JobSchedule.Status.DONE); }); } else { timerId = vertx.setPeriodic(delayMillis, timerId1 -> { final JobExecutionEntity jobExecutionEntity = JobService.getInstance().start(jobXmlName, jobParams); setJobExecutionEntityHref(routingContext, jobExecutionEntity); jobSchedule.addJobExecutionIds(jobExecutionEntity.getExecutionId()); // since this is periodic timer, there may be more in the future // jobSchedule.setStatus(JobSchedule.Status.DONE); }); } jobSchedule.setId(timerId); final LocalMap<String, JobSchedule> timerLocalMap = getTimerLocalMap(vertx); timerLocalMap.put(String.valueOf(timerId), jobSchedule); final JsonObject jsonObject = JsonObject.mapFrom(jobSchedule); sendJsonResponse(routingContext, jsonObject.encodePrettily()); }
From source file:org.jberet.vertx.rest.JBeretRouterConfig.java
License:Open Source License
private static void restartJob(final RoutingContext routingContext) { final String jobXmlName = routingContext.pathParam("jobXmlName"); final Properties jobParams = getJobParameters(routingContext); final JobInstanceEntity[] jobInstances = JobService.getInstance().getJobInstances(jobXmlName, 0, 1); if (jobInstances.length > 0) { final long latestJobExecutionId = jobInstances[0].getLatestJobExecutionId(); final JobExecutionEntity jobExecutionEntity = JobService.getInstance().restart(latestJobExecutionId, jobParams);/* w w w . j ava 2s.c o m*/ setJobExecutionEntityHref(routingContext, jobExecutionEntity); final JsonObject jsonObject = JsonObject.mapFrom(jobExecutionEntity); sendJsonResponse(routingContext, jsonObject.encodePrettily()); } else { throw new VertxException(routingContext.normalisedPath()); } }
From source file:org.jberet.vertx.rest.JBeretRouterConfig.java
License:Open Source License
private static void getJobExecution(final RoutingContext routingContext) { final long jobExecutionId = getIdAsLong(routingContext, "jobExecutionId"); final JobExecutionEntity jobExecutionEntity = JobService.getInstance().getJobExecution(jobExecutionId); setJobExecutionEntityHref(routingContext, jobExecutionEntity); final JsonObject jsonObject = JsonObject.mapFrom(jobExecutionEntity); sendJsonResponse(routingContext, jsonObject.encodePrettily()); }
From source file:org.jberet.vertx.rest.JBeretRouterConfig.java
License:Open Source License
private static void getStepExecution(final RoutingContext routingContext) { final long jobExecutionId = getIdAsLong(routingContext, "jobExecutionId"); final long stepExecutionId = getIdAsLong(routingContext, "stepExecutionId"); StepExecutionEntity stepExecutionFound = null; final StepExecutionEntity[] stepExecutions = JobService.getInstance().getStepExecutions(jobExecutionId); for (StepExecutionEntity e : stepExecutions) { if (e.getStepExecutionId() == stepExecutionId) { stepExecutionFound = e;//from w w w . j a v a 2 s . c o m } } final JsonObject jsonObject = JsonObject.mapFrom(stepExecutionFound); sendJsonResponse(routingContext, jsonObject.encodePrettily()); }
From source file:org.jberet.vertx.rest.JBeretRouterConfig.java
License:Open Source License
private static void restartJobExecution(final RoutingContext routingContext) { final long jobExecutionId = getIdAsLong(routingContext, "jobExecutionId"); final Properties jobParams = getJobParameters(routingContext); final JobExecutionEntity jobExecutionEntity = JobService.getInstance().restart(jobExecutionId, jobParams); setJobExecutionEntityHref(routingContext, jobExecutionEntity); final JsonObject jsonObject = JsonObject.mapFrom(jobExecutionEntity); sendJsonResponse(routingContext, jsonObject.encodePrettily()); }
From source file:org.jberet.vertx.rest.JBeretRouterConfig.java
License:Open Source License
private static void getJobInstances(final RoutingContext routingContext) { final HttpServerRequest request = routingContext.request(); final String jobName = request.getParam("jobName"); final String startString = request.getParam("start"); final int start = startString == null ? 0 : Integer.parseInt(startString); final String countString = request.getParam("count"); final int count = countString == null ? 0 : Integer.parseInt(countString); final String jobExecutionIdString = request.getParam("jobExecutionId"); final long jobExecutionId = jobExecutionIdString == null ? 0 : Long.parseLong(jobExecutionIdString); if (jobExecutionId > 0) { final JobInstanceEntity jobInstanceData = JobService.getInstance().getJobInstance(jobExecutionId); final JsonObject jsonObject = JsonObject.mapFrom(jobInstanceData); sendJsonResponse(routingContext, jsonObject.encodePrettily()); } else {//from w w w. j a va2 s . co m final JobInstanceEntity[] jobInstanceData = JobService.getInstance().getJobInstances( jobName == null ? "*" : jobName, start, count == 0 ? Integer.MAX_VALUE : count); final JsonArray jsonArray = new JsonArray(); for (JobInstanceEntity e : jobInstanceData) { jsonArray.add(JsonObject.mapFrom(e)); } sendJsonResponse(routingContext, jsonArray.encodePrettily()); } }
From source file:org.jberet.vertx.rest.JBeretRouterConfig.java
License:Open Source License
private static void getJobSchedule(final RoutingContext routingContext) { final JobSchedule jobSchedule = lookupJobScheduleWithPathParam(routingContext); final JsonObject jsonObject = JsonObject.mapFrom(jobSchedule); sendJsonResponse(routingContext, jsonObject.encodePrettily()); }