비동기 처리
스레드를 처리하기 위해 JAX-RS 2.0에서 비동기 처리 기술을 사용할 수 있습니다. 비동기 처리는 클라이언트 API와 서버 API 둘 다에서 지원됩니다. 클라이언트 및 서버 API에서의 비동기 처리에 대한 자세한 정보는 JSR 339: JAX-RS 2.0: Java API for RESTful Web Services("스펙")의 8장을 참조하십시오.
다음 두 예에서는 클라이언트 및 서버 API에서의 비동기 처리를 보여줍니다.
- 클라이언트 API에서의 비동기 처리:
Client client = ClientBuilder.newClient(); WebTarget target = client.target("http://example.org/customers/{id}"); target.resolveTemplate("id", 123).request().async().get( new InvocationCallbackCustomer() { @Override public void completed(Customer customer) { // Do something } @Override public void failed(Throwable throwable) { // Process error } });
- 서버 API에서의 비동기 처리:
@Path("/async") public class MyResource{ @GET public void getAsync(@Suspended final AsyncResponse asyncResponse){ CompletionCallback callBack = new CompletionCallback(){ @Override public void onComplete(Throwable throwable) { ... } }; asyncResponse.register(callBack); asyncResponse.resume("some Response"); } }
Liberty에서 JAX-RS 2.0 구현은 EJB를 지원하며 루트 자원 클래스로 Stateless 및 싱글톤 세션 Bean의
사용을 지원합니다.
EJB 메소드에 @Asynchronous 어노테이션이 있는 경우, EJB 컨테이너는 실행에 필요한 자원을
자동으로 할당합니다. 그러므로 이 시나리오에서 비동기 응답을 생성하기 위해 Executor를 사용할 필요가 없습니다. 예를 들어,
@Stateless
@Path("/")
class EJBResource {
@GET @Asynchronous
public void longRunningOp(@Suspended AsyncResponse ar) {
executeLongRunningOp();
ar.resume("Hello async world!");
}
}
이 경우 EJB 컨테이너의 제어 하에 있으므로 명시적 스레드 관리가 필요하지 않습니다. 응답은 삽입된
AsyncResponse의 재개를 호출하여 생성됩니다. 그러므로 longRunningOp의 리턴 유형은 void입니다.