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

x33g5p2x  于11个月前 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(29)

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

AsyncResponse.setTimeout介绍

[英]Set/update the suspend timeout.

The new suspend timeout values override any timeout value previously specified. The asynchronous response must be still in a #isSuspended() state for this method to succeed.
[中]设置/更新挂起超时。
新的挂起超时值覆盖以前指定的任何超时值。异步响应必须仍处于#isSuspended()状态,此方法才能成功。

代码示例

代码示例来源: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: resteasy/Resteasy

@GET
@Path("test")
public void test(final @Suspended AsyncResponse response)
{
 response.setTimeout(5000, TimeUnit.MILLISECONDS);
 Thread t = new Thread()
 {
   @Override
   public void run()
   {
    try
    {
      LOG.info("TestResource: async thread started");
      Thread.sleep(10000);
      Response jaxrs = Response.ok("test").type(MediaType.TEXT_PLAIN).build();
      response.resume(jaxrs);
      LOG.info("TestResource: async thread finished");
    }
    catch (Exception e)
    {
      LOG.error(e.getMessage(), e);
    }
   }
 };
 t.start();
}

代码示例来源: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: stackoverflow.com

@Path("/poll")
@GET
public void poll(@Suspended final AsyncResponse asyncResponse)
    throws InterruptedException {
  asyncResponse.setTimeout(30, TimeUnit.SECONDS);
  this.asyncResponse = asyncResponse;
}

@POST
@Path("/printed")
public Response printCallback(String barcode) throws IOException {
  // ...

  this.asyncResponse.resume("MESSAGE");

  return Response.ok().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: Nike-Inc/wingtips

@GET
@Path(ASYNC_TIMEOUT_PATH)
public void getAsyncTimeout(@Suspended AsyncResponse asyncResponse) {
  logger.info("Async timeout endpoint hit");
  asyncResponse.setTimeout(SLEEP_TIME_MILLIS, TimeUnit.MILLISECONDS);
}

代码示例来源: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: stackoverflow.com

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void list(@Suspended
    final AsyncResponse asyncResponse) {
  asyncResponse.setTimeout(10, TimeUnit.SECONDS);
  executorService.submit(() -> {
    List<Product> res = super.listProducts();
    Product[] arr = res.toArray(new Product[res.size()]);
    asyncResponse.resume(arr);
  });
}

代码示例来源: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: apache/cxf

@GET
@Path("/setTimeOut")
public String setTimeOut() {
  boolean setTimeout = asyncResponse.setTimeout(2, TimeUnit.SECONDS);
  return String.valueOf(setTimeout);
}

代码示例来源: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: apache/cxf

@Override
public void handleTimeout(AsyncResponse asyncResponse) {
  if (!resumeOnly && timeoutExtendedCounter.addAndGet(1) <= 2) {
    asyncResponse.setTimeout(1, TimeUnit.SECONDS);
  } else {
    asyncResponse.resume(books.get(id));
  }
}

代码示例来源: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: apache/cxf

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

代码示例来源: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: 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: 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/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: 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());
}

相关文章