com.github.ibole.infrastructure.common.utils.PortFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of infrastructure-all Show documentation
Show all versions of infrastructure-all Show documentation
The all in one project of ibole infrastructure
The newest version!
package com.github.ibole.infrastructure.common.utils;
import java.net.ServerSocket;
import java.util.concurrent.TimeUnit;
/*********************************************************************************************
* .
*
*
*
* Copyright 2016, iBole Inc. All rights reserved.
*
*********************************************************************************************/
public class PortFactory {
public static int findFreePort() {
int port;
try {
ServerSocket server = new ServerSocket(0);
port = server.getLocalPort();
server.close();
// allow time for the socket to be released
TimeUnit.MILLISECONDS.sleep(350);
} catch (Exception e) {
throw new RuntimeException("Exception while trying to find a free port", e);
}
return port;
}
public static void main(String[] args) {
System.out.print(findFreePort());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy