Example usage for java.net ServerSocket close

List of usage examples for java.net ServerSocket close

Introduction

In this page you can find the example usage for java.net ServerSocket close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this socket.

Usage

From source file:com.baidu.terminator.manager.service.LinkServiceImpl.java

@Override
public boolean isPortAvailable(int port) {
    int count = linkMapper.countValidPort(port);
    if (count >= 1) {
        return false;
    } else {/*from  w  w  w .j  av  a2s  .  c o m*/
        try {
            ServerSocket s = new ServerSocket(port);
            s.close();
        } catch (IOException e) {
            return false;
        }
    }
    return true;
}

From source file:net.timewalker.ffmq4.jmx.rmi.JMXOverRMIServerSocketFactory.java

/**
 * Cleanup sockets created by this factory
 *//*from  w w w.  j av  a  2 s  . c om*/
public void close() {
    if (!manageSockets)
        throw new IllegalStateException("Cannot close an un-managed socket factory");

    synchronized (createdSockets) {
        Iterator<ServerSocket> sockets = createdSockets.iterator();
        while (sockets.hasNext()) {
            ServerSocket socket = sockets.next();
            try {
                socket.close();
            } catch (Exception e) {
                log.error("Could not close server socket", e);
            }
        }
        createdSockets.clear();
    }
}

From source file:sample.config.RedisConfig.java

private int getPort() throws IOException {
    if (availablePort == null) {
        ServerSocket socket = new ServerSocket(0);
        availablePort = socket.getLocalPort();
        socket.close();
    }/*w  w  w. j  a v  a2 s.co  m*/
    return availablePort;
}

From source file:org.mule.test.infrastructure.process.CommandServer.java

private void closeQuietly(ServerSocket loggerSocket) {
    try {//  www  . j av a  2 s  .  com
        if (loggerSocket != null) {
            loggerSocket.close();
        }
    } catch (Exception e) {
        //Do nothing.
    }
}

From source file:org.apache.sling.commons.messaging.mail.MailTestSupport.java

protected synchronized int findFreePort() {
    try {//from   w  ww. jav a2 s  .com
        final ServerSocket serverSocket = new ServerSocket(0);
        final int port = serverSocket.getLocalPort();
        serverSocket.close();
        return port;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.ning.billing.dbi.MysqlTestingHelper.java

public void startMysql() throws IOException {
    // New socket on any free port
    final ServerSocket socket = new ServerSocket(0);
    port = socket.getLocalPort();/*w  w w  .j  a  va 2  s  .  c  o  m*/
    socket.close();

    dbDir = File.createTempFile("mysql", "");
    dbDir.delete();
    dbDir.mkdir();
    mysqldResource = new MysqldResource(dbDir);

    final Map<String, String> dbOpts = new HashMap<String, String>();
    dbOpts.put(MysqldResourceI.PORT, Integer.toString(port));
    dbOpts.put(MysqldResourceI.INITIALIZE_USER, "true");
    dbOpts.put(MysqldResourceI.INITIALIZE_USER_NAME, USERNAME);
    dbOpts.put(MysqldResourceI.INITIALIZE_PASSWORD, PASSWORD);

    mysqldResource.start("test-mysqld-thread", dbOpts);
    if (!mysqldResource.isRunning()) {
        throw new IllegalStateException("MySQL did not start.");
    } else {
        log.info("MySQL running on port " + mysqldResource.getPort());
    }
}

From source file:org.sonar.server.database.EmbeddedDatabaseTest.java

private int findFreeServerPort() throws IOException, InterruptedException {
    ServerSocket srv = new ServerSocket(0);
    int port = srv.getLocalPort();
    srv.close();
    Thread.sleep(1500);/* w w  w . j  a v  a 2  s.c  o  m*/
    return port;
}

From source file:com.ning.metrics.collector.processing.db.CollectorMysqlTestingHelper.java

public void startMysql() throws IOException {
    ServerSocket socket = new ServerSocket(0);

    port = socket.getLocalPort();/* w  w  w.  jav a 2s  . c  om*/
    socket.close();

    dbDir = File.createTempFile("mysqldb", ".db");
    Assert.assertTrue(dbDir.delete());
    Assert.assertTrue(dbDir.mkdir());

    mysqldResource = new MysqldResource(dbDir);

    Map<String, String> dbOpts = new HashMap<String, String>();

    dbOpts.put(MysqldResourceI.PORT, Integer.toString(port));
    dbOpts.put(MysqldResourceI.INITIALIZE_USER, "true");
    dbOpts.put(MysqldResourceI.INITIALIZE_USER_NAME, USERNAME);
    dbOpts.put(MysqldResourceI.INITIALIZE_PASSWORD, PASSWORD);

    mysqldResource.start("test-mysqld-thread", dbOpts);

    if (!mysqldResource.isRunning()) {
        throw new IllegalStateException("MySQL did not start.");
    }
}

From source file:org.mule.transport.tcp.issues.LingerExperimentMule2067TestCase.java

protected void openCloseServer(int numberOfSockets, int port) throws IOException {
    for (int i = 0; i < numberOfSockets; i++) {
        ServerSocket socket = new ServerSocket(port);
        socket.close();
    }//from   w  ww.  j av a2  s. c om
}

From source file:org.mule.test.infrastructure.process.TestProcess.java

private void closeQuietly(ServerSocket loggerSocket) {
    try {/* www. j  a v a  2  s  .co m*/
        if (loggerSocket != null) {
            loggerSocket.close();
        }
    } catch (IOException e) {
        //Do nothing.
    }
}