Consume an async api (with callback) from sync endpoint using vert.x

Overview

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 does some operations and makes a rest call request to B. B does some operations of it's own and gets back with a response to A. A receives the response from B, does some operations and sends response back to the client.

What if the api provided by B is asynchronous in nature?

For problem purpose, let's now give B some defined characteristics-

  1. Expects caller's callbackUrl in request body
  2. Upon receiving request from caller, performs some basic validtions of the request body.
  3. If the validation is successful, returns an acknowledgement response to the caller
  4. After that, it does the actual job in asynchronous manner
  5. Upon completion of the job, it sends the response in caller's callbackUrl (passed during original request)

This complicates our previously mentioned trivial scenario. A will now only get the acknowledgement response in the initial rest call to B. It will receive the actual callback response in it's callbackUrl.

If we are to keep the behavior / user journey unchanged in client side (makes request to A and expects actual response) - this requires a level of engineering.

This project aims to solve the problem statement using vert.x and it's built in event bus.

alt text

Summary of Approach

Note: vertx-consumer-sync module replicates A, vertx-producer-async module replicates B

To add certain level of complexity, B service provides CRUD service based APIs on the entity class POST. MongoDB has been used as the underlying database. When client makes request to A, A then makes request to B. Upon making a request to B, A starts a timer of 29 seconds, within which both the acknowledgement and callback must reach A.

  • In a happy scenario - B receives request from A, validates the request. Validation is successful. B sends acknowledgement response back to A. It then performs some MongoDB operation and sends the response in A's callbackUrl. This whole process is completed with 29 second. A sends the final response back to client.
  • Not so happy scenario (1) - B receives request from A, validates the request. Validation fails. Sends failure acknowledgement response back to A. A sends the final failure message back to client.
  • Not so happy scenario (2) - B receives request from A, validates the request. Validation is successful. B sends acknowledgement response back to A. It then performs some MongoDB operations and sends the response in A's callbackUrl. The whole process exceeds the limit of 29 seconds. A sends timeout response back to client.

How to Run

Pre-requisites

  1. Docker
  2. Java 11

Steps

  1. Open terminal. Start the docker containers. Write docker-compose up -d
  2. Open terminal. Start verx-consumer-sync (service A)
cd vertx-consumer-sync
mvn clean package
java -jar target/*-fat.jar
  1. Similarly, start vertx-producer-async (service B)
cd ../vertx-producer-async
mvn clean package
java -jar target/*-fat.jar
  1. Open terminal. Insert 100 sample posts in our mongodb collection for test purpose (thanks to jsonplaceholder)
curl --location --request POST 'localhost:9081/addBulkPosts'

Testing

CURL Commands

  • Find all Posts

curl --location --request GET 'localhost:9080/posts'

  • Get Post by id

curl --location --request GET 'localhost:9080/posts/1'

  • Create new Post
curl --location --request POST 'localhost:9080/posts' \
--header 'Content-Type: application/json' \
--data-raw '{
    "userId": 1,
    "id": 101,
    "title": "Lorem Ipsum Title",
    "body": "Lorem Ipsum Body"
}'
  • Update Post
curl --location --request PUT 'localhost:9080/posts' \
--header 'Content-Type: application/json' \
--data-raw '{
    "userId": 1,
    "id": 101,
    "title": "Lorem Ipsum Title - NEW",
    "body": "Lorem Ipsum Body - NEW"
}'
  • Delete Post by id

curl --location --request DELETE 'localhost:9080/posts/101'

You might also like...

This is a Java-API to controll the Lights from Phillips Hue

This is a Java-API to controll the Lights from Phillips Hue

LightControllerAPI This is an easy to use LightControllerAPI in for Java to control Lights from PhillipsHue. How to get started Gradle (Default): repo

Apr 9, 2022

Automation Tests (REST-API with REST-ASSURED examples)

Automation Tests (REST-API with REST-ASSURED examples) Technology Stack IDEA Java Junit5 Gradle Selenide Allure Jenkins Rest-Assured See details: src/

Apr 11, 2022

REST API for Apache Spark on K8S

Lighter Lighter is an opensource application for interacting with Apache Spark on Kubernetes or Apache Hadoop YARN. It is hevily inspired by Apache Li

Jan 5, 2023

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

An e-commerce automation project of Selenium TestNG using Page Object Model

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

Aug 4, 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
Owner
Tahniat Ashraf Priyam
Tahniat Ashraf Priyam
Async-Await support for Java

Java Async-Await Async-Await support for Java CompletionStage. import com.augustnagro.jaa.AsyncContext; import static com.augustnagro.jaa.Async.async;

August Nagro 82 Jan 6, 2023
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
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
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
🔌 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
completely ridiculous API (crAPI)

crAPI completely ridiculous API (crAPI) will help you to understand the ten most critical API security risks. crAPI is vulnerable by design, but you'l

OWASP 545 Jan 8, 2023
Playwright is a Java library to automate Chromium, Firefox and WebKit with a single API.

Playwright is a Java library to automate Chromium, Firefox and WebKit with a single API. Playwright is built to enable cross-browser web automation that is ever-green, capable, reliable and fast.

Microsoft 634 Jan 8, 2023
A template for Spring Boot REST API tested with JUnit 5 and Cucumber 6

demo-bdd Un template Spring Boot pour lancer un BDD/ATDD avec Cucumber 6 et JUnit 5. Maven et le JDK 17 seront nécessaires. Exécuter les tests Le proj

Rui Lopes 4 Jul 19, 2022
API-автотесты для Reqres с использованием библиотеки REST Assured

API-автотесты для Reqres Покрытый функционал Разработаны автотесты на API. API Запросы GET, POST, PUT, PATCH и DELETE Отображение statusCode и body в

Karina Gordienko 2 Jan 31, 2022