All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.scalecube.issues.i187.NodeNoInboundRunner Maven / Gradle / Ivy

package io.scalecube.issues.i187;

import io.scalecube.cluster.Cluster;
import io.scalecube.cluster.ClusterConfig;
import io.scalecube.cluster.ClusterImpl;
import io.scalecube.net.Address;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NodeNoInboundRunner {

  public static final Logger logger = LoggerFactory.getLogger(NodeNoInboundRunner.class);

  public static final int DEFAULT_PORT = 9090;
  public static final int DEFAULT_SEED_PORT = 4545;

  /**
   * Main.
   *
   * @param args args
   * @throws Exception exceptoin
   */
  public static void main(String[] args) throws Exception {
    Address address =
        getSeedAddress(args).orElseGet(() -> Address.create("localhost", DEFAULT_SEED_PORT));
    int port = getPort(args).orElse(DEFAULT_PORT);

    Map metadata =
        Collections.singletonMap("node-no-inbound", Integer.toHexString(new Object().hashCode()));

    ClusterConfig config =
        new ClusterConfig()
            .membership(
                opts ->
                    opts.syncGroup("issue187")
                        .seedMembers(address)
                        .syncInterval(1000)
                        .syncTimeout(1000))
            .transport(opts -> opts.connectTimeout(1000).port(port))
            .metadata(metadata)
            .metadataTimeout(1000);

    logger.debug("Starting Node-With-No-Inbound with config {}", config);
    Cluster cluster = new ClusterImpl(config).startAwait();
    logger.debug("Started Node-With-No-Inbound: {}, address: {}", cluster, cluster.address());

    Thread.currentThread().join();
  }

  private static Optional getPort(String[] args) {
    if (args.length < 2) {
      return Optional.empty();
    }
    String portArg = args[0];
    if (portArg.isEmpty()) {
      return Optional.empty();
    }
    try {
      return Optional.of(Integer.parseInt(portArg));
    } catch (Exception ex) {
      logger.error("Error in getPort: " + ex);
      return Optional.empty();
    }
  }

  private static Optional
getSeedAddress(String[] args) { if (args.length < 2) { return Optional.empty(); } String addressArg = args[1]; if (addressArg.isEmpty()) { return Optional.empty(); } try { return Optional.of(Address.from(addressArg)); } catch (Exception ex) { logger.error("Error in getSeedAddress: " + ex); return Optional.empty(); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy