activej / activej

ActiveJ is an alternative Java platform built from the ground up. ActiveJ redefines core, web and high-load programming in Java, providing simplicity, maximum performance and scalability
https://activej.io
Apache License 2.0
880 stars 72 forks source link

Launcher Command Line and Arguments Description #13

Closed sirinath closed 4 years ago

sirinath commented 4 years ago

What are the arguments passed to a launcher?

Can the description of this be added to the documentation?

Also, how do you override the number of workers on a multi-threaded launcher to a specific number or the numbers of cores on the system? Can this also be added to documentation?

eduard-vasinskyi commented 4 years ago

Hello, @sirinath If you use a custom Launcher, you can pass any arguments that you want. (By calling Launcher's launch(String[] args) method) All of our common launchers just pass arguments from public static void main(String[] args) method. You can obtain passed arguments in your application via dependency injection by requesting an instance by the key Key.of(String[].class, Args.class) or by accessing Launcher's protected field args. You can read more about Launcher here

There are multiple ways to specify custom number of workers. You can create a custom launcher and configure it as you like. Or you can override Launcher's

protected Module getOverrideModule()

method and provide your own pre-configured WorkerPool Here is the example:

    @Override
    protected Module getOverrideModule() {
        int numberOfProcessors = Runtime.getRuntime().availableProcessors();
        return ModuleBuilder.create()
                .bind(WorkerPool.class)
                .to(pools -> pools.createPool(numberOfProcessors), WorkerPools.class)
                .build();
    }

If you have any problems with changing number of workers, please let me know.

sirinath commented 4 years ago

Many thanks for the reply.

What are the commands (args) to be passed to the standard launchers in: https://github.com/activej/activej/tree/master/launchers. E.g. MultithreadedHttpServerLauncher, HttpServerLauncher, etc.

eduard-vasinskyi commented 4 years ago

@sirinath, all of the arguments that are passed to the main method will be passed to MultithreadedHttpServerLauncher and HttpServerLauncher. Launcher itself does not use these arguments for configuration. But instead it provides an easy way to access program arguments in your code either via dependency injection or by accessing Launcher's protected field args.

Here is a small snippet that demonstrates how to do just that:

package io.activej.launchers.http;

import io.activej.http.AsyncServlet;
import io.activej.http.HttpResponse;
import io.activej.inject.annotation.Provides;
import io.activej.launcher.annotation.Args;

import java.util.Arrays;

public final class MyLauncher extends HttpServerLauncher {

    @Provides
    AsyncServlet servlet(@Args String[] arguments) {
        return request -> HttpResponse.ok200()
                .withPlainText("Arguments passed to launcher: " + Arrays.toString(arguments));
    }

    @Override
    protected void run() throws Exception {
        logger.info("Arguments passed to launcher: " + Arrays.toString(this.args));
        super.run();
    }

    public static void main(String[] args) throws Exception {
        new MyLauncher().launch(args);
    }
}
eduard-vasinskyi commented 3 years ago

We have added a standalone example example that demonstrates how program arguments may be accessed.