ActiveJ is a minimalistic and boilerplate-free Java platform that is indented to replace Spring, Spark, Micronaut, and other similar solutions. It features full stack of technologies with efficient async programming model and powerful DI library ActiveInject.
ActiveJ consists of several 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 ActiveInject 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:
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 master. 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:
- Add Maven dependency to your project
pom.xmlfile in the following way:
- Clone ActiveJ and install it Another way to integrate ActiveJ is to clone it, import it as a Maven project, check out branch master 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 lambda. As you can
see, we are using Promises here, creating a promise of HttpResponse with code
200 and “Hello World!” body. ActiveJ is fully asynchronous, so our HTTP Servlets are asynchronous too.
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.