org.apache.storm.utils.Utils.getAvailablePort()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(3.8k)|赞(0)|评价(0)|浏览(97)

本文整理了Java中org.apache.storm.utils.Utils.getAvailablePort()方法的一些代码示例,展示了Utils.getAvailablePort()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Utils.getAvailablePort()方法的具体详情如下:
包路径:org.apache.storm.utils.Utils
类名称:Utils
方法名:getAvailablePort

Utils.getAvailablePort介绍

[英]Shortcut to calling #getAvailablePort(int) with 0 as the preferred port
[中]

代码示例

代码示例来源:origin: apache/storm

/**
 * Shortcut to calling {@link #getAvailablePort(int) } with 0 as the preferred port
 *
 * @return A random available port
 */
public static int getAvailablePort() {
  return getAvailablePort(0);
}

代码示例来源:origin: apache/storm

/**
 * Gets an available port. Consider if it is possible to pass port 0 to the server instead of using this method, since there is no
 * guarantee that the port returned by this method will remain free.
 *
 * @param preferredPort
 * @return The preferred port if available, or a random available port
 */
public static int getAvailablePort(int preferredPort) {
  int localPort = -1;
  try (ServerSocket socket = new ServerSocket(preferredPort)) {
    localPort = socket.getLocalPort();
  } catch (IOException exp) {
    if (preferredPort > 0) {
      return getAvailablePort(0);
    }
  }
  return localPort;
}

代码示例来源:origin: apache/storm

try {
  AtomicReference<TaskMessage> response = new AtomicReference<>();
  int port = Utils.getAvailablePort(6700);
  try (IConnection client = context.connect(null, "localhost", port, remoteBpStatus)) {
    AtomicReference<IConnection> server = new AtomicReference<>();

代码示例来源:origin: apache/storm

private void doTestServerAlwaysReconnects(Map<String, Object> stormConf) throws Exception {
  LOG.info("6. test server always reconnects");
  String reqMessage = "0123456789abcdefghijklmnopqrstuvwxyz";
  IContext context = TransportFactory.makeContext(stormConf);
  try {
    AtomicReference<TaskMessage> response = new AtomicReference<>();
    int port = Utils.getAvailablePort(6700);
    try (IConnection client = context.connect(null, "localhost", port, remoteBpStatus)) {
      byte[] messageBytes = reqMessage.getBytes(StandardCharsets.UTF_8);
      send(client, taskId, messageBytes);
      try (IConnection server = context.bind(null, port)) {
        server.registerRecv(mkConnectionCallback(response::set));
        waitUntilReady(client, server);
        send(client, taskId, messageBytes);
        waitForNotNull(response);
        TaskMessage responseMessage = response.get();
        assertThat(responseMessage.task(), is(taskId));
        assertThat(responseMessage.message(), is(messageBytes));
      }
    }
  } finally {
    context.term();
  }
}

代码示例来源:origin: apache/storm

@Test
public void testSubmitTopologyToLocalNimbus() throws Exception {
  int port = Utils.getAvailablePort();
  try (ILocalCluster localCluster = new LocalCluster.Builder()
    .withNimbusDaemon(true)
    .withDaemonConf(Config.NIMBUS_THRIFT_PORT, port)
    .build()) {
    Config topoConf = new Config();
    topoConf.putAll(Utils.readDefaultConfig());
    topoConf.setDebug(true);
    topoConf.put("storm.cluster.mode", "local"); // default is aways "distributed" but here local cluster is being used.
    topoConf.put(Config.STORM_TOPOLOGY_SUBMISSION_NOTIFIER_PLUGIN, InmemoryTopologySubmitterHook.class.getName());
    topoConf.put(Config.NIMBUS_THRIFT_PORT, port);
    List<TopologyDetails> topologyNames = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
      final String topologyName = "word-count-" + UUID.randomUUID().toString();
      final StormTopology stormTopology = createTestTopology();
      topologyNames.add(new TopologyDetails(topologyName, stormTopology));
      localCluster.submitTopology(topologyName, topoConf, stormTopology);
    }
    Assert.assertEquals(InmemoryTopologySubmitterHook.submittedTopologies, topologyNames);
  }
}

代码示例来源:origin: org.apache.storm/storm-core

public static int getAvailablePort() {
  return getAvailablePort(0);
}

代码示例来源:origin: org.apache.storm/storm-core

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;
}

相关文章

微信公众号

最新文章

更多

Utils类方法