Search in sources :

Example 1 with ArgumentType

use of net.sourceforge.argparse4j.inf.ArgumentType in project atomix by atomix.

the class AtomixAgent method main.

public static void main(String[] args) throws Exception {
    ArgumentType<Node> nodeArgumentType = (ArgumentParser argumentParser, Argument argument, String value) -> {
        String[] address = parseAddress(value);
        return Node.builder(parseNodeId(address)).withType(Node.Type.CORE).withEndpoint(parseEndpoint(address)).build();
    };
    ArgumentType<Node.Type> typeArgumentType = (ArgumentParser argumentParser, Argument argument, String value) -> Node.Type.valueOf(value.toUpperCase());
    ArgumentType<File> fileArgumentType = (ArgumentParser argumentParser, Argument argument, String value) -> new File(value);
    ArgumentParser parser = ArgumentParsers.newArgumentParser("AtomixServer").defaultHelp(true).description("Atomix server");
    parser.addArgument("node").type(nodeArgumentType).nargs("?").metavar("NAME:HOST:PORT").setDefault(Node.builder("local").withType(Node.Type.CORE).withEndpoint(new Endpoint(InetAddress.getByName("127.0.0.1"), NettyMessagingService.DEFAULT_PORT)).build()).help("The local node info");
    parser.addArgument("--type", "-t").type(typeArgumentType).metavar("TYPE").choices("core", "data", "client").setDefault(Node.Type.CORE).help("Indicates the local node type");
    parser.addArgument("--bootstrap", "-b").nargs("*").type(nodeArgumentType).metavar("NAME:HOST:PORT").required(false).help("Bootstraps a new cluster");
    parser.addArgument("--http-port", "-p").type(Integer.class).metavar("PORT").required(false).setDefault(5678).help("An optional HTTP server port");
    parser.addArgument("--data-dir", "-dd").type(fileArgumentType).metavar("FILE").required(false).setDefault(new File(System.getProperty("user.dir"), "data")).help("The server data directory");
    parser.addArgument("--core-partitions", "-cp").type(Integer.class).metavar("NUM").required(false).setDefault(7).help("The number of core partitions");
    parser.addArgument("--data-partitions", "-dp").type(Integer.class).metavar("NUM").required(false).setDefault(71).help("The number of data partitions");
    Namespace namespace = null;
    try {
        namespace = parser.parseArgs(args);
    } catch (ArgumentParserException e) {
        parser.handleError(e);
        System.exit(1);
    }
    Node localNode = namespace.get("node");
    Node.Type type = namespace.get("type");
    localNode = Node.builder(localNode.id()).withType(type).withEndpoint(localNode.endpoint()).build();
    List<Node> bootstrap = namespace.getList("bootstrap");
    if (bootstrap == null) {
        bootstrap = Collections.singletonList(localNode);
    }
    File dataDir = namespace.get("data_dir");
    Integer httpPort = namespace.getInt("http_port");
    Integer corePartitions = namespace.getInt("core_partitions");
    Integer dataPartitions = namespace.getInt("data_partitions");
    LOGGER.info("node: {}", localNode);
    LOGGER.info("bootstrap: {}", bootstrap);
    LOGGER.info("data-dir: {}", dataDir);
    Atomix atomix = Atomix.builder().withLocalNode(localNode).withBootstrapNodes(bootstrap).withDataDirectory(dataDir).withCorePartitions(corePartitions).withDataPartitions(dataPartitions).build();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        atomix.stop().join();
    }));
    atomix.start().join();
    LOGGER.info("Atomix listening at {}:{}", localNode.endpoint().host().getHostAddress(), localNode.endpoint().port());
    ManagedRestService rest = RestService.builder().withAtomix(atomix).withEndpoint(Endpoint.from(localNode.endpoint().host().getHostAddress(), httpPort)).build();
    rest.start().join();
    LOGGER.info("HTTP server listening at {}:{}", localNode.endpoint().host().getHostAddress(), httpPort);
    synchronized (Atomix.class) {
        while (atomix.isRunning()) {
            Atomix.class.wait();
        }
    }
}
Also used : Atomix(io.atomix.core.Atomix) Argument(net.sourceforge.argparse4j.inf.Argument) Node(io.atomix.cluster.Node) ArgumentParser(net.sourceforge.argparse4j.inf.ArgumentParser) Namespace(net.sourceforge.argparse4j.inf.Namespace) ArgumentType(net.sourceforge.argparse4j.inf.ArgumentType) Endpoint(io.atomix.messaging.Endpoint) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException) ManagedRestService(io.atomix.rest.ManagedRestService) File(java.io.File)

Aggregations

Node (io.atomix.cluster.Node)1 Atomix (io.atomix.core.Atomix)1 Endpoint (io.atomix.messaging.Endpoint)1 ManagedRestService (io.atomix.rest.ManagedRestService)1 File (java.io.File)1 Argument (net.sourceforge.argparse4j.inf.Argument)1 ArgumentParser (net.sourceforge.argparse4j.inf.ArgumentParser)1 ArgumentParserException (net.sourceforge.argparse4j.inf.ArgumentParserException)1 ArgumentType (net.sourceforge.argparse4j.inf.ArgumentType)1 Namespace (net.sourceforge.argparse4j.inf.Namespace)1