Skip to main content

Using React in ActiveJ projects

In this example we will integrate React in an ActiveJ project. You can find full example sources on GitHub. Here we will consider using HttpServerLauncher and AsyncServlet to set up the server that processes the requests. See how ActiveJ makes this process extremely simple.

Creating launcher#

SimpleApplicationLauncher extends HttpServerLauncher. HttpServerLauncher superclass takes care of setting up all the required configurations for the HTTP server:

import io.activej.http.AsyncServlet;
import io.activej.http.StaticServlet;
import io.activej.inject.annotation.Provides;
import io.activej.launchers.http.HttpServerLauncher;
import java.util.concurrent.Executor;
import static java.util.concurrent.Executors.newSingleThreadExecutor;
//[START EXAMPLE]
public final class SimpleApplicationLauncher extends HttpServerLauncher {
@Provides
Executor executor() {
return newSingleThreadExecutor();
}
@Provides
AsyncServlet servlet(Executor executor) {
return StaticServlet.ofClassPath(executor, "build")
.withIndexHtml();
}
public static void main(String[] args) throws Exception {
SimpleApplicationLauncher launcher = new SimpleApplicationLauncher();
launcher.launch(args);
}
}
//[END EXAMPLE]

First, we provide an executor for the AsyncServlet. Then, we provide an AsyncServlet to open the index.html of the provided path. Finally, we write down main() method to launch SimpleApplicationLauncher. And that's it, no additional configurations are required. Simple and streamlined, isn't it?

Running the application#

If you want to run the example, clone ActiveJ and import it as a Maven project. Check out branch v4.3. Before running the example, build the project (Ctrl + F9 for IntelliJ IDEA).

Then, run the following command in activej/examples/tutorials/react-integration/front

npm i
npm run-script build

Open SimpleApplicationLauncher class and run its main() method. Then open your favourite browser and go to localhost:8080