Yêu cầu thg 3 18, 7:58 SA 113 0 1
  • 113 0 1
0

Timelimiter vẫn chạy lưu order sau khi timeout trong spring boot 6.2.2

Chia sẻ
  • 113 0 1

Xin chào mọi người em đang học spring cloud thì gặp một vấn đề như này, đó là em đã set timelimiter trong application.properties là 3s. Khi client gọi đến controller thì vẫn đẩy bug timeout nhưng đoạn code trong service vẫn chạy và vẫn lưu order vào database dù đã trả về lỗi. Em đang loay hoay chưa biết sửa sao mong mn giúp đỡ.

2024-03-18T13:51:15.204+07:00  INFO 2024 --- [order-service] [o-auto-1-exec-3] c.m.o.controller.OrderController         : Order placing...
2024-03-18T13:51:18.245+07:00 ERROR 2024 --- [order-service] [o-auto-1-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] threw exception

java.util.concurrent.TimeoutException: TimeLimiter 'inventory' recorded a timeout exception.
	at io.github.resilience4j.timelimiter.TimeLimiter.createdTimeoutExceptionWithName(TimeLimiter.java:225) ~[resilience4j-timelimiter-2.1.0.jar:2.1.0]
	at io.github.resilience4j.timelimiter.internal.TimeLimiterImpl$Timeout.lambda$of$0(TimeLimiterImpl.java:185) ~[resilience4j-timelimiter-2.1.0.jar:2.1.0]
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[na:na]
	at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264) ~[na:na]
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:na]
	at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[na:na]
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na]
	at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

2024-03-18T13:51:18.257+07:00 ERROR 2024 --- [order-service] [o-auto-1-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.util.concurrent.TimeoutException: TimeLimiter 'inventory' recorded a timeout exception.] with root cause

java.util.concurrent.TimeoutException: TimeLimiter 'inventory' recorded a timeout exception.
	at io.github.resilience4j.timelimiter.TimeLimiter.createdTimeoutExceptionWithName(TimeLimiter.java:225) ~[resilience4j-timelimiter-2.1.0.jar:2.1.0]
	at io.github.resilience4j.timelimiter.internal.TimeLimiterImpl$Timeout.lambda$of$0(TimeLimiterImpl.java:185) ~[resilience4j-timelimiter-2.1.0.jar:2.1.0]
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[na:na]
	at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264) ~[na:na]
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:na]
	at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[na:na]
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na]
	at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

Hibernate: insert into t_order (order_number) values (?)
Hibernate: insert into t_order_line_items (price,quantity,sku_code) values (?,?,?)
2024-03-18T13:51:25.943+07:00  INFO 2024 --- [order-service] [onPool-worker-2] c.m.orderservice.service.OrderService    : Order 22 is placed!
Hibernate: update t_order_line_items set fk_order_id=? where id=?
@PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    @CircuitBreaker(name = "inventory", fallbackMethod = "fallbackMethod")
    @TimeLimiter(name = "inventory")
    public CompletableFuture<String> createOrder(@RequestBody OrderRequest orderRequest) {
        log.info("Order placing...");
        return CompletableFuture.supplyAsync(() -> orderService.placeOrder(orderRequest));

    }
    public CompletableFuture<String> fallbackMethod(OrderRequest orderRequest, RuntimeException runtimeException) {
        return CompletableFuture.supplyAsync(() -> "Oops! Something went wrong, please order after some time!");
    }
public String placeOrder(OrderRequest orderRequest) {

        Set<OrderLineItems> orderLineItems = orderRequest.getOrderLineItemsDtos()
                .stream().map(this::mapToOrderLineItem).collect(Collectors.toSet());

        Order order = Order.builder()
                .orderNumber(UUID.randomUUID().toString())
                .orderLineItems(orderLineItems)
                .build();
        List<String> skuCodes = orderLineItems.stream().map(OrderLineItems::getSkuCode).toList();
        // call to inventory service
        InventoryResponse[] inventoryResponseArray = webClientBuilder.build().get()
                .uri("http://inventory-service/api/inventory",
                        uriBuilder -> uriBuilder.queryParam("skuCode", skuCodes).build())
                .retrieve()
                .bodyToMono(InventoryResponse[].class)
                .block();

        boolean allProductIsInStock = Arrays.stream(Objects.requireNonNull(inventoryResponseArray)).allMatch(InventoryResponse::getIsInStock);
        if (Boolean.TRUE.equals(allProductIsInStock)) {
            orderRepository.save(order);
            log.info("Order {} is placed!", order.getId());
            return "Order Placed Successfully";
        } else {
            throw new IllegalArgumentException("Product is not in stock, please try again later");
        }
    }

1 CÂU TRẢ LỜI


Đã trả lời thg 4 17, 7:31 SA
0

Bạn dùng CompletableFuture.supplyAsync(() -> orderService.placeOrder(orderRequest)); thì nó đang tách sang 1 thread khác để thực thi method orderService.placeOrder(orderRequest). Nó ở 2 context khác nhau thì sao mà stop được hả bạn ?

Chia sẻ
Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí