Java micro-frameworks: Why Jooby?

Edgar Espina
5 min readMar 30, 2017

--

Over the last years the number of new micro-frameworks increased dramatically. The days where we have to choose between two or three frameworks are gone and won’t be back!

Today, we can choose between Pippo, Rapidoid, Sparkjava, Ninjaframework, …, etc.

Some of these might be considered micro-frameworks, while others aren’t. Also, didn’t want to mention Spring Boot and Play Framework because they definitely aren’t.

In this post going to talk about Jooby a scalable, fast and modular micro-framework for the JVM.

What is a micro-framework?

Hard to tell, but some benefits a micro-framework could be:

  • Fast. Fast bootstrap and responses.
  • Easy. To learn, setup, maintain and extend.

Long bootstrap and over-engineering are usually a symptom of bloated library or framework.

Also, I would love to agree that micro-frameworks (in general) are NOT toys. Some people tend to under estimate micro-frameworks without giving them a chance.

Simplicity is goal that every framework developer should follow. Complexity comes from business requirements (problem to solve). It should never come from our technology stack.

Seriously, it is time to build Java web application as quick and easy without compromising scalability and application performance.

Don’t you think?

jooby: do more, more easily

Jooby is a minimally opinionated micro-framework with a modern and powerful HTTP API for building web applications in Java, JavaScript or Kotlin.

These 14 lines give you a beautiful Hello World web application. The route / accepts an optional name parameter and sends back a friendly message to the client:

GET /            => Hello World!
GET /?name=Mundo => Hello Mundo!

There is one point here worth mentioning:

The use of the instance initializer.

{
get("/", () -> "Hello World!")
}

Which means you need an instance to run an application. Here is the exact same application without extending Jooby:

See? No shared/static/global state. This is probably obvious for you but you will be surprised with the number of (micro-)frameworks that make use and abuse of shared state.

programming models

Jooby offers two programming models:

  • Script: based on Java 8 lambdas which is annotation and reflection free. Similar to sinatra, express.js, etc…
  • MVC: based on Java class that depends on annotations and reflection. Similar to Jersey, Spring, etc…

You can choose one or mix them both in one application. It is your call.

The next example uses script routes for HTML templates and mvc routes for business logic (JSON API):

From script routes we access the application services via require method:

Pets pets = require(Pets.class);

From mvc routes we access the application services via @Inject annotation:

@Inject
public Pets(Database db) {
...
}

In both cases, Guice is going to provisioning those services.

The example also uses two modules:

  • Jackson for JSON processing
  • Rocker for type-safe and super fast templates

micro or full-stack

Does a micro-framework setup a connection pool? an ORM? a template engine?

The answer to all these questions is: NO. Why? Because a micro-framework usually does one thing only.

This is partially true for Jooby. It is micro at his core with only three hard dependencies (config, logback and guice). Everything else is pluggable (even the HTTP server).

You can extend and compose your application via modules:

Modules are thin and do a lot of work to bootstrap and configure an external library, but they don’t provide a new level of abstraction nor [do] they provide a custom API to access functionality in that library. Instead they expose the library components as they are (major difference with other Java Web frameworks).

If you think twice, it is a sane and safe approach to provide new features. Users don’t have to learn a new API for using their favorite library, even more users have complete control of the library. This increases developer productivity and simplifies development of modules.

A Module looks like:

The configure callback has three parameter:

  1. env: which give you access to environment where the application runs: dev, prod, etc… and to application core components.
  2. conf: application configuration properties.
  3. binder: Guice binder where you can define and wire application services.

Once defined, you need to install it. Installation is done via use method:

{
use(new MyModule());
}

In similar fashion your module might expose routes too:

void configure(Env env, Config conf, Binder binder) {
Router router = env.router();
router.get("/ping", () -> "pong!");
}

Modules are a key concept in Jooby and help you to progressively build full-stack applications.

productivity

There are plenty of modules ready to use: https://jooby.io/modules. There are modules for connection pool, all kind of datastore, ORMs, ESB, Swagger, Raml, Template Engines, Job Scheduler, Async/Reactive programming among others.

Did you change a Java class? Forget about long restarts, jooby:run quickly restart the application and your changes are immediately available.

There is a LiveReload module in addition to jooby:run. So you don’t ever need to hit the refresh button on your browser.

tests

This is another issue or concern when choosing a micro-framwork. It is a valid concern and not all the micro-frameworks supports unit or integration tests.

Luckily enough, both are covered by Jooby.

Let’s see how to do a unit test against a Hello World application:

@Test
public void unitTest() {
MockRouter router = new MockRouter(new App());
assertEquals("Hello World!", router.get("/"));
}

and the integration test:

@ClassRule
private static JoobyRule bootstrap = new JoobyRule(new MyApp());
@Test
public void integrationTest() {
get("/")
.then()
.assertThat()
.body(equalTo("Hello World!"));
}

code quality

Code quality is the only way to build rock solid software.

We took code quality very serious in Jooby with more than 4000 tests with a code coverage of 99.5%.

See it by yourself: https://coveralls.io/github/jooby-project/jooby

performance

Jooby isn’t only easy to use but also has good/decent performance (compared to some big players).

We invite you to check these two 3rd party benchmarks:

  1. https://www.techempower.com/benchmarks
  2. https://github.com/networknt/microservices-framework-benchmark

I’m going to talk about thread-model in a another post. For now you need to know Jooby uses an event-loop model and your code always runs in a worker thread. What this mean? You can safely block a worker thread and your application will be responsive.

final comments

Jooby makes Java web development fun again. It is simple, expressive and effective.

The number of lines required to build something are usually smaller than in any other Java framework.

At the same time has great performance while keeping you safe by using a worker thread.

We provide all the tools to make you more productive: hotreload, browser live reload, unit/integrations tests and lot of modules.

Not convinced yet?

Checkout our quickstart guide or one of our starter projects.

Do you have questions?

Join our channel and we’ll help you: https://gitter.im/jooby-project/jooby

Try it! You will really enjoy it! I promise!

--

--