Example usage for java.net ServerSocket getLocalPort

List of usage examples for java.net ServerSocket getLocalPort

Introduction

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

Prototype

public int getLocalPort() 

Source Link

Document

Returns the port number on which this socket is listening.

Usage

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

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

    port = socket.getLocalPort();
    socket.close();//from  w  w  w . ja v  a2 s . com

    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:com.talis.jersey.apitest.DefaultExceptionMapperAcceptanceTest.java

private int findFreePort() throws IOException {
    ServerSocket serverSocket = new ServerSocket(0);
    int localPort = serverSocket.getLocalPort();
    serverSocket.close();//w  ww .  ja  v  a 2s  .  c o m
    return localPort;
}

From source file:com.miraclelinux.historygluon.ConnectionThread.java

@Override
public void run() {
    ServerSocket server = null;
    try {//  w w w. j  a va 2 s .  c o m
        server = new ServerSocket(m_port);
        m_log.info("start listening on port: " + server.getLocalPort());
        while (true) {
            Socket client = server.accept();
            StorageDriver driver = m_driver.createInstance();
            BridgeWorker bridge = new BridgeWorker(client, driver);
            bridge.start();
        }
    } catch (Exception e) {
        e.printStackTrace();
        m_log.error(e);
    }
}

From source file:org.ow2.proactive.resourcemanager.node.jmx.SigarExposer.java

@Override
public int getJMXRMIConnectorServerPort() {
    ServerSocket server;
    try {/*  w w  w .  ja va 2  s  .  c o m*/
        server = new ServerSocket(0);
        int port = server.getLocalPort();
        server.close();
        return port;
    } catch (Exception e) {
        LOGGER.error("", e);
    }

    // in worst case try to return a random port from the range 5000-6000
    return (int) (5000 + (Math.random() * 1000));
}

From source file:test.unit.be.e_contract.dssp.client.DigitalSignatureServiceClientTest.java

private int getFreePort() throws IOException {
    ServerSocket server = new ServerSocket(0);
    int port = server.getLocalPort();
    server.close();/*  ww  w . ja  va  2  s. c  om*/
    return port;
}

From source file:org.apache.cxf.dosgi.singlebundle.AggregatedActivatorTest.java

@Override
protected void setUp() throws Exception {
    oldDefaultPort = AggregatedActivator.DEFAULT_HTTP_PORT;
    // Change the default port to one that we know is available
    ServerSocket s = new ServerSocket(0);
    int availablePort = s.getLocalPort();
    s.close();//  w ww.  j a  v  a  2 s.  c om
    AggregatedActivator.DEFAULT_HTTP_PORT = "" + availablePort;

    savedProps = new HashMap<Object, Object>(System.getProperties());
    super.setUp();
}

From source file:com.ning.arecibo.dao.MysqlTestingHelper.java

public MysqlTestingHelper() {
    // New socket on any free port
    final ServerSocket socket;
    try {/*from ww  w  .java  2s.  c  o m*/
        socket = new ServerSocket(0);
        port = socket.getLocalPort();
        socket.close();
    } catch (IOException e) {
        Assert.fail();
    }
}

From source file:com.zxy.commons.hystrix.HystrixProperties.java

/**
 * get hystrix stream port/*from w  w w. j  av a 2  s  .  co m*/
 * 
 * @return port
 * @throws IOException IOException
*/
public int getHystrixStreamPort() throws IOException {
    String portStr = env.getProperty(HYSTRIX_STREAM_PORT);
    if (StringUtils.isBlank(portStr)) {
        ServerSocket socket = new ServerSocket(0);
        try {
            return socket.getLocalPort();
        } finally {
            socket.close();
        }
    }
    return Integer.parseInt(portStr);
}

From source file:com.streamsets.datacollector.http.TestLogServlet.java

private int getRandomPort() throws Exception {
    ServerSocket ss = new ServerSocket(0);
    int port = ss.getLocalPort();
    ss.close();/*from   w  ww . ja  v a  2s  . co m*/
    return port;
}

From source file:org.apache.solr.prometheus.exporter.SolrExporterTest.java

@Test
public void testExecute() throws Exception {
    // solr client
    CloudSolrClient cloudSolrClient = cluster.getSolrClient();

    int port;/*from   ww  w  . j a  v a 2s.  c  om*/
    ServerSocket socket = null;
    try {
        socket = new ServerSocket(0);
        port = socket.getLocalPort();
    } finally {
        socket.close();
    }

    // index sample docs
    File exampleDocsDir = new File(getFile("exampledocs").getAbsolutePath());
    List<File> xmlFiles = Arrays.asList(exampleDocsDir.listFiles((dir, name) -> name.endsWith(".xml")));
    for (File xml : xmlFiles) {
        ContentStreamUpdateRequest req = new ContentStreamUpdateRequest("/update");
        req.addFile(xml, "application/xml");
        cloudSolrClient.request(req, "collection1");
    }
    cloudSolrClient.commit("collection1");

    // start exporter
    SolrExporter solrExporter = new SolrExporter(port, cloudSolrClient,
            getFile("conf/solr-exporter-config.xml").toPath(), 1);
    try {
        solrExporter.start();

        URI uri = new URI("http://localhost:" + String.valueOf(port) + "/metrics");

        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            HttpGet request = new HttpGet(uri);
            response = httpclient.execute(request);

            int expectedHTTPStatusCode = HttpStatus.SC_OK;
            int actualHTTPStatusCode = response.getStatusLine().getStatusCode();
            assertEquals(expectedHTTPStatusCode, actualHTTPStatusCode);
        } finally {
            response.close();
            httpclient.close();
        }
    } finally {
        solrExporter.stop();
    }
}