Processing Messages in Order With CompletableFuture

Processing Messages in Order With CompletableFuture

Processing Messages in Order With CompletableFuture. We receive messages from different sources like JMS, AMQP, EventBus and try to process them quickly. When Processing Is the Main CompletableFuture. In the microservices world, we receive messages from different sources like JMS, AMQP, EventBus and try to process them as quickly as possible.

We receive messages from different sources like JMS, AMQP, EventBus and try to process them quickly. Multithreading with Thread Pool can help us to do that.

When Performance Is the Main Consideration

In the microservices world, we receive messages from different sources like JMS, AMQP, EventBus and try to process them as quickly as possible. Multithreading with Thread Pool can help us to do that.

But if we want to treat messages of the same categories (same users, same products...) in the order we receive them, it could get more complicated.

When Order Is Necessary

This sample code shows that an ExecutorThreadPool does not treat messages in the right order for categories 1 and 2:

Java

public class Main {
    public static final int NB_MSG = 5;

    public static final int TASK_DURATION = 500;

    public static void main(String[] args) throws InterruptedException {

        ThreadPoolNotOrdered threadPool = new ThreadPoolNotOrdered();

        new Main().executeInThreadPool(threadPool);

        Thread.sleep(NB_MSG * TASK_DURATION + 1_000);

        threadPool.shutdown();

    }

    private void executeInThreadPool(ThreadPool threadPool) {

        for (int i = 0; i < NB_MSG; i++) {

            executeForUser(threadPool, "CATEGORY_1", i);

            executeForUser(threadPool, "CATEGORY_2", i);

        }

    }

    private void executeForUser(ThreadPool threadPool, String userUuid, int i) {

        Message message = new Message(userUuid, i);

        threadPool.execute(message, () -> {

            System.out.println("Start task  => " + message);

            try {

                Thread.sleep(TASK_DURATION);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            System.out.println("End of task => " + message);

        });

    }

}

java microservices microservice java microservices

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Going Beyond Java 8: Local Variable Type Inference (var) - DZone Java

In this article, we will talk about the most important new feature introduced with Java 10, officially called local variable type inference. An extremely important function in java. You will regret skipping this article.

Lightweight Parallel Tasks in Java Microservices

BascomTask lightweight in-process task orchestration helps reign in the complexities involved with processing data from multiple sources. Lightweight Parallel Tasks in Java Microservices. The Importance of Lightweight Tasks.

Java Online Training | Java Online Course | ITGuru

Our Java Online Training provide you to learn about Java programming and its different features with realty. Our Java Online Course includes live sessions, live projects

Spring Boot and Java 16 Records

In this article, we will discuss Java 16's newest feature, Records. Then we will apply this knowledge and use it in conjunction with a Spring Boot application. An extremely important function in java. You will regret skipping this article.