#19440 replace Scala Future usage with CompletionStage in javadsl
This entails:
* adding akka.pattern.PatternCS.* to enable ask etc. with
CompletionStage
* changing RequestContext to offer an ExecutionContextExecutor for the
CompletionStage.*Async combinators
* splitting up akka.stream.Queue for JavaDSL consistency
This commit is contained in:
parent
396f4370e9
commit
4c72495581
118 changed files with 1646 additions and 1379 deletions
|
|
@ -19,6 +19,8 @@ import javax.net.ssl.SSLContext;
|
|||
import static akka.http.javadsl.ConnectHttp.*;
|
||||
import static akka.http.javadsl.ConnectHttp.toHostHttps;
|
||||
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public class HttpAPIsTest extends JUnitRouteTest {
|
||||
|
||||
|
|
@ -43,7 +45,7 @@ public class HttpAPIsTest extends JUnitRouteTest {
|
|||
http.bindAndHandle(handler, "127.0.0.1", 8080, materializer());
|
||||
http.bindAndHandle(handler, "127.0.0.1", 8080, httpsContext, materializer());
|
||||
|
||||
final Function<HttpRequest, Future<HttpResponse>> handler1 = null;
|
||||
final Function<HttpRequest, CompletionStage<HttpResponse>> handler1 = null;
|
||||
http.bindAndHandleAsync(handler1, "127.0.0.1", 8080, materializer());
|
||||
http.bindAndHandleAsync(handler1, "127.0.0.1", 8080, httpsContext, materializer());
|
||||
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ package akka.http.javadsl.server;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import akka.dispatch.Futures;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import akka.http.javadsl.testkit.*;
|
||||
|
||||
import akka.http.javadsl.marshallers.jackson.Jackson;
|
||||
|
|
@ -52,13 +52,10 @@ public class CompleteTest extends JUnitRouteTest {
|
|||
Handler2<Integer, Integer> slowCalc = new Handler2<Integer, Integer>() {
|
||||
@Override
|
||||
public RouteResult apply(final RequestContext ctx, final Integer x, final Integer y) {
|
||||
return ctx.completeWith(Futures.future(new Callable<RouteResult>() {
|
||||
@Override
|
||||
public RouteResult call() throws Exception {
|
||||
int result = x + y;
|
||||
return ctx.complete(String.format("%d + %d = %d",x, y, result));
|
||||
}
|
||||
}, executionContext()));
|
||||
return ctx.completeWith(CompletableFuture.supplyAsync(() -> {
|
||||
int result = x + y;
|
||||
return ctx.complete(String.format("%d + %d = %d",x, y, result));
|
||||
}, ctx.executionContext()));
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ public class CodingDirectivesTest extends JUnitRouteTest {
|
|||
.assertHeaderExists(ContentEncoding.create(HttpEncodings.DEFLATE));
|
||||
|
||||
ByteString decompressed =
|
||||
Await.result(Coder.Deflate.decode(response.entityBytes(), mat), Duration.apply(3, TimeUnit.SECONDS));
|
||||
Coder.Deflate.decode(response.entityBytes(), mat).toCompletableFuture().get(3, TimeUnit.SECONDS);
|
||||
Assert.assertEquals("tester", decompressed.utf8String());
|
||||
}
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -52,32 +52,19 @@ public class RouteDirectivesTest extends JUnitRouteTest {
|
|||
.withoutSizeLimit()
|
||||
.getDataBytes()
|
||||
.runWith(Sink.<ByteString>head(), ctx.materializer())
|
||||
.map(new Mapper<ByteString, RouteResult>() {
|
||||
@Override
|
||||
public RouteResult apply(ByteString s) {
|
||||
return ctx.complete(s.utf8String());
|
||||
}
|
||||
}, ctx.executionContext()));
|
||||
.thenApplyAsync(s -> ctx.complete(s.utf8String()), ctx.executionContext()));
|
||||
}
|
||||
})),
|
||||
path("limit-5")
|
||||
.route(
|
||||
handleWith(new Function<RequestContext, RouteResult>() {
|
||||
@Override
|
||||
public RouteResult apply(final RequestContext ctx) throws Exception {
|
||||
handleWith(ctx -> {
|
||||
final RequestEntity entity = ctx.request().entity();
|
||||
return ctx.completeWith(
|
||||
entity
|
||||
.withSizeLimit(5)
|
||||
.getDataBytes()
|
||||
.runWith(Sink.<ByteString>head(), ctx.materializer())
|
||||
.map(new Mapper<ByteString, RouteResult>() {
|
||||
@Override
|
||||
public RouteResult apply(ByteString s) {
|
||||
return ctx.complete(s.utf8String());
|
||||
}
|
||||
}, ctx.executionContext()));
|
||||
}
|
||||
.thenApplyAsync(s -> ctx.complete(s.utf8String()), ctx.executionContext()));
|
||||
}))
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -4,9 +4,10 @@
|
|||
|
||||
package akka.http.javadsl.server.values;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
import org.junit.Test;
|
||||
import scala.Option;
|
||||
import scala.concurrent.Future;
|
||||
|
||||
import akka.http.javadsl.server.*;
|
||||
import akka.http.javadsl.model.HttpRequest;
|
||||
|
|
@ -17,7 +18,7 @@ public class HttpBasicAuthenticationTest extends JUnitRouteTest {
|
|||
HttpBasicAuthenticator<String> authenticatedUser =
|
||||
new HttpBasicAuthenticator<String>("test-realm") {
|
||||
@Override
|
||||
public Future<Option<String>> authenticate(BasicCredentials credentials) {
|
||||
public CompletionStage<Optional<String>> authenticate(BasicCredentials credentials) {
|
||||
if (credentials.available() && // no anonymous access
|
||||
credentials.identifier().equals("sina") &&
|
||||
credentials.verify("1234"))
|
||||
|
|
@ -29,7 +30,7 @@ public class HttpBasicAuthenticationTest extends JUnitRouteTest {
|
|||
OAuth2Authenticator<String> authenticatedToken =
|
||||
new OAuth2Authenticator<String>("test-realm") {
|
||||
@Override
|
||||
public Future<Option<String>> authenticate(OAuth2Credentials credentials) {
|
||||
public CompletionStage<Optional<String>> authenticate(OAuth2Credentials credentials) {
|
||||
if (credentials.available() && // no anonymous access
|
||||
credentials.identifier().equals("myToken") &&
|
||||
credentials.verify("myToken"))
|
||||
|
|
|
|||
|
|
@ -13,9 +13,11 @@ import akka.http.javadsl.server.values.Parameters;
|
|||
import akka.http.javadsl.server.values.PathMatchers;
|
||||
import akka.http.javadsl.testkit.JUnitRouteTest;
|
||||
import akka.http.javadsl.testkit.TestRoute;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
import org.junit.Test;
|
||||
import scala.concurrent.ExecutionContext;
|
||||
import scala.concurrent.Future;
|
||||
|
||||
public class HandlerExampleDocTest extends JUnitRouteTest {
|
||||
@Test
|
||||
|
|
@ -236,12 +238,12 @@ public class HandlerExampleDocTest extends JUnitRouteTest {
|
|||
//#async-example-full
|
||||
//#async-service-definition
|
||||
class CalculatorService {
|
||||
public Future<Integer> multiply(final int x, final int y, ExecutionContext ec) {
|
||||
return akka.dispatch.Futures.future(() -> x * y, ec);
|
||||
public CompletionStage<Integer> multiply(final int x, final int y) {
|
||||
return CompletableFuture.supplyAsync(() -> x * y);
|
||||
}
|
||||
|
||||
public Future<Integer> add(final int x, final int y, ExecutionContext ec) {
|
||||
return akka.dispatch.Futures.future(() -> x + y, ec);
|
||||
public CompletionStage<Integer> add(final int x, final int y) {
|
||||
return CompletableFuture.supplyAsync(() -> x + y);
|
||||
}
|
||||
}
|
||||
//#async-service-definition
|
||||
|
|
@ -253,15 +255,10 @@ public class HandlerExampleDocTest extends JUnitRouteTest {
|
|||
//#async-handler-1
|
||||
// would probably be injected or passed at construction time in real code
|
||||
CalculatorService calculatorService = new CalculatorService();
|
||||
public Future<RouteResult> multiplyAsync(final RequestContext ctx, int x, int y) {
|
||||
Future<Integer> result = calculatorService.multiply(x, y, ctx.executionContext());
|
||||
Mapper<Integer, RouteResult> func = new Mapper<Integer, RouteResult>() {
|
||||
@Override
|
||||
public RouteResult apply(Integer product) {
|
||||
return ctx.complete("x * y = " + product);
|
||||
}
|
||||
}; // cannot be written as lambda, unfortunately
|
||||
return result.map(func, ctx.executionContext());
|
||||
public CompletionStage<RouteResult> multiplyAsync(final RequestContext ctx, int x, int y) {
|
||||
CompletionStage<Integer> result = calculatorService.multiply(x, y);
|
||||
return result.thenApplyAsync(product -> ctx.complete("x * y = " + product),
|
||||
ctx.executionContext());
|
||||
}
|
||||
Route multiplyAsyncRoute =
|
||||
path("multiply").route(
|
||||
|
|
@ -271,14 +268,9 @@ public class HandlerExampleDocTest extends JUnitRouteTest {
|
|||
|
||||
//#async-handler-2
|
||||
public RouteResult addAsync(final RequestContext ctx, int x, int y) {
|
||||
Future<Integer> result = calculatorService.add(x, y, ctx.executionContext());
|
||||
Mapper<Integer, RouteResult> func = new Mapper<Integer, RouteResult>() {
|
||||
@Override
|
||||
public RouteResult apply(Integer sum) {
|
||||
return ctx.complete("x + y = " + sum);
|
||||
}
|
||||
}; // cannot be written as lambda, unfortunately
|
||||
return ctx.completeWith(result.map(func, ctx.executionContext()));
|
||||
CompletionStage<Integer> result = calculatorService.add(x, y);
|
||||
return ctx.completeWith(result.thenApplyAsync(sum -> ctx.complete("x + y = " + sum),
|
||||
ctx.executionContext()));
|
||||
}
|
||||
Route addAsyncRoute =
|
||||
path("add").route(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue