49.6. 클라이언트의 비동기 처리

49.6.1. 개요

JAX-RS 2.0은 클라이언트 측에서 호출의 비동기 처리를 지원합니다. java.util.concurrent.Future<V > 반환 값을 사용하거나 호출 콜백을 등록하여 비동기 처리 스타일이 지원됩니다.

49.6.2. future<V> 반환 값을 사용한 비동기 호출

future< V > 접근 방식을 비동기 처리에 사용하면 다음과 같이 클라이언트 요청을 비동기적으로 호출할 수 있습니다.Using the future<V> approach to asynchronous processing, you can invoke a client request asynchronously, as follows:

// Java
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Client;
import java.util.concurrent.Future;
import javax.ws.rs.core.Response;
...
Client client = ClientBuilder.newClient();
Future<Response> futureResp = client.target("http://example.org/bookstore/books/123")
                     .request("application/xml")
                     .async()
                     .get();
...
// At a later time, check (and wait) for the response:
Response resp = futureResp.get();

입력한 답변에 유사한 방법을 사용할 수 있습니다. 예를 들어, type에 대한 응답을 얻으려면 BookInfo:

Client client = ClientBuilder.newClient();
Future<BookInfo> futureResp = client.target("http://example.org/bookstore/books/123")
                     .request("application/xml")
                     .async()
                     .get(BookInfo.class);
...
// At a later time, check (and wait) for the response:
BookInfo resp = futureResp.get();

49.6.3. 호출 콜백을 사용하는 비동기 호출

future<V> 개체를 사용하여 반환 값에 액세스하는 대신 호출 콜백( javax.ws.rs.client.InvocationCallback<RESPONSE>)을 사용하여 호출을 정의할 수 있습니다.

// Java
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Client;
import java.util.concurrent.Future;
import javax.ws.rs.core.Response;
import javax.ws.rs.client.InvocationCallback;
...
Client client = ClientBuilder.newClient();
Future<Response> futureResp = client.target("http://example.org/bookstore/books/123")
                     .request("application/xml")
                     .async()
                     .get(
    new InvocationCallback<Response>() {
        @Override
        public void completed(final Response resp) {
            // Do something when invocation is complete
            ...
        }

        @Override
        public void failed(final Throwable throwable) {
            throwable.printStackTrace();
        }
    });
...

입력한 답변에 유사한 방법을 사용할 수 있습니다.

// Java
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Client;
import java.util.concurrent.Future;
import javax.ws.rs.core.Response;
import javax.ws.rs.client.InvocationCallback;
...
Client client = ClientBuilder.newClient();
Future<BookInfo> futureResp = client.target("http://example.org/bookstore/books/123")
                     .request("application/xml")
                     .async()
                     .get(
    new InvocationCallback<BookInfo>() {
        @Override
        public void completed(final BookInfo resp) {
            // Do something when invocation is complete
            ...
        }

        @Override
        public void failed(final Throwable throwable) {
            throwable.printStackTrace();
        }
    });
...