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

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

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

AsyncResponse.setTimeoutHandler介绍

[英]Set/replace a time-out handler for the suspended asynchronous response.

The time-out handler will be invoked when the suspend period of this asynchronous response times out. The job of the time-out handler is to resolve the time-out situation by either

  • resuming the suspended response
  • cancelling the suspended response
  • extending the suspend period by setting a new suspend time-out

Note that in case the response is suspended #NO_TIMEOUT, the time-out handler may never be invoked.
[中]为挂起的异步响应设置/替换超时处理程序。
当此异步响应的挂起周期超时时,将调用超时处理程序。超时处理程序的工作是通过以下两种方式解决超时情况:
*恢复暂停的答复
*取消挂起的响应
*通过设置新的暂停超时来延长暂停时间
请注意,如果响应被挂起(无超时),则可能永远不会调用超时处理程序。

代码示例

代码示例来源: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: confluentinc/kafka-streams-examples

public static void setTimeout(long timeout, AsyncResponse asyncResponse) {
 asyncResponse.setTimeout(timeout, TimeUnit.MILLISECONDS);
 asyncResponse.setTimeoutHandler(resp -> resp.resume(
   Response.status(Response.Status.GATEWAY_TIMEOUT)
     .entity("HTTP GET timed out after " + timeout + " ms\n")
     .build()));
}

代码示例来源:origin: confluentinc/qcon-microservices

public static void setTimeout(long timeout, AsyncResponse asyncResponse) {
  asyncResponse.setTimeout(timeout, TimeUnit.MILLISECONDS);
  asyncResponse.setTimeoutHandler(resp -> resp.resume(
      Response.status(Response.Status.GATEWAY_TIMEOUT)
          .entity("HTTP GET timed out after " + timeout + " ms\n")
          .build()));
}

代码示例来源:origin: documents4j/documents4j

public static IInputStreamConsumer to(AsyncResponse asyncResponse, DocumentType targetType, long requestTimeout) {
  AsynchronousConversionResponse response = new AsynchronousConversionResponse(asyncResponse, targetType);
  asyncResponse.setTimeoutHandler(response);
  asyncResponse.setTimeout(requestTimeout, TimeUnit.MILLISECONDS);
  return 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: stackoverflow.com

@POST
@Path("bid/{exchangeId}/{key}")
public void handleBid(@PathParam("exchangeId") final String exchangeId, @PathParam("key") final String key,
      final OrtbBidRequest request, @Suspended final AsyncResponse asyncResponse) throws FilterException, IOException {

  // Timeout for clarity.
  asyncResponse.setTimeout(timeoutMs, TimeUnit.MILLISECONDS);
  asyncResponse.setTimeoutHandler(timeoutHandler);

  // Sending no data as an example
  asyncResponse.resume(Response.noContent().build());
}

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

public StreamingAsyncSubscriber(AsyncResponse ar, String openTag, String closeTag, String sep,
                long pollTimeout, long asyncTimeout) {
  super(ar);
  this.openTag = openTag;
  this.closeTag = closeTag;
  this.separator = sep;
  this.pollTimeout = pollTimeout;
  this.asyncTimeout = 0;
  if (asyncTimeout > 0) {
    ar.setTimeout(asyncTimeout, TimeUnit.MILLISECONDS);
    ar.setTimeoutHandler(new TimeoutHandlerImpl());
  }
}
@Override

代码示例来源:origin: org.apache.cxf/cxf-rt-rs-extension-reactivestreams

public StreamingAsyncSubscriber(AsyncResponse ar, String openTag, String closeTag, String sep,
                long pollTimeout, long asyncTimeout) {
  super(ar);
  this.openTag = openTag;
  this.closeTag = closeTag;
  this.separator = sep;
  this.pollTimeout = pollTimeout;
  this.asyncTimeout = 0;
  if (asyncTimeout > 0) {
    ar.setTimeout(asyncTimeout, TimeUnit.MILLISECONDS);
    ar.setTimeoutHandler(new TimeoutHandlerImpl());
  }
}
@Override

代码示例来源:origin: com.proofpoint.platform/http-server

public AsyncResponseHandler withTimeout(Duration timeout, Supplier<Response> timeoutResponse)
{
  asyncResponse.setTimeoutHandler(asyncResponse -> {
    asyncResponse.resume(timeoutResponse.get());
    cancelFuture();
  });
  asyncResponse.setTimeout(timeout.toMillis(), MILLISECONDS);
  return this;
}

代码示例来源:origin: io.airlift/http-server

public AsyncResponseHandler withTimeout(Duration timeout, Supplier<Response> timeoutResponse)
{
  asyncResponse.setTimeoutHandler(asyncResponse -> {
    asyncResponse.resume(timeoutResponse.get());
    cancelFuture();
  });
  asyncResponse.setTimeout(timeout.toMillis(), MILLISECONDS);
  return this;
}

代码示例来源:origin: com.teradata.airlift/http-server

public AsyncResponseHandler withTimeout(Duration timeout, Supplier<Response> timeoutResponse)
{
  asyncResponse.setTimeoutHandler(asyncResponse -> {
    asyncResponse.resume(timeoutResponse.get());
    cancelFuture();
  });
  asyncResponse.setTimeout(timeout.toMillis(), MILLISECONDS);
  return this;
}

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

JsonPolicyDef.Policy.NOTIFICATION_TO_CLIENT);
asyncResponse.setTimeoutHandler(asyncRes -> asyncRes.resume(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/timeouthandlerresume/{id}")
public void getBookDescriptionWithHandlerResumeOnly(@PathParam("id") String id,
                          @Suspended AsyncResponse async) {
  async.setTimeout(1000, TimeUnit.MILLISECONDS);
  async.setTimeoutHandler(new TimeoutHandlerImpl(id, true));
}

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

@GET
@Path("/books/timeouthandler/{id}")
public void getBookDescriptionWithHandler(@PathParam("id") String id,
                     @Suspended AsyncResponse async) {
  async.setTimeout(1000, TimeUnit.MILLISECONDS);
  async.setTimeoutHandler(new TimeoutHandlerImpl(id, false));
}

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

@GET
@Path("/books/cancel")
public void getBookDescriptionWithCancel(@PathParam("id") String id,
                     @Suspended AsyncResponse async) {
  PhaseInterceptorChain.getCurrentMessage().getClass();
  async.setTimeout(2000, TimeUnit.MILLISECONDS);
  async.setTimeoutHandler(new CancelTimeoutHandlerImpl());
}

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

@GET
@Path("books/suspend/unmapped")
@Produces("text/plain")
public void handleNotMappedAfterSuspend(@Suspended AsyncResponse response) throws BookNotFoundFault {
  response.setTimeout(2000, TimeUnit.MILLISECONDS);
  response.setTimeoutHandler(new CancelTimeoutHandlerImpl());
  throw new BookNotFoundFault("");
}

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

asyncResponse.setTimeoutHandler(asyncRes ->
    asyncRes.resume(ResponseFactory.response(Response.Status.NO_CONTENT)));

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

Policy.COMMAND_LISTED);
asyncResponse.setTimeoutHandler(asyncRes -> asyncRes.resume(response));

代码示例来源:origin: labsai/EDDI

response.setTimeoutHandler((asyncResp) ->
    asyncResp.resume(Response.status(Response.Status.REQUEST_TIMEOUT).build()));
try {

相关文章