【优化】Springboot 修改 tomcat连接池

发布时间:2023年12月19日

【优化】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());
        };
    }
    
}

文章来源:https://blog.csdn.net/G971005287W/article/details/135090078
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。