Reactive Client

In JAXRS 2.0, a client to handle async resources looks like.

public class AsyncClient {

    public final static void main(String[] args) throws Exception {

        WebTarget target = ClientBuilder.newClient().target("http://localhost:8080/jaxrs-async/rest/ejb");

        Future<String> future = target.request()
                .async()
                .get(String.class);

        System.out.println("ejb resource future:" + future.get());

        target.request()
                .async()
                .get(AsyncClient.responseInvocationCallback());
    }

    private static InvocationCallback<Response> responseInvocationCallback() {
        return new InvocationCallback<Response>() {
            @Override
            public void completed(Response res) {
                System.out.println("Status:" + res.getStatusInfo());
                System.out.println("Entity:" + res.getEntity());
                System.out.println("Request success!");
            }

            @Override
            public void failed(Throwable e) {
                System.out.println("Request failed!");
            }

        };
    }

}

JAXRS 2.1 embraces the Reactive concept, added a rx() method switch to Reactive APIs and handle the response in stream.

By default, it supports Java 8 CompletionStage.

Jersey added extra support fro rxjava1.

And rxjava2.

And Guava's ListenableFuture.

Grab the source codes from my GitHub account, and have a try.

Last updated

Was this helpful?