/** * Shortcut to calling {@link #getAvailablePort(int) } with 0 as the preferred port * * @return A random available port */ public static int getAvailablePort() { return getAvailablePort(0); }
/** * 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; }
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<>();
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(); } }
@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); } }
public static int getAvailablePort() { return getAvailablePort(0); }
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; }