SpringBoot @Async 異步的使用基礎篇


請參考文檔:https://rensanning.iteye.com/blog/2360749

參考: 
http://qiita.com/kazuki43zoo/items/8be79f98621f90865b78 
http://qiita.com/kazuki43zoo/items/ce88dea403c596249e8a 
http://qiita.com/kazuki43zoo/items/53b79fe91c41cc5c2e59   

 

異步處理 

  • Java的異步處理Thread/Runnable、Callable/Future
  • Servlet 2.5的異步處理 Tomcat的CometProcessor、Jetty的Continuations
  • Servlet 3.0的異步處理 asyncSupported、AsyncContext
  • Spring MVC的異步處理 @Async、AsyncTaskExecutor
  • Spring MVC的SSE ResponseBodyEmitter、SseEmitter、StreamingResponseBody

 

  • Spring Boot本身對異步調用沒有多大的變動,基本還是Spring MVC的@Async。 

 

(1)開啟Spring的異步支持  (添加@EnableAsync)

    @Configuration
   @EnableAsync public class SpringAsyncConfig{   
      
    }  

 

開啟@EnableWebMvc的時候也自動開啟了異步處理,但在Spring Boot項目中是不能使用@EnableWebMvc的。它會使Spring Boot的AutoConfigure一部分功能失效。
官方文檔里有說明:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration

 

 //可以不配置
    @ComponentScan("com.sea.test")  
    @Configuration  
    public class WebMvcConfig extends WebMvcConfigurerAdapter {  
        
      
     //配置異步的支持 @Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) { configurer.setTaskExecutor(mvcAsyncExecutor()); }   
    
     //異步線程池的定義 @Bean
public AsyncTaskExecutor mvcAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setMaxPoolSize(10); return executor; } }

 

 

線程池建議這樣配置:

    @Configuration  
    @EnableAsync  
    public class ThreadPollConfig {  
      
        /** Set the ThreadPoolExecutor's core pool size. */  
        private int corePoolSize = 10;  
        /** Set the ThreadPoolExecutor's maximum pool size. */  
        private int maxPoolSize = 200;  
        /** Set the capacity for the ThreadPoolExecutor's BlockingQueue. */  
        private int queueCapacity = 10;  
      
        private String ThreadNamePrefix = "MyLogExecutor-";  
      
        @Bean  
        public Executor logExecutor() {  
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();  
            executor.setCorePoolSize(corePoolSize);  
            executor.setMaxPoolSize(maxPoolSize);  
            executor.setQueueCapacity(queueCapacity);  
            executor.setThreadNamePrefix(ThreadNamePrefix);  
            // rejection-policy:當pool已經達到max size的時候,如何處理新任務  
            // CALLER_RUNS:不在新線程中執行任務,而是有調用者所在的線程來執行  
            executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());  
            executor.initialize();  
            return executor;  
        }  
      
    }  

 

 

 

 

(2)定義需要執行異步處理的方法

1)沒有返回值

 

    @Async  
    public void asyncMethodWithVoidReturnType() {  
        System.out.println("Execute method asynchronously. "  
          + Thread.currentThread().getName());  
    }  

2)帶返回值

    @Async  
    public Future<String> asyncMethodWithReturnType() {  
        System.out.println("Execute method asynchronously - "  
          + Thread.currentThread().getName());  
        try {  
            Thread.sleep(5000);  
            return new AsyncResult<String>("hello world !");  
        } catch (InterruptedException e) {  
        }  
       
        return null;  
    }  

 

(3)異步線程池的定義

1)一個線程池

    @Configuration  
    @EnableAsync  
    public class SpringAsyncConfig {  
        @Bean  
        public AsyncTaskExecutor taskExecutor() {  
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();  
            executor.setMaxPoolSize(10);  
            return executor;  
        }  
    }  

2)多個線程池

    @Configuration  
    @EnableAsync  
    public class SpringAsyncConfig {  
           
        @Bean(name = "threadPoolTaskExecutor1")  
        public Executor threadPoolTaskExecutor() {  
            return new ThreadPoolTaskExecutor();  
        }  
           
        @Bean(name = "threadPoolTaskExecutor2")  
        public Executor threadPoolTaskExecutor() {  
            return new ThreadPoolTaskExecutor();  
        }  
      
    }  

 

 

3) 多個線程池指定使用哪一個:

    @Async("threadPoolTaskExecutor1")  
    public void asyncMethodWithConfiguredExecutor() {  
        System.out.println("Execute method with configured executor - "  
          + Thread.currentThread().getName());  
    }  

 

(4)異步異常的處理

    public interface AsyncConfigurer {  
        Executor getAsyncExecutor();  
        AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler();  
    }  

AsyncConfigurerSupport是AsyncConfigurer接口的實現但里邊什么也沒做。 

    @Configuration  
    @EnableAsync  
    class SpringAsyncConfigurer extends AsyncConfigurerSupport {  
      
        @Bean  
        public ThreadPoolTaskExecutor asyncExecutor() {  
            ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();  
            threadPool.setCorePoolSize(3);  
            threadPool.setMaxPoolSize(3);  
            threadPool.setWaitForTasksToCompleteOnShutdown(true);  
            threadPool.setAwaitTerminationSeconds(60 * 15);  
            return threadPool;  
        }  
      
        @Override  
        public Executor getAsyncExecutor() {  
            return asyncExecutor;  
        }  
    }  

 

可以自己實現AsyncConfigurer接口處理異常。 

    @Configuration  
    @EnableAsync  
    public class SpringAsyncConfigurer implements AsyncConfigurer {  
           
        @Override  
        public Executor getAsyncExecutor() {  
            return new ThreadPoolTaskExecutor();  
        }  
      
        @Override  
        public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {  
            return new CustomAsyncExceptionHandler();  
        }  
      
    }  

 

    public class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {  
       
        @Override  
        public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {  
            System.out.println("Exception message - " + throwable.getMessage());  
            System.out.println("Method name - " + method.getName());  
            for (Object param : obj) {  
                System.out.println("Parameter value - " + param);  
            }  
        }  
           
    }  

     

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM