Search in sources :

Example 1 with Ingredient

use of io.spring.cloud.samples.brewery.common.model.Ingredient 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();
        }
    });
}
Also used : Ingredient(io.spring.cloud.samples.brewery.common.model.Ingredient) Tracer(brave.Tracer) WebAsyncTask(org.springframework.web.context.request.async.WebAsyncTask) Span(brave.Span) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with Ingredient

use of io.spring.cloud.samples.brewery.common.model.Ingredient in project brewery by spring-cloud-samples.

the class IngredientsAggregator method fetchIngredients.

// TODO: Consider simplifying the case by removing the DB (always matches threshold)
public Ingredients fetchIngredients(Order order, String processId, TestConfigurationHolder testConfigurationHolder) throws Exception {
    TestConfigurationHolder.TEST_CONFIG.set(testConfigurationHolder);
    log.info("Fetching ingredients for order [{}] , processId [{}]", order, processId);
    /**
     * [SLEUTH] ParallelStreams won't work out of the box
     * - example of a completable future with our TraceableExecutorService
     * - makes little business sense here but that's just an example
     */
    CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> {
        TestConfigurationHolder.TEST_CONFIG.set(testConfigurationHolder);
        ingredientsCollector.collectIngredients(order, processId).stream().filter(ingredient -> ingredient != null).forEach((Ingredient ingredient) -> {
            log.info("Adding an ingredient [{}] for order [{}] , processId [{}]", ingredient);
            ingredientWarehouse.addIngredient(ingredient);
        });
        return null;
    }, new TraceableExecutorService(this.beanFactory, Executors.newFixedThreadPool(5), "fetchIngredients"));
    // block to perform the request (as I said the example is stupid)
    completableFuture.get();
    eventGateway.emitEvent(Event.builder().eventType(EventType.INGREDIENTS_ORDERED).processId(processId).build());
    Ingredients ingredients = ingredientWarehouse.getCurrentState();
    return maturingUpdater.updateIfLimitReached(ingredients, processId);
}
Also used : Autowired(org.springframework.beans.factory.annotation.Autowired) TraceableExecutorService(org.springframework.cloud.sleuth.instrument.async.TraceableExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) TestConfigurationHolder(io.spring.cloud.samples.brewery.common.TestConfigurationHolder) Ingredients(io.spring.cloud.samples.brewery.common.model.Ingredients) Executors(java.util.concurrent.Executors) Event(io.spring.cloud.samples.brewery.common.events.Event) EventGateway(io.spring.cloud.samples.brewery.common.events.EventGateway) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) Order(io.spring.cloud.samples.brewery.common.model.Order) BeanFactory(org.springframework.beans.factory.BeanFactory) EventType(io.spring.cloud.samples.brewery.common.events.EventType) Ingredient(io.spring.cloud.samples.brewery.common.model.Ingredient) CompletableFuture(java.util.concurrent.CompletableFuture) Ingredient(io.spring.cloud.samples.brewery.common.model.Ingredient) Ingredients(io.spring.cloud.samples.brewery.common.model.Ingredients) TraceableExecutorService(org.springframework.cloud.sleuth.instrument.async.TraceableExecutorService)

Aggregations

Ingredient (io.spring.cloud.samples.brewery.common.model.Ingredient)2 Span (brave.Span)1 Tracer (brave.Tracer)1 TestConfigurationHolder (io.spring.cloud.samples.brewery.common.TestConfigurationHolder)1 Event (io.spring.cloud.samples.brewery.common.events.Event)1 EventGateway (io.spring.cloud.samples.brewery.common.events.EventGateway)1 EventType (io.spring.cloud.samples.brewery.common.events.EventType)1 Ingredients (io.spring.cloud.samples.brewery.common.model.Ingredients)1 Order (io.spring.cloud.samples.brewery.common.model.Order)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Executors (java.util.concurrent.Executors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 TraceableExecutorService (org.springframework.cloud.sleuth.instrument.async.TraceableExecutorService)1 Component (org.springframework.stereotype.Component)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 WebAsyncTask (org.springframework.web.context.request.async.WebAsyncTask)1