Scan for port near default
This commit is contained in:
parent
e69e190ae9
commit
59f07d37ab
|
|
@ -113,7 +113,7 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer {
|
||||||
public void customize(ConfigurableEmbeddedServletContainerFactory factory) {
|
public void customize(ConfigurableEmbeddedServletContainerFactory factory) {
|
||||||
Integer port = getPort();
|
Integer port = getPort();
|
||||||
if (this.scan) {
|
if (this.scan) {
|
||||||
port = SocketUtils.findAvailableTcpPort(port != null ? 8080 : port);
|
port = scanForPort(port);
|
||||||
}
|
}
|
||||||
if (port != null) {
|
if (port != null) {
|
||||||
factory.setPort(port);
|
factory.setPort(port);
|
||||||
|
|
@ -132,6 +132,26 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Integer scanForPort(Integer port) {
|
||||||
|
boolean found = false;
|
||||||
|
int delta = 1;
|
||||||
|
port = port == null ? 8080 : port;
|
||||||
|
while (!found) {
|
||||||
|
try {
|
||||||
|
port = SocketUtils.findAvailableTcpPort(port, port + delta);
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
catch (IllegalStateException e) {
|
||||||
|
if (delta > 65536) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
delta = delta > 5 ? delta > 100 ? delta * 4 : delta * 3 : delta * 2;
|
||||||
|
}
|
||||||
|
port = port + delta;
|
||||||
|
}
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
public static class Tomcat {
|
public static class Tomcat {
|
||||||
|
|
||||||
private String accessLogPattern;
|
private String accessLogPattern;
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,15 @@ public class ServerPropertiesTests {
|
||||||
assertTrue(factory.getPort() > 1000);
|
assertTrue(factory.getPort() > 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPortScanFromHigher() throws Exception {
|
||||||
|
this.properties.setScan(true);
|
||||||
|
this.properties.setPort(5678);
|
||||||
|
ConfigurableEmbeddedServletContainerFactory factory = new MockEmbeddedServletContainerFactory();
|
||||||
|
this.properties.customize(factory);
|
||||||
|
assertTrue(factory.getPort() < 6000);
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME test customize
|
// FIXME test customize
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue