Async-Await support for Vertx using Project Loom

Overview

Vertx-Async-Await

Async-Await support for Vertx using Project Loom.

import static com.augustnagro.vertx.loom.AsyncAwait.async;
import static com.augustnagro.vertx.loom.AsyncAwait.await;

Future<byte[]> buildPdf() {
  return async(() -> {
    
    List<Long> userIds = await(userIdsFromDb());
    
    List<String> userNames = new ArrayList<>(userIds.size());
    for (Long id : userIds) {
      userNames.add(await(userNameFromSomeApi(id)))
    }
    
    byte[] pdf = await(somePdfBuilder(userIds))
  
    System.out.println(userIds);
    return pdf;
  });
}

vs.

Future<byte[]> buildPdf() {
  return userIdsFromDb().flatMap(userIds -> {
    Future<List<String>> userNamesFuture =
      Future.succeededFuture(new ArrayList<>());
  
    for (Long userId : userIds) {
      userNamesFuture = userNamesFuture.flatMap(list -> {
        return userNameFromSomeApi(userId)
          .map(userName -> {
            list.add(userName);
            return list;
          });
      });
    }
  
    return userNamesFuture.flatMap(userNames -> {
      return buildPdf(userNames)
        .onComplete(__ ->
          System.out.println("Generated pdf for user ids: " + userIds)
        );
    });
});

Maven Coordinates

<dependency>
  <groupId>com.augustnagro</groupId>
  <artifactId>vertx-loom</artifactId>
  <version>0.2.2</version>
</dependency>

This library requires a JDK 18 Loom Preview Build and depends on vertx-core v. 4.2.4.

JDK 18 is used instead of the new 19 previews because no IDEs work with 19 yet.

WARNING: this library uses class Continuation, which has recently been made private in the Loom OpenJDK fork. It is likely that Continuation will return later in some form, either with a restricted API or virtual thread schedulers. In newer preview builds, we will use --add-exports to get access.

Docs:

async(Callable<A>) returns Future<A>. Within the provided Callable, you can await Futures and program in an imperative style. Stack traces are also significantly improved in the case of errors.

The execution context remains on the current thread; no new threads, virtual or otherwise, are created by calling async.

Finally, async and await calls can be nested to any depth although recursion is not stack-stafe.

Why Async-Await?

Vertx is great as-is.

But there are some downsides too.

  • Using Futures is harder to read & maintain than simple blocking code.
  • It's hard to debug big Future chains in IDEs
  • Stack traces are poor, especially if the Exception is thrown on a different thread than your current Vertx Context. For example, when pgClient.prepareQuery("SELEC * FROM my_table").execute() fails, any logged stacktrace won't show you where this code is. This is because the postgres client maintains its own pool of threads.

Project Loom solves all three issues. The goal of this project is to combine the performance of Vertx's event loop with the productivity of Loom's synchronous programming model.

Testing Notes:

  • the io.vertx.ext.unit.junit.RunTestOnContext JUnit 4 rule is not working with this. See the simple implementation of asyncTest in this project.
You might also like...

RAT Malware for school project, educational purpose

RAT Malware for school project, educational purpose

CoBat RAT Malware DISCLAIMER: I am not responsible for any effects that occur from this project, this project was intended for educational purposes on

Mar 7, 2022

This is an experiment project I used to learn more about UI Automation with Playwright

Automated Wordle using Playwright (Java) This is an experiment project I used to learn more about UI Automation with Playwright. What's in this reposi

Jan 30, 2022

A collection of bite size examples for using chrome DevTools protocol commands with Selenium Webdriver v4.

selenium-devtools-guide A collection of bite size examples for using chrome DevTools protocol commands with Selenium Webdriver v4. Chrome Devtools Pro

Aug 12, 2021

Ghost Driver is an implementation of the Remote WebDriver Wire protocol, using PhantomJS as back-end

Ghost Driver is an implementation of the Remote WebDriver Wire protocol, using PhantomJS as back-end

Dec 15, 2022

This repository contains examples using TestContainers in a Spring Boot Application

This repository contains examples using TestContainers in a Spring Boot Application

Sample REST CRUD API with Spring Boot, Mysql, JPA and Hibernate Using TestContainer to assure our Integration/API testing Steps to Setup 1. Build and

Nov 29, 2021

BDD framework for automation using Selenium Cucumber and TestNg

BDD framework for automation using Selenium Cucumber and TestNg

Selenium Framework with Cucumber BDD framework for automation using Selenium Cucumber and TestNg The framework has following features Modular Design M

Jan 20, 2022

Ready-to-use UI Test Automation Architecture using Java and Selenium WebDriver.

Selenium Test Automation Boilerplate Ready-to-use UI Test Automation Architecture using Java and Selenium WebDriver. Languages and Frameworks The proj

Dec 26, 2022

This repository includes selenium tests examples using cucumber-jvm framework.

Cucumber Selenium Tests This repository includes cucumber selenium tests examples using wikipedia.org. Run tests To run tests on your local machine, y

Nov 27, 2022

Framework for Mobile test automation using Appium with Java - BDD

Framework for Mobile test automation using Appium with Java - BDD

appium-mobile-automation-framework-bdd Mobile automation framework using appium - BDD 🚀 Quick Start - Appium set up on Windows (Android): Install Jav

Oct 19, 2022
Releases(v0.2.1)
Owner
August Nagro
Java & Scala Software Engineer
August Nagro
make async-await code style available in java just like csharp and es6

JAsync - the async-await pattern of Java 中文版 JAsync implements Async-Await pattern just like es in Java. It allows developers to write asynchronous co

null 124 Dec 26, 2022
Consume an async api (with callback) from sync endpoint using vert.x

vertx-async-to-sync Problem statement Suppose we have two services - A and B. In a trivial and everyday scenario, client makes request to A. A then do

Tahniat Ashraf Priyam 12 Oct 19, 2022
JVM version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.

pact-jvm JVM implementation of the consumer driven contract library pact. From the Ruby Pact website: Define a pact between service consumers and prov

Pact Foundation 962 Dec 31, 2022
This is a Selenium test project for the Champlain PetClinic project

champlain_petclinic_selenium This is a Selenium test project for the Champlain PetClinic project Selenium Login Test Helper class info With the additi

null 3 Oct 31, 2021
A project was created using the API of the TMDB page

TMDB API The project was created using the API of the TMDB page. You can find the description of the functions and their usage at https://developers.t

Atakan Koçyiğit 3 Jan 27, 2022
An e-commerce automation project of Selenium TestNG using Page Object Model

Selenium-POM-TestNG Prerequisites Install jdk 8 or any LTS version Configure JAVA_HOME and GRADLE_HOME Download Allure 2.17.2 and configure environmen

Asif Shahriar 2 Aug 4, 2022
Restful-booker API test automation project using Java and REST Assured.

Restful-booker API Test Automation Restful-booker API is an API playground created by Mark Winteringham for those wanting to learn more about API test

Tahanima Chowdhury 7 Aug 14, 2022
A shitty brainfuck interpreter that does not support comments !

Welcome to BrainfuckInterpreter ?? A shitty brainfuck interpreter that does not support comments ! Use it only if you are determined :D Author ?? Reds

RedsTom 3 Jul 15, 2022
🔌 Simple library to manipulate HTTP requests/responses and capture network logs made by the browser using selenium tests without using any proxies

Simple library to manipulate HTTP requests and responses, capture the network logs made by the browser using selenium tests without using any proxies

Sudharsan Selvaraj 29 Oct 23, 2022