springboot2 继承WebMvcConfigurationSupport 后自动配置失效,swagger-ui.html 无法访问

5 篇文章 0 订阅
2 篇文章 0 订阅

项目中使用callable 返回,释放tomcat线程 增加吞吐量

    //列表查询
    @ApiOperation(value = "职务、部门、工种列表", notes = "下拉框列表,职务、部门、工种列表大数据高性能")
    @RequestMapping(value = "/listParatest", method = RequestMethod.GET)
    public  Callable<Map<String, Object>> listPara2()  {
        System.out.println("主线开始"+Thread.currentThread()+":"+System.currentTimeMillis());
        Callable<Map<String, Object>> personinfo =new Callable<Map<String, Object>>() {
            @Override
            public Map<String, Object> call() throws Exception {
                System.out.println("子线开始"+Thread.currentThread()+":"+System.currentTimeMillis());
                Map<String, Object> personMap = personService.listPostAndDepAndTypeWork2();//用于下拉列表选择职务
                System.out.println("子程结束"+Thread.currentThread()+":"+System.currentTimeMillis());
                return personMap;
            }
        };
        System.out.println("主线程结束"+Thread.currentThread()+":"+System.currentTimeMillis());
        return personinfo;
    }


 @Override
    public Map<String, Object> listPostAndDepAndTypeWork2() {

        ExecutorService tasks = Executors.newFixedThreadPool(3);
        Callable<List<Map<String, Object>>> postinfo = new Callable<List<Map<String, Object>>>() {
            @Override
            public List<Map<String, Object>> call() throws Exception {
                List<Map<String, Object>> postList = postDao.findAllPost();//用于下拉列表选择职务
                return postList;
            }
        };
        Callable<List<Map<String, Object>>> departmentinfo = new Callable<List<Map<String, Object>>>() {
            @Override
            public List<Map<String, Object>> call() throws Exception {
                List<Map<String, Object>> departmentList = departmentDao.findAllDepartment();//用于下拉列表选择部门
                return departmentList;
            }
        };
        Callable<List<Map<String, Object>>> typeworkinfo = new Callable<List<Map<String, Object>>>() {
            @Override
            public List<Map<String, Object>> call() throws Exception {
                List<Map<String, Object>> typeworkList = typeworkDao.findAllTypework();//用于下拉列表选择工种
                return typeworkList;
            }
        };
        FutureTask<List<Map<String, Object>>> postTask = new FutureTask<List<Map<String, Object>>>(postinfo);
        FutureTask<List<Map<String, Object>>> departmentTask = new FutureTask<List<Map<String, Object>>>(departmentinfo);
        FutureTask<List<Map<String, Object>>> typeworkTask = new FutureTask<List<Map<String, Object>>>(typeworkinfo);

        tasks.submit(postTask);
        tasks.submit(departmentTask);
        tasks.submit(typeworkTask);

        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map.put("postList", postTask.get());
            map.put("departmentList", departmentTask.get());
            map.put("typeworkList", typeworkTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return map;
    }

警告提醒

2019-01-24 10:58:12 920 [WARN]: 
!!!
An Executor is required to handle java.util.concurrent.Callable return values.
Please, configure a TaskExecutor in the MVC config under "async support".
The SimpleAsyncTaskExecutor currently in use is not suitable under load.
-------------------------------
Request URI: '/AnRanPSLS/person/listParatest'
!!!

后参照网上资料加入以下重写方法来源:https://www.jb51.net/article/134289.htm

@Configuration
@ConfigurationProperties(prefix = "web-mvc-async")
public class WebMvcConfig extends WebMvcConfigurationSupport {
 
    @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(corePoolSize);
        t.setMaxPoolSize(maxPoolSize);
        t.setQueueCapacity(queueCapacity);
        t.setThreadNamePrefix("WYF-Thread-");
        return t;
    }
}

springboot2 继承WebMvcConfigurationSupport 后自动配置失效,swagger-ui.html 无法访问,查资料后加入以下配置来源:https://blog.csdn.net/liu0bing/article/details/80826590 


    @Bean
    LoginInterceptor localInterceptor() {
        return new LoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }



发现项目中使用的entityManager 的

query2.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

NativeQueryImpl报错,自动化配置失效导致。后又查找发现不继承WebMvcConfigurationSupport,改为实现

WebMvcConfigurer接口即可,具体实现如下代码即可
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
   

    @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(100);
        t.setQueueCapacity(20);
        t.setThreadNamePrefix("WYF-Thread-");
        return t;
    }
}

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值