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 create 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 three options:#

Create from Maven Archetype#

Simply enter the following command in the terminal:

mvn archetype:generate -DarchetypeGroupId=io.activej -DarchetypeArtifactId=archetype-http -DarchetypeVersion={{site.activej_version}}

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

Working Example#

Clone ActiveJ locally and import it as a Maven project. Check out branch. Before running the example, build the project (Ctrl + F9 for IntelliJ IDEA).

Then open 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

Step-by-step guide#

1. Configure the project#

First, create a folder for application and build an appropriate project structure:

getting-started
└── pom.xml
└── src
└── main
└── java
└── HttpHelloWorldExample.java

Next, you need to integrate ActiveJ in your project. There are two ways to do it. The first option is to add Maven dependency to your project. Simply configure your pom.xml file in the following way:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.activej</groupId>
<artifactId>tutorial-getting-started</artifactId>
<version>4.3</version>
<name>Tutorials : Getting-Started</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.activej</groupId>
<artifactId>activej-launchers-http</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>HttpHelloWorldExample</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>HttpHelloWorldExample</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

The second option is to clone ActiveJ from GitHub, import it as a Maven project, check out branch v4.3 and run the following script in project directory:

./install.sh

Next, configure your pom.xml file like in the example above. Make sure that your project SDK is set to 1.8+.

2. Write HttpHelloWorldExample class#

After you integrate ActiveJ in your project in one of the suggested ways, write down the following code to HttpHelloWorldExample.java:

public final class HttpHelloWorldExample extends HttpServerLauncher {
@Provides
AsyncServlet servlet() {
return request -> HttpResponse.ok200().withPlainText("Hello World");
}
public static void main(String[] args) throws Exception {
Launcher launcher = new HttpHelloWorldExample();
launcher.launch(args);
}
}

First, we extend HttpHelloWorldExample from HttpServerLauncher 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.

Next, we provide an AsyncServlet which receives HttpRequests from clients, creates HttpResponses and sends them. @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.

Finally, we define main method and launch our server with launch method. This method injects dependencies, starts the application, runs it and stops it when needed.

3. 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 processed by the server. Congratulations, you've just created 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.