How to execute Vertx Future in sequential order

Hantsy

In my sample project, I tried to do some initialization work when the application is started.

  • Java 16
  • Vertx 4.1.0

Check the complete project codes.

log.info("Data initialization is starting...");
        var deleteComments = this.comments.deleteAll().onSuccess(event -> log.info("deleted comments: {}", event));
        var deletePosts = this.posts.deleteAll().onSuccess(event -> log.info("deleted posts: {}", event));
        var deleteUsers = this.authors.deleteAll().onSuccess(event -> log.info("deleted users: {}", event));

        //log.info("deleted rows: authors: {}, comments: {}, posts: {}", authorsDel, commentsDel, postDel);
        var insertData = this.authors.create("user", "[email protected]").onSuccess(
            authorId -> {
                IntStream.range(1, 5)
                    .forEach(
                        i -> {
                            this.posts.create("Dgs post #" + i, "test content of #" + i, PostStatus.DRAFT.name(), authorId).onSuccess(
                                postId -> {

                                    IntStream.range(1, new Random().nextInt(5) + 1)
                                        .forEach(c -> this.comments.create("comment #" + c, postId));
                                }
                            );

                        }
                    );
            }
        );

        var printPosts = this.posts.findAll().onSuccess(p -> log.info("post: {}", p));
        var printComments = this.comments.findAll().onSuccess(p -> log.info("comment: {}", p));
        var printAuthors = this.authors.findAll().onSuccess(p -> log.info("author: {}", p));

        deleteComments
            .flatMap(integer -> deletePosts)
            .flatMap(integer -> deleteUsers)
            .flatMap(integer -> insertData)
            .flatMap( uuid -> printPosts)
            .flatMap(postEntities -> printComments)
            .flatMap(commentEntities -> printAuthors)
            .onSuccess(event -> log.info("done"));
        log.info("done data initialization...");

But it does not work as expected.

  1. There is no a then method like Reactor.
  2. And the Future.result() will return an exception instead of a blocking call(how to make execution in a blocking way, I thought it is a blocking invocation).

I can not find an effective way to execute them in sequential order.

Update:

I changed the codes as suggested.

  this.comments.deleteAll().onSuccess(event -> log.info("deleted comments: {}", event))
            .flatMap(r -> this.posts.deleteAll().onSuccess(event -> log.info("deleted posts: {}", event)))
            .flatMap(r -> this.authors.deleteAll().onSuccess(event -> log.info("deleted users: {}", event)))
            .flatMap(r -> this.authors.create("user", "[email protected]")
                .onSuccess(
                    authorId -> {
                        log.info("inserted user: {}", authorId);
                        IntStream.range(1, 5)
                            .forEach(
                                i -> {
                                    this.posts.create("Dgs post #" + i, "test content of #" + i, PostStatus.DRAFT.name(), authorId).onSuccess(
                                        postId -> {
                                            log.info("inserted post: {}", postId);
                                            IntStream.range(1, new Random().nextInt(5) + 1)
                                                .forEach(c -> this.comments.create("comment #" + c, postId)
                                                    .onSuccess(id -> log.info("inserted comment: {}", id))
                                                );
                                        }
                                    );

                                }
                            );
                    }
                )
            )
            .flatMap(r -> this.posts.findAll().onSuccess(p -> log.info("saved posts: {}", p)))
            .flatMap(r -> this.comments.findAll().onSuccess(p -> log.info("saved comments: {}", p)))
            .flatMap(r -> this.authors.findAll().onSuccess(p -> log.info("saved authors: {}", p)))
            .onSuccess(event -> log.info("done data initialization..."));

And got the following log in the console.

10:20:46.699 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - Data initialization is starting...
10:20:47.768 [vert.x-eventloop-thread-1] INFO  com.example.demo.MainVerticle - HTTP server started on port 8080
10:20:47.768 [vert.x-eventloop-thread-0] INFO  i.v.c.i.l.c.VertxIsolatedDeployer - Succeeded in deploying verticle
10:20:47.918 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - deleted comments: 10
10:20:47.942 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - deleted posts: 4
10:20:47.960 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - deleted users: 1
10:20:48.022 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - inserted user: 260fee93-cf60-408d-a3e9-f7b08ed7545a
10:20:48.047 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - saved posts: []
10:20:48.052 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - inserted post: 57a936e2-9611-43b5-94e2-1b01069cd327
10:20:48.056 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - saved comments: []
10:20:48.067 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - saved authors: [AuthorEntity[id=260fee93-cf60-408d-a3e9-f7b08ed7545a, name=user, [email protected]
m, createdAt=null]]
10:20:48.092 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - done data initialization...
10:20:48.112 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - inserted post: 534b99ae-4847-4cea-84c1-88cea87f20b7
10:20:48.117 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - inserted post: f7dca2eb-0954-4368-b9b6-68a00f6748f2
10:20:48.120 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - inserted post: 02e8f84f-25dd-4d69-b7f1-8c125ce35bc1
10:20:48.131 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - inserted comment: b9bf618a-f579-48f5-96cc-51f962ca041c
10:20:48.134 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - inserted comment: 052cfde8-627d-4cb3-8812-5543673a20ea
10:20:48.143 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - inserted comment: 69a5dc2b-30f8-4008-90a1-e8c724336dcd
10:20:48.149 [vert.x-eventloop-thread-1] INFO  com.example.demo.DataInitializer - inserted comment: a19c2626-1546-4ba0-a663-823df08d836f

How to make sure all futures in the insert block are completed before entering the print result.

Selim

It's most likely because your deleteAll and findAll operations are not lazy. Because of this, they are running as soon as you call them. Instead of doing:

deleteComments
  .flatMap(integer -> deletePosts)
  ...

You want to be doing this:

deleteComments
  .flatMap(integer -> this.posts.deleteAll())
  ...

This will make sure that they will run sequentially. If you want something that's more similar to Reactor, you can take a look at the RxJava3 extensions or mutiny bindings for Vert.x.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I execute array of promises in sequential order?

How to fetch the value out of VertX Future object

How to execute a loop with sequential summation?

compose with vertx for sequential code

Vertx Future does not wait

How to execute sequential base commands in nodejs?

How execute promises in order?

XSLT:How to Maintain Sequential Order of tag in XML

How to rename dictionary keys to follow sequential order?

How do I read images in their sequential order?

How to make multiple API calls in sequential order

How to use RxJS observables in sequential order?

How to run test in sequential order in loadimpact?

How to change files in the sequential order of the loop

How to reshape a dataset in order to make it sequential?

vertx failed future but treated as succeeded()

how to transform Future[Option[(Person, Future[Vector[Order]])]] into Future[Option[(Person, Vector[Order])]]

How long to execute paypal order?

How to execute Thread Groups in order

How to Execute Multiple Queries in a order

Sequential composition for arbitrary number of calls in Vertx with Futures

How to execute dependent, sequential calls and return an array with all responses?

How to compare 2 files having random numbers in non sequential order?

How do i compare alphanumeric characters in non sequential order?

How to add a column containing sequential counts based on the order of another column?

How to detect next task in list, depending on sequential order

How to call the API in sequential order in Angular9 with rxjs?

fping – How to make fping result always sequential in order of command line

How to insert words from a sentence to a list in the correct sequential order