Book Image

Play Framework Essentials

By : Julien Richard-Foy
Book Image

Play Framework Essentials

By: Julien Richard-Foy

Overview of this book

Table of Contents (14 chapters)
Play Framework Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Making an action's logic reusable and composable with action builders


This section shows patterns to factor out the common code that is duplicated in your controllers. The resulting action builders can be used to capture transversal concerns such as authentication.

Capturing the logic of actions that use blocking APIs

We saw in Chapter 5, Reactively Handling Long-running Requests, how to specify in which execution context to execute an action communicating with a database using a blocking API based on JDBC. We did this by wrapping the action code in a Future object (a Promise object in Java) and by explicitly specifying which execution context to use to execute them. This resulted in quite bloated code:

import my.execution.context
val myAction = Action.async {
  Future {
    // the action's code actually starts only here!
  }
}

The Java equivalent was the following:

public static Promise<Result> myAction() {
  return Promise.promise(
    () -> {
      // the action's code actually starts...