javax.ws.rs.container.AsyncResponse.register()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(6.4k)|赞(0)|评价(0)|浏览(91)

本文整理了Java中javax.ws.rs.container.AsyncResponse.register()方法的一些代码示例,展示了AsyncResponse.register()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AsyncResponse.register()方法的具体详情如下:
包路径:javax.ws.rs.container.AsyncResponse
类名称:AsyncResponse
方法名:register

AsyncResponse.register介绍

[英]Register an asynchronous processing lifecycle callback class to receive lifecycle events for the asynchronous response based on the implemented callback interfaces.
[中]注册异步处理生命周期回调类,以基于实现的回调接口接收异步响应的生命周期事件。

代码示例

代码示例来源:origin: javaee-samples/javaee7-samples

@GET
public void getList(@Suspended final AsyncResponse ar) throws NamingException {
  ar.setTimeoutHandler(new TimeoutHandler() {
    @Override
    public void handleTimeout(AsyncResponse ar) {
      ar.resume("Operation timed out");
    }
  });
  ar.setTimeout(4000, TimeUnit.MILLISECONDS);
  ar.register(new MyCompletionCallback());
  ar.register(new MyConnectionCallback());
  ManagedThreadFactory threadFactory = (ManagedThreadFactory) new InitialContext()
    .lookup("java:comp/DefaultManagedThreadFactory");
  Executors.newSingleThreadExecutor(threadFactory).submit(new Runnable() {
    @Override
    public void run() {
      try {
        Thread.sleep(3000);
        ar.resume(response[0]);
      } catch (InterruptedException ex) {
      }
    }
  });
}

代码示例来源:origin: prestodb/presto

asyncResponse.register((CompletionCallback) throwable -> resultsRequestTime.add(Duration.nanosSince(start)));

代码示例来源:origin: stackoverflow.com

@Path("/callback")
public class AsyncCallback {
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  public void postWithAsync(@Suspended AsyncResponse asyncResponse,  
                     SomeObject object) {

    asyncResponse.register(new CompletionCallback() {
      @Override
      public void onComplete(Throwable error) {
        if (error == null) {
          System.out.println("Processing new Request");
        } else {
          System.out.println("Exception in Request handling");
        }
      }
    });
    Response response = Response.ok("Success").build();
    // Carry on like nothing happened
    asyncResponse.resume(response);
  }
}

代码示例来源:origin: feuyeux/jax-rs2-guide-II

private void configResponse(final AsyncResponse asyncResponse) {
  asyncResponse.register((CompletionCallback)throwable -> {
    if (throwable == null) {
      log.info("CompletionCallback-onComplete: OK");
    } else {
      log.info("CompletionCallback-onComplete: ERROR: " + throwable.getMessage());
    }
  });
  asyncResponse.register((ConnectionCallback)disconnected -> {
    //Status.GONE=410
    log.info("ConnectionCallback-onDisconnect");
    disconnected.resume(Response.status(Response.Status.GONE).entity("disconnect!").build());
  });
  asyncResponse.setTimeoutHandler(r -> {
    //Status.SERVICE_UNAVAILABLE=503
    log.info("TIMEOUT");
    r.resume(Response.status(Response.Status.SERVICE_UNAVAILABLE).entity("Operation time out.").build());
  });
  asyncResponse.setTimeout(TIMEOUT, TimeUnit.SECONDS);
}

代码示例来源:origin: SmartDataAnalytics/jena-sparql-api

.register(new ConnectionCallback() {
  @Override
  public void onDisconnect(AsyncResponse disconnect) {
.register(new CompletionCallback() {
  @Override
  public void onComplete(Throwable t) {

代码示例来源:origin: apache/cxf

@GET
@Path("books/notfound/unmapped")
@Produces("text/plain")
public void handleContinuationRequestNotFoundUnmapped(@Suspended AsyncResponse response) {
  response.register(new CallbackImpl());
  resumeSuspendedNotFoundUnmapped(response);
}

代码示例来源:origin: org.realityforge.replicant/replicant-server

@GET
@Produces( "text/plain" )
public void poll( @Suspended final AsyncResponse response,
         @NotNull @HeaderParam( SharedConstants.CONNECTION_ID_HEADER ) final String sessionId,
         @NotNull @QueryParam( SharedConstants.RECEIVE_SEQUENCE_PARAM ) final int rxSequence )
{
 response.setTimeout( getPollTime(), TimeUnit.SECONDS );
 response.register( (ConnectionCallback) this::doDisconnect );
 response.setTimeoutHandler( this::doTimeout );
 try
 {
  final String data = poll( sessionId, rxSequence );
  if ( null != data )
  {
   resume( response, data );
  }
  else
  {
   _requests.put( response, new SuspendedRequest( sessionId, rxSequence, response ) );
  }
 }
 catch ( final Exception e )
 {
  handleException( sessionId, response, e );
 }
}

代码示例来源:origin: apache/cxf

@GET
@Path("books/unmappedFromFilter")
@Produces("text/plain")
public void handleContinuationRequestUnmappedFromFilter(@Suspended AsyncResponse response) {
  response.register(new CallbackImpl());
  response.resume(Response.ok().build());
}

代码示例来源:origin: devicehive/devicehive-java-server

asyncResponse.register((CompletionCallback) throwable -> notificationService.unsubscribe(Collections.singleton(pair.getLeft())));
} else {
  if (!asyncResponse.isDone()) {

代码示例来源:origin: apache/cxf

@GET
@Path("books/notfound")
@Produces("text/plain")
public void handleContinuationRequestNotFound(@Suspended AsyncResponse response) {
  response.register(new CallbackImpl());
  resumeSuspendedNotFound(response);
}

代码示例来源:origin: apache/cxf

@GET
@Path("/books/defaulttimeout")
public void getBookDescriptionWithTimeout(@Suspended AsyncResponse async) {
  async.register(new CallbackImpl());
  async.setTimeout(2000, TimeUnit.MILLISECONDS);
}

代码示例来源:origin: SmartDataAnalytics/jena-sparql-api

.register(new ConnectionCallback() {
  @Override
  public void onDisconnect(AsyncResponse disconnect) {
.register(new CompletionCallback() {
  @Override
  public void onComplete(Throwable t) {

代码示例来源:origin: apache/cxf

@GET
@Path("/disconnect")
public void handleClientDisconnects(@Suspended AsyncResponse response) {
  response.setTimeout(0, TimeUnit.SECONDS);
  response.register(new ConnectionCallback() {
    @Override
    public void onDisconnect(AsyncResponse disconnected) {
      System.out.println("ConnectionCallback: onDisconnect, client disconnects");
    }
  });
  try {
    Thread.sleep(3000);
  } catch (InterruptedException ex) {
    // ignore
  }
  response.resume(books.values().toString());
}

代码示例来源:origin: io.prestosql/presto-main

asyncResponse.register((CompletionCallback) throwable -> resultsRequestTime.add(Duration.nanosSince(start)));

代码示例来源:origin: prestosql/presto

asyncResponse.register((CompletionCallback) throwable -> resultsRequestTime.add(Duration.nanosSince(start)));

代码示例来源:origin: devicehive/devicehive-java-server

asyncResponse.register((CompletionCallback) throwable -> commandService.sendUnsubscribeRequest(Collections.singleton(pair.getLeft())));
} else {
  if (!asyncResponse.isDone()) {

代码示例来源:origin: SmartDataAnalytics/jena-sparql-api

.register(new ConnectionCallback() {
  @Override
  public void onDisconnect(AsyncResponse disconnect) {
.register(new CompletionCallback() {
  @Override
  public void onComplete(Throwable t) {

代码示例来源:origin: devicehive/devicehive-java-server

asyncResponse.register((CompletionCallback) throwable -> {
  try {
    commandService.sendUnsubscribeRequest(Collections.singleton(future.get().getLeft()));

相关文章