0


I'm implementing a JAX-RS Service in an Application Server using Vert.x to store data in combination with Vert.x Pg pool. Storing data is quite straightforward as there's nothing to return to the client. However when it comes to a HTTP GET Request it becomes more complex. For example:

public List<Data> findAll() {
    List<Data> list = new ArrayList<>();
    pool.query("SELECT * FROM DATA", ar -> {
        if (ar.succeeded()) {

            RowSet rows = ar.result();

            for (Row row : rows) {
                list.add(new Data(row.getString(0), row.getString(1)));

            }
        } else {
            System.out.println("Failure: " + ar.cause().getMessage());
        }
    });

  return list;
}

So, as you can see, being "ar" a io.vertx.core.AsyncResult it returns immediately, with an empty ArrayList. I understand I should return an AsyncResult or a subclass of it, just I don't have an idea how could the client get the List of objects, when the query completed. Any idea / example? Thanks!

Carla
  • 3,064
  • 8
  • 36
  • 65

1 Answers1

3

To create JAX-RS applications with the Reactive Pg Client, I would recommend to use Quarkus.

You will get a PgPool provided by Quarkus that has a special API using the JDK CompletionStage. Then in Quarkus your JAX-RS methods may return a CompletionStage.

Your method would look like:

public CompletionStage<List<Data>> findAll() {
    pool.query("SELECT * FROM DATA")
        .thenApply(rows -> {
            List<Data> list = new ArrayList<>();
            for (Row row : rows) {
                list.add(new Data(row.getString(0), row.getString(1)));
            }
            return list;
    });
}

Disclaimer: I work for Red Hat, I'm a Vert.x core team member and Quarkus contributor.

tsegismont
  • 8,591
  • 1
  • 17
  • 27
  • Thank you very much for your answer. At the moment, I'm deploying the example on WildFly, just wondering why the above code compiles on Quarkus and not on WildFly. The error I get is "cannot resolve the method query(String)" as it's expected to use a (String,Handler) for the query method. Do you know if the above code can be refactored to use also an Handler? Thanks! – Carla Oct 01 '19 at 12:45
  • 1
    The code above compiles on Quarkus because it uses the [Smallrye Vert.x clients](https://github.com/smallrye/smallrye-reactive-utils/tree/master/vertx-axle-clients). On Wildfly I guess you added the raw Vert.x Reactive Pg Client JAR to your WAR libraries. – tsegismont Oct 01 '19 at 13:23
  • I would recommend not to use the callback based API when building JAX-RS applications. The two programming model simply do not match. – tsegismont Oct 01 '19 at 13:25