Spring Boot @Async method in controller is executing synchronously

后端 未结 6 1720
天涯浪人
天涯浪人 2020-12-07 18:02

My [basic] Spring Boot application accepts a request from the browser, sent via jQuery.get() and is supposed to immediately receive a response - such as \"y

6条回答
  •  清歌不尽
    2020-12-07 18:14

    For all those who are still looking for all the steps in @Asnyc explained in a simple way, here is the answer:

    Here is a simple example with @Async. Follow these steps to get @Async to work in your Spring Boot application:

    Step 1: Add @EnableAsync annotation and Add TaskExecutor Bean to Application Class.

    Example:

    @SpringBootApplication
    @EnableAsync
    public class AsynchronousSpringBootApplication {
    
        private static final Logger logger = LoggerFactory.getLogger(AsynchronousSpringBootApplication.class);
    
        @Bean(name="processExecutor")
        public TaskExecutor workExecutor() {
            ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
            threadPoolTaskExecutor.setThreadNamePrefix("Async-");
            threadPoolTaskExecutor.setCorePoolSize(3);
            threadPoolTaskExecutor.setMaxPoolSize(3);
            threadPoolTaskExecutor.setQueueCapacity(600);
            threadPoolTaskExecutor.afterPropertiesSet();
            logger.info("ThreadPoolTaskExecutor set");
            return threadPoolTaskExecutor;
        }
    
        public static void main(String[] args) throws Exception {
      SpringApplication.run(AsynchronousSpringBootApplication.class,args);
     }
    }
    

    Step 2: Add Method which executes an Asynchronous Process

    @Service
    public class ProcessServiceImpl implements ProcessService {
    
        private static final Logger logger = LoggerFactory.getLogger(ProcessServiceImpl.class);
    
        @Async("processExecutor")
        @Override
        public void process() {
            logger.info("Received request to process in ProcessServiceImpl.process()");
            try {
                Thread.sleep(15 * 1000);
                logger.info("Processing complete");
            }
            catch (InterruptedException ie) {
                logger.error("Error in ProcessServiceImpl.process(): {}", ie.getMessage());
            }
        }
    }
    

    Step 3: Add an API in the Controller to execute the asynchronous processing

    @Autowired
    private ProcessService processService;
    
    @RequestMapping(value = "ping/async", method = RequestMethod.GET)
        public ResponseEntity> async() {
            processService.process();
            Map response = new HashMap<>();
            response.put("message", "Request is under process");
            return new ResponseEntity<>(response, HttpStatus.OK);
        }
    

    I have also written a blog and a working application on GitHub with these steps. Please check: http://softwaredevelopercentral.blogspot.com/2017/07/asynchronous-processing-async-in-spring.html

提交回复
热议问题