49.6. クライアントの非同期処理
概要
JAX-RS 2.0 は、クライアント側での呼び出しの非同期処理をサポートします。非同期処理の 2 種類のスタイルがサポートされます。これは、java.util.concurrent.Future<V>
戻り値を使用するか、呼び出しコールバックを登録します。
Future<V> の戻り値での非同期呼び出し
Future<V>
アプローチを非同期処理に使用する場合は、以下のようにクライアント要求を非同期的に呼び出すことができます。
// 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();
// 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();
型付きの応答と同様の方法を使用できます。たとえば、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();
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();
呼び出しコールバックを使用した非同期呼び出し
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<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(); } }); ...
// 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();
}
});
...