@Configuration
public class TomcatConfig {
@Value("${server.port}")
private String port;
@Value("${server.acceptorThreadCount}")
private String acceptorThreadCount;
@Value("${server.minSpareThreads}")
private String minSpareThreads;
@Value("${server.maxSpareThreads}")
private String maxSpareThreads;
@Value("${server.maxThreads}")
private String maxThreads;
@Value("${server.maxConnections}")
private String maxConnections;
@Value("${server.protocol}")
private String protocol;
@Value("${server.redirectPort}")
private String redirectPort;
// @Value("${server.compression}")
// private String compression;
@Value("${server.connectionTimeout}")
private String connectionTimeout;
@Value("${server.MaxFileSize}")
private String MaxFileSize;
@Value("${server.MaxRequestSize}")
private String MaxRequestSize;
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addConnectorCustomizers(new VarysTomcatConnectionCustomizer());
return tomcat;
}
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
// 单个数据大小
factory.setMaxFileSize(MaxFileSize); // KB,MB
/// 总上传数据大小
factory.setMaxRequestSize(MaxRequestSize);
return factory.createMultipartConfig();
}
public class VarysTomcatConnectionCustomizer implements TomcatConnectorCustomizer {
public VarysTomcatConnectionCustomizer() {
}
@Override
public void customize(Connector connector) {
/*connector.setPort(Integer.valueOf(port));
connector.setAttribute("connectionTimeout", connectionTimeout);
connector.setAttribute("acceptorThreadCount", acceptorThreadCount);
connector.setAttribute("minSpareThreads", minSpareThreads);
connector.setAttribute("maxSpareThreads", maxSpareThreads);
connector.setAttribute("maxThreads", maxThreads);
connector.setAttribute("maxConnections", maxConnections);
connector.setAttribute("protocol", protocol);
connector.setAttribute("redirectPort", "redirectPort");*/
// connector.setAttribute("compression", "compression");
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
protocol.setPort(Integer.valueOf(port));
protocol.setMaxConnections(Integer.valueOf(maxConnections));
protocol.setConnectionTimeout(Integer.valueOf(connectionTimeout));
protocol.setAcceptorThreadCount(Integer.valueOf(acceptorThreadCount));
protocol.setMinSpareThreads(Integer.valueOf(minSpareThreads));
protocol.setMaxThreads(Integer.valueOf(maxThreads));
}
}
}