Ratpack is a set of libraries for writing fast, efficient, evolvable and well tested HTTP applications written in Java 8 or any alternative JVM language that plays well with Java, such as Groovy.
The session will start with an introduction about Ratpack, and what makes it different from other popular frameworks like Grails or Vert.x, to name a few. Then, it will cover the fundamentals of the async programming model used in Ratpack applications, as well as other core concepts like handlers and the registry. Also, some of of the core modules ("plug-ins") will be described. Finally, it will cover how to test Ratpack applications using Groovy and Spock.
2. @alvaro_sanchez
About me
● Passionate Software Developer.
○ Living in Madrid, Spain.
● Working professionally in Java since 2001.
○ BEA Systems, Sun Microsystems.
○ Groovy fanboy since 2007.
● Open source contributor: Grails, Ratpack, ..
● Speaker at JavaZone, GeeCON, JavaLand,
Codemotion...
3. @alvaro_sanchez
About
● We build:
● Consumer loan systems.
● Lending platform.
● Payment systems.
● Personal budgeting.
● Anti-fraud and risk system.
● We use:
● Spring Boot.
● Gradle.
● Java 8.
● Groovy.
● Jenkins, Ansible, ...
● We hire the best talent:
● In Warsaw, Prague, Riga, Vilnius and London.
● At http://www.4financeit.com.
13. @alvaro_sanchez
Introduction
● Ratpack is:
○ A set of Java 8 libraries…
■ … lightweight, un-opinionated, Netty-based...
■ … for writing non-blocking HTTP applications…
■ … focused on performance and efficiency…
■ … with an emphasis on testability..
■ … embracing Java 8 or Groovy as programming
languages.
14. @alvaro_sanchez
When to use Ratpack?
● For microservices.
● For high performance applications.
● For lightweight services (no container,
batteries included).
15. If you are writing
microservices, why
do you use
macroframeworks?
16. @alvaro_sanchez
Ratpack is developer friendly
• Starts up in milliseconds.
• Development-time reloading (in ms)
with Gradle.
• First-class support for functional
testing.
19. @alvaro_sanchez
Handlers
● An incoming request is passed through
the handler chain.
● A handler can:
○ Respond to the request.
○ Delegate to the next handler in the chain.
○ Insert more handlers in the chain and
delegate to them.
20. @alvaro_sanchez
Handlers
● Each handler is given a Context instance.
○ To interact with the request / response
objects.
○ To access the registry.
○ Etc.
● Note that Ratpack is not based on the
Servlet API.
23. @alvaro_sanchez
The Context
● The Context provides:
○ Access to the HTTP Request and Response
objects.
○ Interaction with other handlers through
next() and insert() methods.
○ Access to objects in the registry.
24. @alvaro_sanchez
The registry
● The context is also a Registry of
objects.
● Handlers can store objects into the
context for use by downstream handlers.
27. @alvaro_sanchez
Ratpack modules
● Ratpack can integrate with Google Guice
for:
○ Dependency injection for services, etc.
○ Officially supported modules.
○ Reusing your own components as your
modules.
32. @alvaro_sanchez
The classic approach
● A thread-per-request.
○ There is a relatively large thread pool.
○ Each request is assigned a thread from the pool.
○ If the request handling code performs blocking I/O,
the thread sleeps until the result is received.
● Overhead: thread management,
memory consumption.
33. @alvaro_sanchez
Ratpack’s value proposition
• HTTP IO is event-driven (non blocking).
• Powered by Netty.
• Handler chain is organised as a pipeline of
asynchronous functions.
• Small compute thread pool.
• Blocking operations are executed in a separate pool.
• Integration with existing libraries like RxJava.
34. @alvaro_sanchez
Async model
• Ratpack guarantees a deterministic
flow.
• Promises are executed in the order they are defined.
• Once the promises are resolved, they are executed in
compute threads.
• Async functions can be composed.
• To avoid the callback hell.