[ 
https://issues.apache.org/jira/browse/STORM-1523?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15146022#comment-15146022
 ] 

ASF GitHub Bot commented on STORM-1523:
---------------------------------------

Github user redsanket commented on a diff in the pull request:

    https://github.com/apache/storm/pull/1073#discussion_r52829338
  
    --- Diff: storm-core/src/jvm/org/apache/storm/utils/Utils.java ---
    @@ -1446,6 +1448,22 @@ public static RuntimeException 
wrapInRuntime(Exception e){
             }
         }
     
    +    public static int getAvailablePort(int prefferedPort) {
    +        int localPort = -1;
    +        try(ServerSocket socket = new ServerSocket(prefferedPort)) {
    +            localPort = socket.getLocalPort();
    +        } catch(IOException exp) {
    +            if (prefferedPort > 0) {
    +                return getAvailablePort(0);
    +            }
    +        }
    +        return localPort;
    --- End diff --
    
    socket.getLocalPort() returns -1 if no port is found as per the internal 
implementation. No harm in assigning localPort with -1
    
    /**
         * Returns the port number on which this socket is listening.
         * <p>
         * If the socket was bound prior to being {@link #close closed},
         * then this method will continue to return the port number
         * after the socket is closed.
         *
         * @return  the port number to which this socket is listening or
         *          -1 if the socket is not bound yet.
         */
        public int getLocalPort() {
            if (!isBound())
                return -1;
            try {
                return getImpl().getLocalPort();
            } catch (SocketException e) {
                // nothing
                // If we're bound, the impl has been created
                // so we shouldn't get here
            }
            return -1;
        }



> Translate Available Ports task in util.java
> -------------------------------------------
>
>                 Key: STORM-1523
>                 URL: https://issues.apache.org/jira/browse/STORM-1523
>             Project: Apache Storm
>          Issue Type: Sub-task
>          Components: storm-core
>            Reporter: Sanket Reddy
>




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to