Java HTTP Port Find getValidZooKeeperPort()

Here you can find the source of getValidZooKeeperPort()

Description

Check from DEFAULT_ZK_PORT to (DEFAULT_ZK_PORT + TRY_PORT_COUNT) to see if there is valid port to launch embed zookeeper server.

License

Apache License

Return

valid zookeeper port

Declaration

public static int getValidZooKeeperPort() 

Method Source Code

//package com.java2s;
/*//from w  ww  .  ja  v a  2  s. co m
 * Copyright [2013-2014] eBay Software Foundation
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;

import java.net.InetAddress;
import java.net.Socket;

import java.net.UnknownHostException;

import java.util.Random;

public class Main {
    private static final int DEFAULT_ZK_PORT = 2181;
    /**
     * Client connection retry count
     */
    public static final int RETRY_COUNT = 4;
    /**
     * How many ports will be used to launch embed zookeeper server.
     */
    public static final int TRY_PORT_COUNT = 20;
    /**
     * Initial zookeeper port used for check valid zookeeper port.
     */
    public static final int INITAL_ZK_PORT = -1;
    private static final Random RANDOM = new Random();

    /**
     * Check from DEFAULT_ZK_PORT to (DEFAULT_ZK_PORT + TRY_PORT_COUNT) to see if there is valid port to launch embed
     * zookeeper server.
     * 
     * @return valid zookeeper port
     */
    public static int getValidZooKeeperPort() {
        int zkValidPort = INITAL_ZK_PORT;
        int initialPort = DEFAULT_ZK_PORT;
        // add random port to avoid port in the same small range.
        if (System.currentTimeMillis() % 2 == 0) {
            zkValidPort += RANDOM.nextInt(100);
        } else {
            zkValidPort -= RANDOM.nextInt(100);
        }
        for (int i = initialPort; i < (initialPort + TRY_PORT_COUNT); i++) {
            try {
                if (!isServerAlive(InetAddress.getLocalHost(), i)) {
                    zkValidPort = i;
                    break;
                }
            } catch (UnknownHostException e) {
                throw new RuntimeException(e);
            }
        }
        if (zkValidPort == INITAL_ZK_PORT) {
            throw new RuntimeException(
                    "Too many ports are used, please submit guagua app later or specify one zookeeper instance.");
        }
        return zkValidPort;
    }

    /**
     * Check whether a server is alive.
     * 
     * @param host
     *            the server host
     * @param port
     *            the server port
     * @return true if a server is alive, false if a server is not alive.
     */
    public static boolean isServerAlive(InetAddress host, int port) {
        Socket socket = null;
        int i = 0;
        while (i < RETRY_COUNT) {
            try {
                socket = new Socket(host, port);
                break;
            } catch (IOException e) {
                i++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                    Thread.currentThread().interrupt();
                }
            } finally {
                try {
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException ignore) {
                }
            }
        }

        return i != RETRY_COUNT;
    }

    /**
     * Check whether a server is alive.
     * 
     * @param host
     *            the server host
     * @param port
     *            the server port
     * @return true if a server is alive, false if a server is not alive.
     */
    public static boolean isServerAlive(String host, int port) {
        Socket socket = null;
        int i = 0;
        while (i++ < RETRY_COUNT) {
            try {
                socket = new Socket(host, port);
                break;
            } catch (IOException e) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                    Thread.currentThread().interrupt();
                }
            } finally {
                try {
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException ignore) {
                }
            }
        }

        return i < RETRY_COUNT;
    }
}

Related

  1. getSupportedEntityTypes()
  2. getUnusedPort()
  3. getUnusedPort(final int start)
  4. getUnusedPort(int startPort)
  5. getUnusedPorts(final int count, final int start)
  6. getWsdlLocation(String portPrefix)
  7. getXmlReport(String fileName)
  8. hasPermissionToBindPort(int port)
  9. isActivePort(int port)