0

I study quarkus, mutiny and vert.x. I want to create connections with two databases and fetch data in parallel.

I create two similar verticles (second verticle differs only datasource name, mapper and entity):

@ApplicationScoped
public class DbFruitVerticle extends AbstractVerticle {
    private static final Logger LOGGER = Logger.getLogger(DbFruitVerticle.class);

    @Inject
    @ReactiveDataSource("fruit")
    PgPool pgPoolFruit;


    @Override
    public Uni<Void> asyncStart() {
        LOGGER.info(">>> DbFruitVerticle asyncStart");

        return vertx.eventBus().consumer("runrun").handler(m -> {
                    LOGGER.info(">>> Start Handler");
                    getAll().subscribe()
                            .with(fruit -> LOGGER.info(">>> fruit " + fruit.getId()),
                                    fail -> fail.printStackTrace());
                }
        ).completionHandler();
    }

    public Multi<Fruit> getAll() {
        return pgPoolFruit.query("select f.* from fruit f").execute()
                .onItem().transformToMulti(set -> Multi.createFrom().iterable(set))
                .onItem().transform(this::mapFruit);
    }
// mapper
}

delpoy verticles:

  public void init(@Observes StartupEvent event,
                     Vertx vertx,
                     DbTestVerticle dbTestVerticle,
                     DbFruitVerticle dbFruitVerticle) {
        vertx.deployVerticle(dbTestVerticle).await().indefinitely();
        vertx.deployVerticle(dbFruitVerticle).await().indefinitely();
    }

Invoke process:

eventBus.publish("runrun", "run");

And see log:

2021-12-02 17:02:01,551 INFO  [org.byb.ver.DbFruitVerticle] (vert.x-eventloop-thread-6) >>> fruit 1
2021-12-02 17:02:01,551 INFO  [org.byb.ver.DbFruitVerticle] (vert.x-eventloop-thread-6) >>> fruit 3
2021-12-02 17:02:01,552 INFO  [org.byb.ver.DbFruitVerticle] (vert.x-eventloop-thread-6) >>> fruit 4
2021-12-02 17:02:01,552 INFO  [org.byb.ver.DbFruitVerticle] (vert.x-eventloop-thread-6) >>> fruit 5
2021-12-02 17:02:01,552 INFO  [org.byb.ver.DbFruitVerticle] (vert.x-eventloop-thread-6) >>> fruit 6
2021-12-02 17:02:01,752 INFO  [org.byb.ver.DbTestVerticle] (vert.x-eventloop-thread-5) >>> bill 1
2021-12-02 17:02:01,753 INFO  [org.byb.ver.DbTestVerticle] (vert.x-eventloop-thread-5) >>> bill 2
2021-12-02 17:02:01,753 INFO  [org.byb.ver.DbTestVerticle] (vert.x-eventloop-thread-5) >>> bill 3

It seems what at first thread-6 is processed and then thread-5. Why they are not parallel? How to make them parallel? Are there best practice for similar tasks? Please, help.

  • I'd say they ARE running in parallel. Increase the record counts in your DB to several hundreeds, so that select and processing of data takes more considerable amount of time. With your current setup the furits and bill are processed in less than 1 ms – injecteer Dec 02 '21 at 14:36
  • I add rows to tables (6000 and 8000), seems they ok: ` 2021-12-02 19:14:40,622 INFO [org.byb.ver.DbTestVerticle] (vert.x-eventloop-thread-5) >>> bill 2371 2021-12-02 19:14:40,622 INFO [org.byb.ver.DbTestVerticle] (vert.x-eventloop-thread-5) >>> bill 2372 2021-12-02 19:14:40,620 INFO [org.byb.ver.DbFruitVerticle] (vert.x-eventloop-thread-6) >>> fruit 5678 2021-12-02 19:14:40,622 INFO [org.byb.ver.DbTestVerticle] (vert.x-eventloop-thread-5) >>> bill 2373 2021-12-02 19:14:40,622 INFO [org.byb.ver.DbFruitVerticle] (vert.x-eventloop-thread-6) >>> fruit 5679 ` Thankyou! – Anhelica Dec 02 '21 at 16:17

0 Answers0