【优化】Springboot 修改 tomcat连接池

【优化】Springboot 修改 tomcat连接池

factory.setTomcatProtocolHandlerCustomizers(tomcatProtocolHandlerCustomizers);

可以更换为 虚拟线程连接池

复制代码
package org.config.init;

import org.apache.catalina.Context;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.tomcat.util.scan.StandardJarScanner;
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executors;

/**
 * @Description: TomcatFactoryConfig
 */
@Configuration
public class TomcatFactoryConfig {
    /**
     * tomcat-embed-jasper引用后提示jar找不到的问题
     */
    @Bean
    public TomcatServletWebServerFactory tomcatFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                ((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
            }
        };
        factory.addConnectorCustomizers(connector -> {
//            connector.setProperty("relaxedPathChars", "[]{}");
//            connector.setProperty("relaxedQueryChars", "[]{}");

            connector.setProperty("relaxedPathChars", "\"#<>[\\]^`{|}/");
            connector.setProperty("relaxedQueryChars", "\"#<>[\\]^`{|}/");

        });


        //支持APR  AIO
        //factory.setProtocol("org.apache.coyote.http11.Http11AprProtocol");
        //factory.addContextLifecycleListeners(new AprLifecycleListener());
        //支持APR  AIO

        Collection<TomcatProtocolHandlerCustomizer<?>> tomcatProtocolHandlerCustomizers = factory.getTomcatProtocolHandlerCustomizers();
        tomcatProtocolHandlerCustomizers.add(protocolHandlerCustomizer());
        factory.setTomcatProtocolHandlerCustomizers(tomcatProtocolHandlerCustomizers);

        return factory;
    }

    public static TomcatProtocolHandlerCustomizer<?> protocolHandlerCustomizer(){
        return protocolHandler -> {
            protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
        };
    }
    
}
相关推荐
Asthenia04127 小时前
Spring AOP 和 Aware:在Bean实例化后-调用BeanPostProcessor开始工作!在初始化方法执行之前!
后端
Asthenia04128 小时前
什么是消除直接左递归 - 编译原理解析
后端
Asthenia04128 小时前
什么是自上而下分析 - 编译原理剖析
后端
Asthenia04128 小时前
什么是语法分析 - 编译原理基础
后端
Asthenia04129 小时前
理解词法分析与LEX:编译器的守门人
后端
uhakadotcom9 小时前
视频直播与视频点播:基础知识与应用场景
后端·面试·架构
Asthenia041210 小时前
Spring扩展点与工具类获取容器Bean-基于ApplicationContextAware实现非IOC容器中调用IOC的Bean
后端
bobz96510 小时前
ovs patch port 对比 veth pair
后端
Asthenia041210 小时前
Java受检异常与非受检异常分析
后端