生活随笔
收集整理的這篇文章主要介紹了
异步调用的接口设计
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
何謂異步?
每次調用都創建一個新的線程去執行, 不會等待上一次調用執行完,再執行下一個請求
SpringBoot項目怎么做
在啟動類上添加:@EnableAsync 注解
項目中添加下面兩個類
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;import com.imooc.miaoshaproject.util.Threads;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
public class ThreadPoolConfig
{private int corePoolSize
= 50;private int maxPoolSize
= 200;private int queueCapacity
= 1000;private int keepAliveSeconds
= 300;@Bean(name
= "threadPoolTaskExecutor")public ThreadPoolTaskExecutor threadPoolTaskExecutor(){ThreadPoolTaskExecutor executor
= new ThreadPoolTaskExecutor();executor
.setMaxPoolSize(maxPoolSize
);executor
.setCorePoolSize(corePoolSize
);executor
.setQueueCapacity(queueCapacity
);executor
.setKeepAliveSeconds(keepAliveSeconds
);executor
.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());return executor
;}@Bean(name
= "scheduledExecutorService")protected ScheduledExecutorService scheduledExecutorService(){return new ScheduledThreadPoolExecutor(corePoolSize
,new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build()){@Overrideprotected void afterExecute(Runnable r
, Throwable t
){super.afterExecute(r
, t
);Threads.printException(r
, t
);}};}
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.concurrent.*;
public class Threads
{private static final Logger logger
= LoggerFactory.getLogger(Threads.class);public static void sleep(long milliseconds
){try{Thread.sleep(milliseconds
);}catch (InterruptedException e
){return;}}public static void shutdownAndAwaitTermination(ExecutorService pool
){if (pool
!= null && !pool
.isShutdown()){pool
.shutdown();try{if (!pool
.awaitTermination(120, TimeUnit.SECONDS
)){pool
.shutdownNow();if (!pool
.awaitTermination(120, TimeUnit.SECONDS
)){logger
.info("Pool did not terminate");}}}catch (InterruptedException ie
){pool
.shutdownNow();Thread.currentThread().interrupt();}}}public static void printException(Runnable r
, Throwable t
){if (t
== null && r
instanceof Future<?>){try{Future<?> future
= (Future<?>) r
;if (future
.isDone()){future
.get();}}catch (CancellationException ce
){t
= ce
;}catch (ExecutionException ee
){t
= ee
.getCause();}catch (InterruptedException ie
){Thread.currentThread().interrupt();}}if (t
!= null){logger
.error(t
.getMessage(), t
);}}
}
在需要異步調用的接口上添加@Async(“threadPoolTaskExecutor”)注解即可
@Async("threadPoolTaskExecutor")@RequestMapping(value
= "/test",method
= {RequestMethod.GET
,RequestMethod.POST
})@ResponseBodypublic void test() {System.out
.println("===test===");try {Thread.sleep(10000);} catch (InterruptedException e
) {e
.printStackTrace();}}
總結
以上是生活随笔為你收集整理的异步调用的接口设计的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。