标签:
/** * Project: aliwork.web * * File Created at 2014-10-15 * $Id$ * * Copyright 2008 Alibaba.com Corporation Limited. * All rights reserved. * * This software is the confidential and proprietary information of * Alibaba Company. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Alibaba.com. */ package com.alibaba.aliwork.web.listener; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import javax.servlet.ServletContext; import org.springframework.web.context.ContextLoader; import org.springframework.web.context.ContextLoaderListener; import com.alibaba.citrus.webx.WebxComponent; import com.alibaba.citrus.webx.WebxController; import com.alibaba.citrus.webx.context.WebxComponentContext; import com.alibaba.citrus.webx.context.WebxComponentsContext; import com.alibaba.citrus.webx.context.WebxComponentsLoader; /** * 在原有WebxContextLoaderListener的基础上做了加速<br> * 使用多线程加载components,启动速度大幅度提升<br> * components越多,速度提升越明显<br> * * @author xiaoxiaosir.chenxg 2014-10-15 */ public class WebxFastContextLoaderListener extends ContextLoaderListener { @Override protected final ContextLoader createContextLoader() { return new WebxComponentsLoader() { @Override protected Class<? extends WebxComponentsContext> getDefaultContextClass() { Class<? extends WebxComponentsContext> defaultContextClass = WebxFastContextLoaderListener.this .getDefaultContextClass(); if (defaultContextClass == null) { defaultContextClass = super.getDefaultContextClass(); } return defaultContextClass; } /** * 多线程初始化components */ @Override public void finishRefresh() { this.getWebxComponents().getWebxRootController().onFinishedProcessContext(); this.getServletContext().log("Start to concurrent load components"); int poolSize = Runtime.getRuntime().availableProcessors() * 2; int componentNum = this.getWebxComponents().getComponentNames().length; ServletContext context = this.getServletContext(); if (componentNum < poolSize) { // 针对模块较少的应用做的优化 poolSize = componentNum; } ExecutorService executorService = Executors.newFixedThreadPool(poolSize); CountDownLatch countDownLatch = new CountDownLatch(componentNum); for (WebxComponent component : this.getWebxComponents()) { Thread loader = new FastLoader(component, context, countDownLatch); if (!executorService.isShutdown()) { executorService.submit(loader); } } // wait until all components are loaded. try { countDownLatch.await(); } catch (InterruptedException e) { throw new RuntimeException(e); } this.getServletContext().log("WebxComponents: initialization completed"); } }; } protected Class<? extends WebxComponentsContext> getDefaultContextClass() { return null; } class FastLoader extends Thread { private WebxComponent component; private ServletContext getServletContext; private CountDownLatch countDownLatch; public FastLoader(WebxComponent component, ServletContext getServletContext, CountDownLatch countDownLatch) { this.component = component; this.getServletContext = getServletContext; this.countDownLatch = countDownLatch; } @Override public void run() { try { getServletContext.log("Initializing Spring sub WebApplicationContext: " + component.getName()); WebxComponentContext wcc = (WebxComponentContext) component.getApplicationContext(); WebxController controller = component.getWebxController(); wcc.refresh(); controller.onFinishedProcessContext(); } finally { countDownLatch.countDown(); } } } }
标签:
原文地址:http://www.cnblogs.com/zhanghaoh/p/4684129.html