Click here to Skip to main content
15,906,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
'm getting this error java.lang.IllegalStateException: Calling [asyncPostProcess()] is not valid for a request with Async state [ERROR] in an API gateway when I'm executing order-service which uploads more than 700,000 records fetched from an API into PostgreSQL database

Below is my code implementation

What I have tried:

API Gateway Main Class

@SpringBootApplication
@EnableDiscoveryClient
public class ApiGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayApplication.class, args);
    }
}



API Gateway Yaml File


server:
   port: 6464
   
eureka:
    client:
       service-url:
           default-zone: http://localhost:8761/eureka/
           
spring:
   main:
     web-application-type: reactive
   cloud:
      gateway:
          routes:
            - id: ordering-service
              uri: lb://ordering-service
              predicates:
                   - Path=/upload/**
           
management:
    tracing:
       sampling:
           probability: 1.0



Order Processing Service


@Service
public class OrderProcessingService {
    
   @Autowired
   private OrderRepository orderRepository;

   @Transactional
   public void process_data(String orderCode, List<Order> order_list) {
      
   int batchSize = 45000;
    
   for (int i = 0; i < order_list.size(); i += batchSize) {
        
   int endIndex = Math.min(i + batchSize, order_list.size());
    
   List<Order> batch = order_list.subList(i, endIndex);
    
   orderRepository.saveAll(batch);
    
   }
                
   }
}



Orders API


@RestController
@RequestMapping("/api")
public class OrderUploading {
    
    @Autowired
    private OrderProcessingService order_processing_service;
    
    @PostMapping("/orders/{orderCode}")
    private void get_orderss(@PathVariable String orderCode, @RequestBody List<Order> order) {
        order_processing_service.process_data(orderCode, order);
    }
}



Orders Service Yaml File


server:
    port: 8081
    
spring:
   datasource:
      url: jdbc:postgresql://localhost/mms_db?useServerPrepStmts=false&rewriteBatchedStatements=true
      driver-class-name: org.postgresql.Driver
      username: root
      password: root
      hikari:
         idle-timeout: 600000
         maximum-pool-size: 100
         max-life-time: 600000
   jpa:
     database-platform: org.hibernate.dialect.PostgreSQLDialect
     hibernate:
       ddl-auto: update
       
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka/
      
management:
    tracing:
       sampling:
          probability: 1.0



The issue is, not all 700,000 records received from the API are inserted into the PostgreSQL database since data are received in batches so you find at times 200,000 records have been inserted, sometimes 127,000 records, sometimes 398,000 records, sometimes 527,000 records and sometimes all 700,000 records are inserted.

when there is any partial data insertion into PostgreSQL database I find there is an error in an API Gateway as below



[2m2024-04-08T10:57:26.352+03:00[0;39m [31mERROR[0;39m [35m16808[0;39m [2m---[0;39m [2m[api-gateway] [io-9191-exec-15][0;39m [2m[                                                 ] [0;39m[36mo.a.coyote.http11.Http11NioProtocol     [0;39m [2m:[0;39m Error reading request, ignored

java.lang.IllegalStateException: Calling [asyncPostProcess()] is not valid for a request with Async state [ERROR]
    at org.apache.coyote.AsyncStateMachine.asyncPostProcess(AsyncStateMachine.java:302) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProcessor.asyncPostProcess(AbstractProcessor.java:197) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:78) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at java.base/java.lang.Thread.run(Thread.java:840) ~[na:na]

[2m2024-04-08T10:57:26.376+03:00[0;39m [31mERROR[0;39m [35m16808[0;39m [2m---[0;39m [2m[api-gateway] [io-9191-exec-16][0;39m [2m[                                                 ] [0;39m[36mo.a.coyote.http11.Http11NioProtocol     [0;39m [2m:[0;39m Error reading request, ignored

java.lang.IllegalStateException: Calling [asyncPostProcess()] is not valid for a request with Async state [ERROR]
    at org.apache.coyote.AsyncStateMachine.asyncPostProcess(AsyncStateMachine.java:302) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProcessor.asyncPostProcess(AbstractProcessor.java:197) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:78) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at java.base/java.lang.Thread.run(Thread.java:840) ~[na:na]

[2m2024-04-08T10:57:26.382+03:00[0;39m [31mERROR[0;39m [35m16808[0;39m [2m---[0;39m [2m[api-gateway] [io-9191-exec-19][0;39m [2m[                                                 ] [0;39m[36mo.a.coyote.http11.Http11NioProtocol     [0;39m [2m:[0;39m Error reading request, ignored

java.lang.IllegalStateException: Calling [asyncPostProcess()] is not valid for a request with Async state [ERROR]
    at org.apache.coyote.AsyncStateMachine.asyncPostProcess(AsyncStateMachine.java:302) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProcessor.asyncPostProcess(AbstractProcessor.java:197) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:78) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at java.base/java.lang.Thread.run(Thread.java:840) ~[na:na]

[2m2024-04-08T10:57:27.117+03:00[0;39m [31mERROR[0;39m [35m16808[0;39m [2m---[0;39m [2m[api-gateway] [io-9191-exec-12][0;39m [2m[                                                 ] [0;39m[36mo.a.coyote.http11.Http11NioProtocol     [0;39m [2m:[0;39m Error reading request, ignored

java.lang.IllegalStateException: Calling [asyncPostProcess()] is not valid for a request with Async state [ERROR]
    at org.apache.coyote.AsyncStateMachine.asyncPostProcess(AsyncStateMachine.java:302) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProcessor.asyncPostProcess(AbstractProcessor.java:197) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:78) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at java.base/java.lang.Thread.run(Thread.java:840) ~[na:na]

[2m2024-04-08T10:57:27.632+03:00[0;39m [31mERROR[0;39m [35m16808[0;39m [2m---[0;39m [2m[api-gateway] [nio-9191-exec-2][0;39m [2m[                                                 ] [0;39m[36mo.a.coyote.http11.Http11NioProtocol     [0;39m [2m:[0;39m Error reading request, ignored

java.lang.IllegalStateException: Calling [asyncPostProcess()] is not valid for a request with Async state [ERROR]
    at org.apache.coyote.AsyncStateMachine.asyncPostProcess(AsyncStateMachine.java:302) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProcessor.asyncPostProcess(AbstractProcessor.java:197) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:78) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-10.1.18.jar:10.1.18]
    at java.base/java.lang.Thread.run(Thread.java:840) ~[na:na]
Posted
Comments
Pete O'Hanlon 8-Apr-24 5:47am    
What versions of Spring Boot and Tomcat are you running here?
Office Systems 8-Apr-24 5:58am    
I'm using Spring Boot version 3.2.3 and Tomcat version 10.1.19 @Pete O'Hanlon
Office Systems 15-Apr-24 5:20am    
Any suggestions here

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900