|
21 | 21 | import java.util.function.ObjIntConsumer; |
22 | 22 | import java.util.stream.Collectors; |
23 | 23 |
|
24 | | -import javax.management.ObjectName; |
25 | | - |
26 | 24 | import org.apache.catalina.Lifecycle; |
27 | | -import org.apache.catalina.core.StandardThreadExecutor; |
28 | 25 | import org.apache.catalina.valves.AccessLogValve; |
29 | 26 | import org.apache.catalina.valves.ErrorReportValve; |
30 | 27 | import org.apache.catalina.valves.RemoteIpValve; |
|
39 | 36 | import org.springframework.boot.autoconfigure.web.ServerProperties; |
40 | 37 | import org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Accesslog; |
41 | 38 | import org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Remoteip; |
42 | | -import org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Threads; |
43 | 39 | import org.springframework.boot.cloud.CloudPlatform; |
44 | 40 | import org.springframework.boot.context.properties.PropertyMapper; |
45 | 41 | import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory; |
@@ -98,7 +94,16 @@ public void customize(ConfigurableTomcatWebServerFactory factory) { |
98 | 94 | .as(Long::intValue) |
99 | 95 | .to(factory::setBackgroundProcessorDelay); |
100 | 96 | customizeRemoteIpValve(factory); |
101 | | - configureExecutor(factory, properties.getThreads()); |
| 97 | + ServerProperties.Tomcat.Threads threadProperties = properties.getThreads(); |
| 98 | + map.from(threadProperties::getMax) |
| 99 | + .when(this::isPositive) |
| 100 | + .to((maxThreads) -> customizeMaxThreads(factory, maxThreads)); |
| 101 | + map.from(threadProperties::getMinSpare) |
| 102 | + .when(this::isPositive) |
| 103 | + .to((minSpareThreads) -> customizeMinThreads(factory, minSpareThreads)); |
| 104 | + map.from(threadProperties::getMaxQueueCapacity) |
| 105 | + .when(this::isPositive) |
| 106 | + .to((maxQueueCapacity) -> customizeMaxQueueCapacity(factory, maxQueueCapacity)); |
102 | 107 | map.from(this.serverProperties.getMaxHttpRequestHeaderSize()) |
103 | 108 | .asInt(DataSize::toBytes) |
104 | 109 | .when(this::isPositive) |
@@ -146,23 +151,25 @@ public void customize(ConfigurableTomcatWebServerFactory factory) { |
146 | 151 | customizeErrorReportValve(this.serverProperties.getError(), factory); |
147 | 152 | } |
148 | 153 |
|
149 | | - private void configureExecutor(ConfigurableTomcatWebServerFactory factory, Threads threadProperties) { |
150 | | - factory.addProtocolHandlerCustomizers((handler) -> { |
151 | | - StandardThreadExecutor executor = new StandardThreadExecutor(); |
152 | | - executor.setMinSpareThreads(threadProperties.getMinSpare()); |
153 | | - executor.setMaxThreads(threadProperties.getMax()); |
154 | | - executor.setMaxQueueSize(threadProperties.getMaxQueueCapacity()); |
155 | | - if (handler instanceof AbstractProtocol<?> protocol) { |
156 | | - executor.setNamePrefix(ObjectName.unquote(protocol.getName()) + "-exec-"); |
157 | | - } |
158 | | - handler.setExecutor(executor); |
159 | | - }); |
160 | | - } |
161 | | - |
162 | 154 | private boolean isPositive(int value) { |
163 | 155 | return value > 0; |
164 | 156 | } |
165 | 157 |
|
| 158 | + @SuppressWarnings("rawtypes") |
| 159 | + private void customizeMaxThreads(ConfigurableTomcatWebServerFactory factory, int maxThreads) { |
| 160 | + customizeHandler(factory, maxThreads, AbstractProtocol.class, AbstractProtocol::setMaxThreads); |
| 161 | + } |
| 162 | + |
| 163 | + @SuppressWarnings("rawtypes") |
| 164 | + private void customizeMinThreads(ConfigurableTomcatWebServerFactory factory, int minSpareThreads) { |
| 165 | + customizeHandler(factory, minSpareThreads, AbstractProtocol.class, AbstractProtocol::setMinSpareThreads); |
| 166 | + } |
| 167 | + |
| 168 | + @SuppressWarnings("rawtypes") |
| 169 | + private void customizeMaxQueueCapacity(ConfigurableTomcatWebServerFactory factory, int maxQueueCapacity) { |
| 170 | + customizeHandler(factory, maxQueueCapacity, AbstractProtocol.class, AbstractProtocol::setMaxQueueSize); |
| 171 | + } |
| 172 | + |
166 | 173 | @SuppressWarnings("rawtypes") |
167 | 174 | private void customizeAcceptCount(ConfigurableTomcatWebServerFactory factory, int acceptCount) { |
168 | 175 | customizeHandler(factory, acceptCount, AbstractProtocol.class, AbstractProtocol::setAcceptCount); |
|
0 commit comments