zoukankan      html  css  js  c++  java
  • springboot异步处理请求 结合自定义线程池

    package yinhu.yinhu.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import yinhu.yinhu.yh.vo.AsyncTest;
    
    @Controller
    @EnableAsync
    public class AsyncController {
        @Autowired
        private AsyncTest asyncTest;
        @ResponseBody
        @GetMapping
        @RequestMapping(value="/async")
        public String test() {
            asyncTest.mywait();
            System.out.println("wait");
            return "ok";
        }
    
    }
    package yinhu.yinhu.yh.vo;
    
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Component;
    
    @Component
    public class AsyncTest {
        @Async("taskExecutor")//自己配置的自定义线程池的名字(也就是带bean的方法名);也可以不要,不要的话就只能用框架自带的线程池
        public void mywait() {
            System.out.println("------------------oh no");
            // TODO Auto-generated method stub
            try {
                Thread.sleep(1000*10);
                System.out.println("线程名字"+Thread.currentThread().getName());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("------------------oh yes");
        }
    }
    package yinhu.yinhu.yh.config;
    
    import java.util.concurrent.Executor;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
    
    @Configuration
    public class MyAsyncTaskThreadPool {
        private int poolsize=10;
        private int maxpoolsize=200;
        private int queueCapacity=10;
        
        @Bean
        public Executor taskExecutor() {
            ThreadPoolTaskExecutor threadPoolTaskExecutor=new ThreadPoolTaskExecutor();
            threadPoolTaskExecutor.setCorePoolSize(poolsize);
            threadPoolTaskExecutor.setMaxPoolSize(maxpoolsize);
            threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
            threadPoolTaskExecutor.setThreadNamePrefix("线程前缀名字");
            threadPoolTaskExecutor.initialize();
            return threadPoolTaskExecutor;
        }
        
    }

    springboot异步处理请求并响应的方式
    controller加@EnableAsync注解
    组件类(如)中的方法上方加@Async注解

  • 相关阅读:
    Maven学习总结(二)——Maven项目构建过程练习
    使用Maven搭建Struts2框架的开发环境
    使用Maven编译项目遇到——“maven编码gbk的不可映射字符”解决办法
    MyEclipse10安装Log4E插件
    大数据以及Hadoop相关概念介绍
    Servlet3.0学习总结(四)——使用注解标注监听器(Listener)
    Servlet3.0学习总结(三)——基于Servlet3.0的文件上传
    Servlet3.0学习总结(二)——使用注解标注过滤器(Filter)
    Servlet3.0学习总结(一)——使用注解标注Servlet
    使用kaptcha生成验证码
  • 原文地址:https://www.cnblogs.com/qinyios/p/12551498.html
Copyright © 2011-2022 走看看