扩展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
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论