eclipse-ee4j / eclipse-ee4j/jersey

Return CompletableFuture from ApplicationHandler.apply() instead of Future

Open
#3,173 5 comments 0 reactions 0 assignees View on GitHub
Component: core Priority: Major Type: Improvement
Dominant language
Java
Stars
730
Forks
382
PR merge metrics
No merged PRs in 30d

Description

It would be great if ApplicationHandler.apply(x, y) could return CompletableFuture instead of Future.

The problem with Future is that there is no way for client asynchronously getting result without blocking the thread. It's highly ineffective in high-load server.

CompletableFuture was instroduced in JDK 1.8\. If dependency on 1.8 is blocker we could use vanilla interface such as

```
public interface CompletionCallback {
public void onComplete(ContainerResponse response);
}
```

and add ApplicationHandler.setCompletionHandler(CompletionCallback callback).

Currently I have to use ugly hack as follows

```
class OutputStreamCompletion extends OutputStream {

final private OutputStream stream;
final private Promise> promise;

private Future javaFuture;

public OutputStreamCompletion(@NotNull OutputStream stream, @NotNull Promise> promise) {
this.stream = stream;
this.promise = promise;
}

@Override
public void write(int b) throws IOException {
stream.write(b);
}

@Override
public void write(@NotNull byte b[]) throws IOException {
stream.write(b);
}

@Override
public void write(@NotNull byte b[], int off, int len) throws IOException {
stream.write(b, off, len);
}

@Override
public void flush() throws IOException {
stream.flush();
}

@Override
public void close() throws IOException {
stream.close();
promise.success(javaFuture);
}

public void setJavaFuture(@NotNull Future future) {
this.javaFuture = future;
}
}
```
#### Affected Versions
[2.18, 2.19]

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.