use of org.springframework.web.context.request.async.WebAsyncTask in project brewery by spring-cloud-samples.
the class IngredientsFetchController method ingredients.
/**
* [SLEUTH] WebAsyncTask
*/
@RequestMapping(value = "/{ingredient}", method = RequestMethod.POST)
public WebAsyncTask<Ingredient> ingredients(@PathVariable("ingredient") IngredientType ingredientType, @RequestHeader("PROCESS-ID") String processId, @RequestHeader(TestConfigurationHolder.TEST_COMMUNICATION_TYPE_HEADER_NAME) String testCommunicationType) {
log.info("Received a request to [/{}] with process id [{}] and communication type [{}]", ingredientType, processId, testCommunicationType);
return new WebAsyncTask<>(() -> {
Span span = tracer.nextSpan().name("inside_ingredients").start();
try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) {
Ingredient ingredient = new Ingredient(ingredientType, stubbedIngredientsProperties.getReturnedIngredientsQuantity());
log.info("Returning [{}] as fetched ingredient from an external service", ingredient);
return ingredient;
} finally {
span.finish();
}
});
}
use of org.springframework.web.context.request.async.WebAsyncTask in project cas by apereo.
the class HealthCheckController method handleRequestInternal.
/**
* Handle request.
*
* @param request the request
* @param response the response
* @return the model and view
* @throws Exception the exception
*/
@GetMapping
@ResponseBody
protected WebAsyncTask<HealthStatus> handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
ensureEndpointAccessIsAuthorized(request, response);
final Callable<HealthStatus> asyncTask = () -> {
final HealthStatus healthStatus = healthCheckMonitor.observe();
response.setStatus(healthStatus.getCode().value());
if (StringUtils.equals(request.getParameter("format"), "json")) {
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
JsonUtils.render(healthStatus.getDetails(), response);
} else {
final StringBuilder sb = new StringBuilder();
sb.append("Health: ").append(healthStatus.getCode());
final AtomicInteger i = new AtomicInteger();
healthStatus.getDetails().forEach((name, status) -> {
response.addHeader("X-CAS-" + name, String.format("%s;%s", status.getCode(), status.getDescription()));
sb.append("\n\n\t").append(i.incrementAndGet()).append('.').append(name).append(": ");
sb.append(status.getCode());
if (status.getDescription() != null) {
sb.append(" - ").append(status.getDescription());
}
});
response.setContentType(MediaType.TEXT_PLAIN_VALUE);
try (Writer writer = response.getWriter()) {
IOUtils.copy(new ByteArrayInputStream(sb.toString().getBytes(response.getCharacterEncoding())), writer, StandardCharsets.UTF_8);
writer.flush();
}
}
return null;
};
return new WebAsyncTask<>(casProperties.getHttpClient().getAsyncTimeout(), asyncTask);
}
use of org.springframework.web.context.request.async.WebAsyncTask in project spring-cloud-sleuth by spring-cloud.
the class TraceWebAspect method wrapWebAsyncTaskWithCorrelationId.
@Around("anyControllerOrRestControllerWithPublicWebAsyncTaskMethod()")
public Object wrapWebAsyncTaskWithCorrelationId(ProceedingJoinPoint pjp) throws Throwable {
final WebAsyncTask<?> webAsyncTask = (WebAsyncTask<?>) pjp.proceed();
if (this.tracer.currentSpan() != null) {
try {
if (log.isDebugEnabled()) {
log.debug("Wrapping callable with span [" + this.tracer.currentSpan() + "]");
}
Field callableField = WebAsyncTask.class.getDeclaredField("callable");
callableField.setAccessible(true);
callableField.set(webAsyncTask, new TraceCallable<>(this.tracer, this.spanNamer, this.errorParser, webAsyncTask.getCallable()));
} catch (NoSuchFieldException ex) {
log.warn("Cannot wrap webAsyncTask's callable with TraceCallable", ex);
}
}
return webAsyncTask;
}
Aggregations