Skip to main content

Getting Started

ActiveJ is a minimalistic and boilerplate-free Java platform that is intended to replace Spring, Spark, Micronaut, and other similar solutions. It features a rich stack of technologies with an efficient async programming model and powerful DI library ActiveJ Inject.

ActiveJ consists of several loosely coupled components that complement and empower each other: async core, efficient I/O processing, high-performance Jetty and Netty alternatives, along with handy boot utilities.

In this tutorial we will look at a simple HTTP server that sends a “Hello World!” greeting. With ActiveJ Launchers, particularly HttpServerLauncher subclass, and ActiveJ Inject DI you can write a fully-functional server in around 10 lines of code.

What you will need:

Which ActiveJ technologies will be used:

To proceed with this guide you have two options:

  1. Create the app from Maven archetype
  2. Download and run working example

1. Create from Maven Archetype

Simply enter the following command in the terminal:

mvn archetype:generate -DarchetypeGroupId=io.activej -DarchetypeArtifactId=archetype-http -DarchetypeVersion=6.0-beta2

The project will be automatically generated on your machine. Open WebApp and run its main method. Then open your favourite browser and go to localhost:8080

2. Download and run the example

First, clone ActiveJ locally:

git clone -b examples-6.0-beta2 https://github.com/activej/activej.git

Then open the project in your IDE of choice. Before running the example, build the project (Ctrl + F9 for IntelliJ IDEA).

Navigate to a HttpHelloWorldExample class, which is located at activej/examples/tutorials/getting-started and run its main method. Open your favourite browser and go to localhost:8080

Source code

A source code for a simple web server is quite minimal. It only consists of a single Java class:

public final class HttpHelloWorldExample extends HttpServerLauncher {
@Provides
AsyncServlet servlet() {
return request -> HttpResponse.ok200()
.withPlainText("Hello World")
.toPromise();
}

public static void main(String[] args) throws Exception {
Launcher launcher = new HttpHelloWorldExample();
launcher.launch(args);
}
}

HttpHelloWorldExample extends HttpServerLauncher class to manage application lifecycle. In this example, the only important thing to know about the superclass is that it implements the launch method that starts our server.

We provide an AsyncServlet which receives HttpRequests from clients, creates HttpResponses and sends them back to the client. @Provides annotation means that this method is available for binding as the root HTTP endpoint listener.

To define processing of the received requests, we override AsyncServlet.serve method with a lambda. We create a promise of HttpResponse with code 200 and "Hello World!" body. ActiveJ is fully asynchronous, so our HTTP Servlets are asynchronous too. A simple HttpResponse can be replaced with a Promise of HttpResponse to make it asynchronous.

We also define the main method and launch our server using the launch method. This method injects dependencies, starts the application, runs it and stops it when needed.

Run the app

Now, let's test the application. First, run HttpHelloWorldExample.main, then open your favourite browser and go to localhost:8080 You will receive a Hello World! message from the server. Congratulations, you've just launched your first ActiveJ application!

What's next?

To make ActiveJ more developer-friendly, we've created dozens of tutorials and examples of different scales, representing most of the platform's capabilities. Click "Next" to get to the next tutorial. You can also explore ActiveJ docs first.