How do we convert a reactive JDBC call in java to a Future in scala?

yalis

See an example here: https://github.com/vietj/reactive-pg-client

// Pool options
PgPoolOptions options = new PgPoolOptions()
  .setPort(5432)
  .setHost("the-host")
  .setDatabase("the-db")
  .setUser("user")
  .setPassword("secret")
  .setMaxSize(5);

// Create the client pool
PgPool client = PgClient.pool(options);

// A simple query
client.query("SELECT * FROM users WHERE id='julien'", ar -> {
  if (ar.succeeded()) {
    PgResult<Row> result = ar.result();
    System.out.println("Got " + result.size() + " results ");
  } else {
    System.out.println("Failure: " + ar.cause().getMessage());
  }

  // Now close the pool
  client.close();
});

How does one get a Future out of this? Ideally, we'd be able to write code along the lines of this:

val f: Future[...] = clientF.query("select ...")
Krzysztof Atłasik

You could try to use promise:

import scala.concurrent._

def fetch(query: String): Future[PgRowSet] = {

  val options = new PgPoolOptions()
      .setPort(5432)
      .setHost("the-host")
      .setDatabase("the-db")
      .setUser("user")
      .setPassword("secret")
      .setMaxSize(5)

  val client = PgClient.pool(options)

  val p = Promise[PgRowSet]()

  client.query(query, ar => {
      if (ar.succeeded) {
        p.success(ar.result()) //resolve promise as sucessful
      } else {
        p.failure(ar.cause()) //reject promise
      }
      client.close
  })

  p.future
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert a Java Future to a Scala Future

Convert scala future to java future

Convert CompletableFuture (Java 8) to Future (Scala)

Scala how to call a future function on hashmap items

scala how to convert future of one type to future of another type

convert Scala Future to Twitter Future

Scala Convert a Variable to Future?

Convert IgniteFuture to Scala Future

JDBC calls wrapped in Scala Future

How do you call a Scala singleton method from Java?

How do you call Scala objects from Java?

How do I call a Java method named the same as a Scala keyword?

How do I convert Java collections into Scala in Cassandra Datastax driver

How do I convert a Java byte array into a Scala byte array?

How do we call this menu in LinkedIn?

Scala - How to call a future function while mapping over a Seq?

How do we convert a direction vector to an angle?

How do we convert jQuery parent() to VanillaJS

How do we convert a string into Array in hive?

How can we call multiple threads in Java?

Spring Webflux - How do I call an reactive endpoint within a WebFilter

SCALA: How I can initiate another future call within callback from future

How to poll with a Future in Scala?

How can I safely do retries on a Scala Future?

How do I wait for a Scala future's onSuccess callback to complete?

In Scala, how do you use the value of the Future before it's used?

How do we do text measurements without a prior call to paintComponent?

What are the differences between a Scala Future and a Java Future

How do you convert a list future to a list to use as a variable not a widget?

TOP Ranking

HotTag

Archive