WebAsyncTask 직접 방출 처리 스레드 연결, 서버 처리량 향상
4405 단어 동시 프로그래밍
web
서버 스레드, 사용자 요청 처리, 스레드 탱크 관리..도구 클래스:
public class WebAsyncUtils {
Logger logger = LoggerFactory.getLogger(WebAsyncUtils.class);
/**
*
* @param callable
* @return
*/
public WebAsyncTask instance(Callable callable) {
WebAsyncTask asyncTask = new WebAsyncTask<>(Constants.OUT_TIME, callable);
asyncTask.onCompletion(() -> logger.info(" "));
asyncTask.onError(() -> {
return new JsonResponse(JsonResponse.CODE_FAILURE, "out_line");
});
asyncTask.onTimeout(() -> {
return new JsonResponse(JsonResponse.CODE_FAILURE, "out_time_line");
});
return asyncTask;
}
}
구성:
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Resource private CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver;
// WebMvcRegistrationsAdapter a1 = new WebMvcRegistrationsAdapter();
@Bean
public HttpMessageConverter responseBodyConverter(){
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
return converter;
}
@Override
public void configureMessageConverters(List> converters) {
// FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
// // ...
// FastJsonConfig config = new FastJsonConfig();
// /**
// * QuoteFieldNames———- key , true
// WriteMapNullValue——– null , false
// WriteNullNumberAsZero—- null, 0, null
// WriteNullListAsEmpty—–List null, [], null
// WriteNullStringAsEmpty— null, ”“, null
// WriteNullBooleanAsFalse–Boolean null, false, null
// */
// config.setCharset(Charset.forName("UTF-8"));
// config.setSerializerFeatures(SerializerFeature.PrettyFormat);
//// config.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
// converter.setFastJsonConfig(config);
converters.add(responseBodyConverter());
}
// springboot
// WebMvcRegistrations RequestMappingHandlerMapping
@Configuration
public class WebMvcRegistrationsConfig implements WebMvcRegistrations {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new MyRequestMappingHandlerMapping();
}
}
@Override
public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(60 * 1000L);
configurer.registerCallableInterceptors(timeoutInterceptor());
configurer.setTaskExecutor(threadPoolTaskExecutor());
}
@Bean
public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
return new TimeoutCallableProcessingInterceptor();
}
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor t = new ThreadPoolTaskExecutor();
t.setCorePoolSize(10);
t.setMaxPoolSize(50);
t.setThreadNamePrefix("MISSION");
return t;
}
}
사용:
@CrossOrigin
@RequestMapping(value = "missions")
public WebAsyncTask getMyMissionList(Integer fid,Integer classId, Integer uid,int page,int pageSize) {
WebAsyncTask asyncTask = new WebAsyncUtils().instance(()-> {
try {
List
Spring/Spring boot 비동기식 작업 프로그래밍 자세히 보기 WebAsyncTask