Reputation: 874
I'm trying to learn akka by making simple example that queries urls. I was trying to use example from Akka HTTP official docs, but "surprisingly" official doc is outdated.
I was trying to make use of this answer here on StackOverflow, but again - it's referring to outdated API.
My code is:
import java.util.concurrent.CompletionStage;
import static akka.pattern.Patterns.pipe;
import akka.actor.AbstractActor;
import akka.http.javadsl.Http;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.HttpResponse;
import akka.japi.pf.FI.UnitApply;
import akka.japi.pf.ReceiveBuilder;
import akka.stream.ActorMaterializer;
import akka.stream.Materializer;
import scala.concurrent.ExecutionContextExecutor;
public class RequestForPageActor extends AbstractActor {
final Http http = Http.get(context().system());
final ExecutionContextExecutor dispatcher = context().dispatcher();
final Materializer materializer = ActorMaterializer.create(context());
@Override
public Receive createReceive() {
return receiveBuilder().match(RequestForPage.class, onRequestForPage()).build();
// TODO: Handle page response
}
private UnitApply<RequestForPage> onRequestForPage() {
return request -> {
pipe(fetch(request.getUrl()), dispatcher).to(self());
};
}
CompletionStage<HttpResponse> fetch(String url) {
return http.singleRequest(HttpRequest.create(url), materializer);
}
}
The problem is that API has changed. And now compiler's complaint is: The method pipe(Future<T>, ExecutionContext) in the type Patterns is not applicable for the arguments (CompletionStage<HttpResponse>, ExecutionContextExecutor)
Question: So, how do I perform this simple task of querying url and processing results?
Upvotes: 0
Views: 455
Reputation: 9023
The documentation is not outdated. You're just importing the wrong static method.
Replace
import static akka.pattern.Patterns.pipe;
with
import static akka.pattern.PatternsCS.pipe;
PatternsCS
contains a bunch of patterns implementations which work with CompletionStage
instead Future
. A few usage examples can be found in this docs page.
Upvotes: 2