在springboot中扩展tomcat的executor

扩展executor

public class AdvancedThreadExecutor extends StandardThreadExecutor {

    @Override
    protected void startInternal() throws LifecycleException {
        super.namePrefix = "custom-tomcat-";
        super.startInternal();
        executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                //add stat
                throw new RejectedExecutionException("Task " + r.toString() +
                        " rejected from " +
                        executor.toString());
            }
        });
    }
}

配置

@Component
public class EmbeddedTomcatConfig implements EmbeddedServletContainerCustomizer {

    private static final Logger logger = LoggerFactory.getLogger(EmbeddedTomcatConfig.class);

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        if(container instanceof TomcatEmbeddedServletContainerFactory){
            TomcatEmbeddedServletContainerFactory factory = (TomcatEmbeddedServletContainerFactory)container;
            factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
                @Override
                public void customize(Connector connector) {
                    AdvancedThreadExecutor myExecutor = new AdvancedThreadExecutor();
                    connector.getService().addExecutor(myExecutor);
                    ProtocolHandler handler = connector.getProtocolHandler();

                    Http11NioProtocol http11NioProtocol = (Http11NioProtocol)handler;
                    http11NioProtocol.setExecutor(myExecutor);
                }
            });
        }
    }
}

验证

curl -i http://localhost:8080/dump
    原文作者:codecraft
    原文地址: https://segmentfault.com/a/1190000008929195
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞