谈谈JSP的预编译加速及如何参与开源

本文探讨了JSP预编译如何通过JspC工具提高Web应用性能,特别是Tomcat新版本中引入的多线程编译加速。文章详细介绍了提交patch到开源项目的过程,并分享了作者对开源贡献的见解,鼓励读者积极参与开源项目。
摘要由CSDN通过智能技术生成

很早之前的文章,写过一篇关于加速Web应用的功能之一:「JSP预编译」JSP预编译,加速你的应用,在其中我们提到通过JSP的预编译,我们可以提前生成JSP对应的Servlet文件,从而节省执行时再生成所带来的时间占用。

JSPC

而整个JSP预编译的核心入口是 JspC这个工具。

下面两篇是预编译具体的工作原理:

新的 Tomcat Release Note 里,有一个功能,是新增了多线程编译,来加速JSP预编译。是一位热心网友提供的 patch。

我们来看下具体内容:

Make the Jasper (JSP Engine) Java file generation process multi-threaded. By default, one thread will be used per core. Based on a patch by Dan Fabulich.

这个 patch主要做了些什么事情呢? 我把其中的重点代码摘出来一起看下:

通过输入参数,设置线程数
public void setThreadCount(String threadCount) {
        if (threadCount == null) {
            return;
        }
        int newThreadCount;
        try {
            if (threadCount.endsWith("C")) {
                double factor = Double.parseDouble(threadCount.substring(0, threadCount.length() - 1));
                newThreadCount = (int) (factor * Runtime.getRuntime().availableProcessors());
            } else {
                newThreadCount = Integer.parseInt(threadCount);
            }
        } catch (NumberFormatException e) {
            throw new BuildException("Couldn't parse thread count: " + threadCount);
        }
        if (newThreadCount < 1) {
            throw new BuildException("There must be at least one thread: " + newThreadCount);
        }
        this.threadCount = newThreadCount;
    }
增加线程池的使用

上面根据输入配置好的threadCount,会在后面生成线程池的时候被使用到,同时生成的线程池,会做为提交JSP编译工作的入口。

ExecutorService threadPool = Executors.newFixedThreadPool(threadCount);
ExecutorCompletionService<Void> service = new ExecutorCompletionService<>(threadPool);
int pageCount = pages.size();

            for (String nextjsp : pages) {
                File fjsp = new File(nextjsp);
                if (!fjsp.isAbsolute()) {
                    fjsp = new File(uriRootF, nextjsp);
                }
                if (!fjsp.exists()) {
                    if (log.isWarnEnabled()) {
                        log.warn(Localizer.getMessage(
                                "jspc.error.fileDoesNotExist", fjsp.toString()));
                    }
                    continue;
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值