ActiveJ | 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:
- ActiveJ Inject
To proceed with this guide you have three options:
Create from Maven Archetype
Simply enter the following command in the terminal:
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.
Clone ActiveJ locally and import it as a Maven project. Check out branch v4.2. 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.
1. Configure the project
First, create a folder for application and build an appropriate project structure:
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:
The second option is to clone ActiveJ from GitHub, import it as a Maven project, check out branch v4.2 and run the following script in project directory:
Next, configure your
pom.xml file like in the example above.
Make sure that your project SDK is set to 1.8+.
After you integrate ActiveJ in your project in one of the suggested ways, write down the following code to
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
You will receive a
Hello World! message processed by the server. Congratulations, you’ve just created your first
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.