이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 4. The Asynchronous API


4.1. The Asynchronous API

In addition to synchronous API methods, Red Hat JBoss Data Grid also offers an asynchronous API that provides the same functionality in a non-blocking fashion.

The asynchronous method naming convention is similar to their synchronous counterparts, with Async appended to each method name. Asynchronous methods return a Future that contains the result of the operation.

For example, in a cache parameterized as Cache<String, String>, Cache.put(String key, String value) returns a String, while Cache.putAsync(String key, String value) returns a FutureString.

4.2. Asynchronous API Benefits

The asynchronous API does not block, which provides multiple benefits, such as:

  • The guarantee of synchronous communication, with the added ability to handle failures and exceptions.
  • Not being required to block a thread’s operations until the call completes.

These benefits allow you to better harness the parallelism in your system, for example:

Using the Asynchronous API

Set<Future<?>> futures = new HashSet<Future<?>>();
futures.add(cache.putAsync("key1", "value1"));
futures.add(cache.putAsync("key2", "value2"));
futures.add(cache.putAsync("key3", "value3"));

In the example, The following lines do not block the thread as they execute:

  • futures.add(cache.putAsync(key1, value1));
  • futures.add(cache.putAsync(key2, value2));
  • futures.add(cache.putAsync(key3, value3));

The remote calls from the three put operations are executed in parallel. This is particularly useful when executed in distributed mode.

4.3. About Asynchronous Processes

For a typical write operation in Red Hat JBoss Data Grid, the following processes fall on the critical path, ordered from most resource-intensive to the least:

  • Network calls
  • Marshalling
  • Writing to a cache store (optional)
  • Locking

In Red Hat JBoss Data Grid, using asynchronous methods removes network calls and marshalling from the critical path.

4.4. Return Values and the Asynchronous API

When the asynchronous API is used in Red Hat JBoss Data Grid, the client code requires the asynchronous operation to return either the Future or the CompletableFuture in order to query the previous value.

Call the following operation to obtain the result of an asynchronous operation. This operation blocks threads when called.

Future.get()
Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.