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.