# Async improvements

In JAXRS 2.0, you can build an asynchronous RESTful APIs as the following.

```java
@GET
public void getAsync(final @Suspended AsyncResponse res) {
    res.setTimeoutHandler(
            (ar) -> {
                ar.resume(Response.status(Response.Status.SERVICE_UNAVAILABLE)
                        .entity("Operation timed out --- please try again.").build());
            }
    );
    res.setTimeout(1000, TimeUnit.MILLISECONDS);
    executor.submit(() -> {
        //do long run operations.
        try {
            LOG.log(Level.INFO, " execute long run task in AsyncResource");
            //Thread.sleep(new Random().nextInt(1005));
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            LOG.log(Level.SEVERE, "error :" + ex.getMessage());
        }
        res.resume(Response.ok("asynchronous resource").build());
    });
}
```

Inject `@Suspended AsyncResponse res` as the method parameter, call `res.resume()` to return response result asynchronously.

Or combined with EJB `@Asynchronous`.

```java
@GET
@Asynchronous
public void getAsync(final @Suspended AsyncResponse res) {

    //perform long run operations.
    try {
        LOG.log(Level.INFO, " execute long run task in EjbAsyncResource");
        Thread.sleep(500);
    } catch (InterruptedException ex) {
        LOG.log(Level.SEVERE, "error :" +ex.getMessage());
    }

    res.resume(Response.ok("Asynchronus EJB resource").build());
}
```

JAXRS 2.1 allow you return a `CompletionStage` directly.

```java
@GET
public CompletionStage<String> getAsync() {
    CompletionStage<String> cs = CompletableFuture
            .<String>supplyAsync(
                    () -> {
                        try {
                            LOG.log(Level.INFO, " execute long run task in CompletionStageResource");
                            Thread.sleep(500);
                        } catch (InterruptedException ex) {
                            LOG.log(Level.SEVERE, "error :" +ex.getMessage());
                        }
                        return "return CompletableFuture instead of @Suspended";
                    }
                   // , executor
            );

    return cs;
}
```

Grab the [source codes](https://github.com/hantsy/ee8-sandbox) from my GitHub account, and have a try.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hantsy.gitbook.io/java-ee-8-by-example/jaxrs/jaxrs-async.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
