DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Ratpack: Event-Driven Framework Base on Netty

Ratpack: Event-Driven Framework Base on Netty

Check out this awesome summary about Ratpack — codeblocks included!

Rafael Salerno user avatar by
Rafael Salerno
·
Sep. 18, 16 · Opinion
Like (10)
Save
Tweet
Share
8.19K Views

Join the DZone community and get the full member experience.

Join For Free

Ratpack is a set of Java libraries that facilitate fast, efficient, evolvable and well-tested HTTP applications. It is built on the highly performant and efficient Netty event-driven networking engine. 

Netty provides the infrastructure for non-blocking networking but doesn’t help much in the way of web application structure. 

It provides an asynchronous API for working with network data (including HTTP).

Some Key Points

  • Ratpack provides applications with a Netty-based, non-blocking HTTP client
  • Can it be used to integrate with external RESTful services
  • Utilizes the same event loop as the server, so resources are appropriately utilized
  • Provides a robust API for programmatically crafting requests
  • Environment-derivable configuration follows the principle of the Twelve-Factor App Great Support for building MICROSERVICES!!
  • NetflixOSS Hystrix support, via the ratpack- hystrix module
  • Calls to remote services may be made fault tolerant
  • Ability to stream Hystrix metrics to the Hystrix Dashboard Great Support for building MICROSERVICES!!
  • Ratpack’s Promise API is an implementation of Reactive Streams Specification
  • The ratpack-rxjava module provides a bridge between a Ratpack Promise and an RxJava Observable
  • The ratpack-reactor module allows data to be processed using Project Reactor Great Support for REACTIVE PROGRAMMING

Ratpack is purely a runtime.

To build Ratpack applications, you can use any JVM build tool. The Ratpack project provides specific support for Gradle through plugins, but any could be used.

Ratpack’s Goals

  1. To be fast, scalable, and efficient
  2. To allow applications to evolve in complexity without compromise
  3. To leverage the benefits of non-blocking programming and reduce the costs
  4. To be flexible and unopinionated when it comes to integrating other tools and libraries
  5. To allow applications to be easily and thoroughly tested

Ratpacks’s Goals Are Not:

  1. To be a fully integrated, “full stack” solution
  2. Provide every feature you might need in a neat box
  3. To provide an architecture or framework for “business logic”

When to Use Ratpack?

  • Micro-services
  • High-throughput apps
  • Lightweight apps
  • Cloud Deployments

Architecture

Ratpack is strongly typed. Beyond being implemented in Java, a strongly typed language, its API embraces types.

Ratpack at its core is an event-based (i.e. non-blocking) HTTP IO engine, and an API that makes it easy to structure response logic. Being non-blocking imposes a different style of API than “traditional” blocking Java APIs in that the API must be asynchronous.

Ratpack aims to significantly simplify this style of programming for HTTP applications. It provides support for structuring asynchronous code, and uses an innovative approach for structuring request processing into a self-building, asynchronously traversed, graph of functions .

The RatpackServer type is the Ratpack entry point. You write your own main class that uses this API to launch the application.

The port method allows setting the port used to connect to the server. If not configured, the default value is 5050.

Ratpack is designed for “asynchronous” and “non-blocking” request processing. Its internal IO (e.g. HTTP request and response transfer) is all performed in a non-blocking fashion. This approach yields higher throughput, lower resource usage, and importantly, more predictable behavior under load. 

This programming model has become increasingly popular of late due to the Node.js platform. Ratpack is built on the same non-blocking, event driven, model as Node.js.

Ratpack Is Fundamentally Asynchronous in Two Key Ways

  1. HTTP IO is event driven / non-blocking
  2. Request handling is organized as a pipeline of asynchronous functions

The HTTP IO being event driven is largely transparent when using Ratpack. Netty just does its thing.

Embedding Ratpack in a Spring Boot Application

The ratpack-spring-boot extension  provides integration with Spring Boot. There are two main features in this library: one allows a Ratpack server registry to be created from a Spring ApplicationContext , and the other allows Ratpack itself to be embedded in a Spring Boot application (making the ApplicationContext automatically part of the server registry).

As an alternative to embedding Spring (as a Registry) in a Ratpack application, you can do the opposite: embed Ratpack as a server in Spring Boot, providing a nice alternative to the Servlet containers that Spring Boot supports. The core of the feature set is an annotation @EnableRatpack which  you add to a Spring configuration class in order to start Ratpack. 

TIP: Ratpack will register handlers automatically for static content in the classpath under “/public” or “/static” (just like a regular Spring Boot application).

If Ratpack is embedded in a Spring application it can be helpful to re-use existing Guice modules, e.g. for template rendering. To do this just include  a @Bean of type Module. For example with the ThymeleafModule for Thymeleaf support

Basic Sample

Image title

Async Sample:

Image title

SpringBoot Sample

Image title

SpringBoot with Thymeleaf Sample

Image title

More details in my GitHub: My Sandbox

Here are some code samples.

What does Ratpack give you over that Vert.x? 

  1. Templating - fully async rendering, and statically compiled using Groovy 2.1 
  2. Error handling (i.e. 500 pages) 
  3. Not found handling (i.e. 404 pages)
  4. Routing - Vert.x already has this, but Ratpack’s is integrated with 404 and 500 handling (and runtime reloadable)
  5. Static file serving - Goes above what Vert.x gives you and support HTTP caching headers
  6. Session support
  7. Runtime reloading - for routes, and if you’re using the Gradle plugin for all of your application code
  8. Higher level abstractions - for example Request and Response
  9. Build tool (Gradle)

Here is a good sample from ratpack.io.

Spring Framework application Netty (software) Spring Boot Framework API

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • PHP vs React
  • Cloud-Native Application Networking
  • Best Practices for Writing Clean and Maintainable Code
  • What Should You Know About Graph Database’s Scalability?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: