George
George

Reputation: 7543

Use play WS 2.5.x from Akka actor

I'm using akka with java and looking for a way to use asynchronous Play WS api from within akka actors.

In play WS 2.4.x the method WSRequest.get() returns an F.Promise which easily can be converted to Scala Future and then piped with akka.pattern.Patterns.pipe to some akka actor in order the http response to be received as an akka message.

In play WS 2.5.x the method WSRequest.get() returns a CompletionStage which I can not figure out how to pipe it to an Akka actor.

So how can I properly use the play WS 2.5.x api from within an Akka actor?

Upvotes: 3

Views: 620

Answers (1)

Andriy Kuba
Andriy Kuba

Reputation: 8263

From the migration guide:

While Play 2.4 was cross compiled against both Scala 2.10 and Scala 2.11, this new release of Play is only available for Scala 2.11. The reason for dropping Scala 2.10 support is that Play has a new library dependency on scala-java8-compat, which is only available for Scala 2.11. This library makes it easy to convert from and to common Scala and Java8 types, and hence it’s valuable to simplify the Play core. Furthermore, you may also find it handy to use in your own Play project. For example, if you need to convert Scala Future instances into Java CompletionStage (or the other way around).

I am pretty sure you can still do it easy:

import static scala.compat.java8.FutureConverters.*;

...

final Promise<String> p = promise();
final Future<String> sf = p.future();
final CompletionStage<String> cs = toJava(sf);
Future<String> sf1 = toScala(cs);

Upvotes: 4

Related Questions