Description
I would like to begin a conversation regarding planning a possible implementation to support Async Request Handling in the avaje-http-javalin-generator
as right now, there is no support for such.
If you attempt to do:
@Get("/async")
public CompletableFuture<MyCustomPojo> asyncEndpoint() {
return CompletableFuture.supplyAsync(() -> {
return new MyCustomPojo("hello", "world");
});
}
then you will be met by a compile time error stating that CompletableFuture is not an allowed return type.
If you try:
@Get("/async")
public void asyncEndpoint(Context ctx) {
ctx.future(() -> {
return CompletableFuture.supplyAsync(() -> {
return new MyCustomPojo("hello", "world");
}).thenAccept(ctx::result);
});
}
then Avaje will write the response and send it off, before the future is even completed.
Now, this does not stop you from having Async Request Handling if you use avaje-http with Javalin, however, you will have to do all of that in Javalin land rather than avaje-http. Great for a temporary solution, but it'd be nice to have support in avaje-http itself.
There are a few ways that Javalin supports async request handling.
1. Context.future(Supplier<CompletableFuture<?>>)
Javalin:
javalin.get("/async", ctx -> {
ctx.future(() -> {
return CompletableFuture.supplyAsync(() -> {
return new MyCustomPojo("hello", "world");
}).thenAccept(ctx::result);
});
});
Avaje Proposal:
@Get("/async")
public CompletableFuture<MyCustomPojo> asyncEndpoint() {
return CompletableFuture.supplyAsync(() -> {
// Pretend this is an expensive operation (eg: IO)
return new MyCustomPojo("hello", "world");
});
}
Although this would support the server defined Async Timeout that Javalin has, it would not be an overridable option for this method.
2. Context.async(ExecutorService executorService, long timeout, Runnable runnable, ThrowingRunnable throwingRunnable)
Javalin:
javalin.get("/async", ctx -> {
ctx.async(
// Obviously, never do this.
Executors.newCachedThreadPool(),
// Timeout in Milliseconds.
10_000L,
// On Timeout
() -> {
// Do something when the request handling times out.
// For example, write a timeout response, or throw a
// timeout exception for handling elsewhere.
},
// Request Handling Runnable
() -> {
// Handle your request here
}
);
});
Avaje Proposal:
I do not have one... How do others think this could be implemented?
We would need to make sure that all possible return types are supported too, just like they are right now without the async support, this includes void returns too.
Any tips, feedback, suggestions, and/or notes about how to start approaching such a feature would be a nice to have as well.