springmvc使用线程池Executor

1.在application-mvc中加入如下头文件内容:

xmlns:task="http://www.springframework.org/schema/task"

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd



<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">  
        <!-- 核心线程数 -->       
        <property name="corePoolSize" value="${task.core_pool_size}" />  
        <!-- 最大线程数 -->    
        <property name="maxPoolSize" value="${task.max_pool_size}" />  
        <!-- 队列最大长度 -->  
        <property name="queueCapacity" value="${task.queue_capacity}" />  
        <!-- 线程池维护线程所允许的空闲时间,默认为60s -->  
        <property name="keepAliveSeconds" value="${task.keep_alive_seconds}" />  
    </bean>  
    <!-- 注解式 -->  
    <task:annotation-driven />  



2.在所需要的service或者controller类里面添加

@Resource(name = "taskExecutor")  
private TaskExecutor taskExecutor;

3.测试

@Controller  
public class IndexController {  
int studentscount = 0;  
@RequestMapping(value = "/index.html")  
    public ModelAndView goIndex() {  
        logBefore(logger, "列表Center");  
        ModelAndView mv = this.getModelAndView();  
                taskExecutor.execute(new Runnable() {  
  
                @Override  
                public void run() {  
                    // TODO Auto-generated method stub  
                    // 得到所有学生人数  
                    try {  
                                          studentscount = coursesService.getStudentCount(pd);  
                    } catch (Exception e) {  
                        // TODO Auto-generated catch block  
                        e.printStackTrace();  
                    }  
  
                }  
            });  
                  mv.addObject("studentscount", studentscount);  
                  mv.setViewName("common/index");  
                  return mv;  
} 


原文转自:https://blog.csdn.net/qq_24046745/article/details/5281658
阅读更多
个人分类: SSM
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭