actually set the prgram to the free port
This commit is contained in:
parent
bc144b52ae
commit
1c4085ce51
1 changed files with 29 additions and 0 deletions
29
server/src/main/java/server/ServerPortCustomizer.java
Normal file
29
server/src/main/java/server/ServerPortCustomizer.java
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
|
||||
package server;
|
||||
|
||||
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
|
||||
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ServerPortCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory>{
|
||||
/**
|
||||
* Changes the port that will be used to launch the program
|
||||
* made with the help of: https://www.baeldung.com/spring-boot-change-port
|
||||
* @param factory
|
||||
*/
|
||||
@Override
|
||||
public void customize(ConfigurableWebServerFactory factory) {
|
||||
try {
|
||||
PortChecker portChecker = new PortChecker();
|
||||
int port = portChecker.findFreePort();
|
||||
factory.setPort(port);
|
||||
System.out.println("Server starts on port: " + port);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to find a free port", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue