io.vertx.ext.web.Route.blockingHandler()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(11.6k)|赞(0)|评价(0)|浏览(212)

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

Route.blockingHandler介绍

[英]Like io.vertx.ext.web.Route#blockingHandler(Handler,boolean) called with ordered = true
[中]就像伊奥。维特斯。ext.web。使用ordered=true调用Route#blockingHandler(Handler,boolean)

代码示例

代码示例来源:origin: vert-x3/vertx-examples

@Override
 public void start() throws Exception {

  Router router = Router.router(vertx);

  router.route().blockingHandler(routingContext -> {
   // Blocking handlers are allowed to block the calling thread
   // So let's simulate a blocking action or long running operation
   try {
    Thread.sleep(5000);
   } catch (Exception ignore) {
   }

   // Now call the next handler
   routingContext.next();
  }, false);

  router.route().handler(routingContext -> {
   routingContext.response().putHeader("content-type", "text/html").end("Hello World!");
  });

  vertx.createHttpServer().requestHandler(router).listen(8080);
 }
}

代码示例来源:origin: gentics/mesh

@Override
public InternalEndpointRoute blockingHandler(Handler<RoutingContext> requestHandler) {
  route.blockingHandler(requestHandler);
  return this;
}

代码示例来源:origin: gentics/mesh

@Override
public InternalEndpointRoute blockingHandler(Handler<RoutingContext> requestHandler, boolean ordered) {
  route.blockingHandler(requestHandler, ordered);
  return this;
}

代码示例来源:origin: wang007/vertx-start

@Override
public Route blockingHandler(Handler<RoutingContext> requestHandler, boolean ordered) {
  return delegate.blockingHandler(requestHandler, ordered);
}

代码示例来源:origin: io.vertx/vertx-rx-java

/**
 * Like {@link io.vertx.rxjava.ext.web.Route#blockingHandler} called with ordered = true
 * @param requestHandler 
 * @return 
 */
public io.vertx.rxjava.ext.web.Route blockingHandler(Handler<io.vertx.rxjava.ext.web.RoutingContext> requestHandler) { 
 delegate.blockingHandler(new Handler<io.vertx.ext.web.RoutingContext>() {
  public void handle(io.vertx.ext.web.RoutingContext event) {
   requestHandler.handle(io.vertx.rxjava.ext.web.RoutingContext.newInstance(event));
  }
 });
 return this;
}

代码示例来源:origin: wang007/vertx-start

@Override
public Route blockingHandler(Handler<RoutingContext> requestHandler) {
  return delegate.blockingHandler(requestHandler);
}

代码示例来源:origin: vert-x3/vertx-rx

/**
 * Like {@link io.vertx.rxjava.ext.web.Route#blockingHandler} called with ordered = true
 * @param requestHandler 
 * @return 
 */
public io.vertx.rxjava.ext.web.Route blockingHandler(Handler<io.vertx.rxjava.ext.web.RoutingContext> requestHandler) { 
 delegate.blockingHandler(new Handler<io.vertx.ext.web.RoutingContext>() {
  public void handle(io.vertx.ext.web.RoutingContext event) {
   requestHandler.handle(io.vertx.rxjava.ext.web.RoutingContext.newInstance(event));
  }
 });
 return this;
}

代码示例来源:origin: vert-x3/vertx-web

@Test
public void testExecuteBlockingParallel() throws Exception {
 long start = System.currentTimeMillis();
 int numExecBlocking = 5;
 long pause = 1000;
 router.route().blockingHandler(rc -> {
  try {
   Thread.sleep(pause);
  } catch (Exception ignore) {
  }
  rc.response().end();
 }, false);
 CountDownLatch latch = new CountDownLatch(numExecBlocking);
 for (int i = 0; i < numExecBlocking; i++) {
  client.getNow("/", onSuccess(resp -> {
   assertEquals(200, resp.statusCode());
   assertEquals("OK", resp.statusMessage());
   latch.countDown();
  }));
 }
 awaitLatch(latch);
 long now = System.currentTimeMillis();
 // we sleep for 5 seconds and we expect to be done within 2 + 1 seconds
 // this proves we run in parallel
 long leeway = 2000;
 assertTrue(now - start < pause + leeway);
}

代码示例来源:origin: vert-x3/vertx-web

@Test
public void testBlockingHandlerFailureThrowException() throws Exception {
 List<Thread> threads = new ArrayList<>();
 List<Context> contexts = new ArrayList<>();
 router.route().handler(rc -> {
  threads.add(Thread.currentThread());
  contexts.add(rc.vertx().getOrCreateContext());
  rc.next();
 });
 router.route().blockingHandler(rc -> {
  assertTrue(!threads.get(0).equals(Thread.currentThread()));
  assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext()));
  assertTrue(rc.currentRoute()!=null);
  throw new RuntimeException("foo");
 });
 router.route().failureHandler(rc -> {
  assertTrue(threads.get(0).equals(Thread.currentThread()));
  assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext()));
  assertTrue(rc.currentRoute()!=null);
  Throwable t = rc.failure();
  assertNotNull(t);
  assertTrue(t instanceof RuntimeException);
  assertEquals("foo", t.getMessage());
  rc.response().setStatusCode(500).end();
 });
 testRequest(HttpMethod.GET, "/", 500, "Internal Server Error");
}

代码示例来源:origin: io.vertx/vertx-rx-java

/**
 * Specify a blocking request handler for the route.
 * This method works just like {@link io.vertx.rxjava.ext.web.Route#handler} excepted that it will run the blocking handler on a worker thread
 * so that it won't block the event loop. Note that it's safe to call context.next() from the
 * blocking handler as it will be executed on the event loop context (and not on the worker thread.
 *
 * If the blocking handler is ordered it means that any blocking handlers for the same context are never executed
 * concurrently but always in the order they were called. The default value of ordered is true. If you do not want this
 * behaviour and don't mind if your blocking handlers are executed in parallel you can set ordered to false.
 * @param requestHandler the blocking request handler
 * @param ordered if true handlers are executed in sequence, otherwise are run in parallel
 * @return a reference to this, so the API can be used fluently
 */
public io.vertx.rxjava.ext.web.Route blockingHandler(Handler<io.vertx.rxjava.ext.web.RoutingContext> requestHandler, boolean ordered) { 
 delegate.blockingHandler(new Handler<io.vertx.ext.web.RoutingContext>() {
  public void handle(io.vertx.ext.web.RoutingContext event) {
   requestHandler.handle(io.vertx.rxjava.ext.web.RoutingContext.newInstance(event));
  }
 }, ordered);
 return this;
}

代码示例来源:origin: vert-x3/vertx-rx

/**
 * Specify a blocking request handler for the route.
 * This method works just like {@link io.vertx.rxjava.ext.web.Route#handler} excepted that it will run the blocking handler on a worker thread
 * so that it won't block the event loop. Note that it's safe to call context.next() from the
 * blocking handler as it will be executed on the event loop context (and not on the worker thread.
 *
 * If the blocking handler is ordered it means that any blocking handlers for the same context are never executed
 * concurrently but always in the order they were called. The default value of ordered is true. If you do not want this
 * behaviour and don't mind if your blocking handlers are executed in parallel you can set ordered to false.
 * @param requestHandler the blocking request handler
 * @param ordered if true handlers are executed in sequence, otherwise are run in parallel
 * @return a reference to this, so the API can be used fluently
 */
public io.vertx.rxjava.ext.web.Route blockingHandler(Handler<io.vertx.rxjava.ext.web.RoutingContext> requestHandler, boolean ordered) { 
 delegate.blockingHandler(new Handler<io.vertx.ext.web.RoutingContext>() {
  public void handle(io.vertx.ext.web.RoutingContext event) {
   requestHandler.handle(io.vertx.rxjava.ext.web.RoutingContext.newInstance(event));
  }
 }, ordered);
 return this;
}

代码示例来源:origin: vert-x3/vertx-web

@Test
public void testBlockingHandlerFailure() throws Exception {
 List<Thread> threads = new ArrayList<>();
 List<Context> contexts = new ArrayList<>();
 router.route().handler(rc -> {
  threads.add(Thread.currentThread());
  contexts.add(rc.vertx().getOrCreateContext());
  rc.response().setChunked(true);
  rc.next();
 });
 router.route().blockingHandler(rc -> {
  assertTrue(!threads.get(0).equals(Thread.currentThread()));
  assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext()));
  assertTrue(rc.currentRoute()!=null);
  rc.fail(501);
 });
 router.route().failureHandler(rc -> {
  assertTrue(threads.get(0).equals(Thread.currentThread()));
  assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext()));
  assertTrue(rc.currentRoute()!=null);
  rc.response().setStatusCode(rc.statusCode()).end();
 });
 testRequest(HttpMethod.GET, "/", 501, "Not Implemented");
}

代码示例来源:origin: vert-x3/vertx-web

rc.next();
});
router.route().blockingHandler(rc -> {
 assertTrue(!threads.get(0).equals(Thread.currentThread()));
 assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext()));
 rc.next();
});
router.route().blockingHandler(rc -> {
 assertTrue(!threads.get(0).equals(Thread.currentThread()));
 assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext()));

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

Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
router.route().blockingHandler(context -> {
  correlator.registerResponse(context.getBodyAsString(), context.request().headers());
  final HttpServerResponse response = context.response();

代码示例来源:origin: io.vertx/vertx-web

@Test
public void testExecuteBlockingParallel() throws Exception {
 long start = System.currentTimeMillis();
 int numExecBlocking = 5;
 long pause = 1000;
 router.route().blockingHandler(rc -> {
  try {
   Thread.sleep(pause);
  } catch (Exception ignore) {
  }
  rc.response().end();
 }, false);
 CountDownLatch latch = new CountDownLatch(numExecBlocking);
 for (int i = 0; i < numExecBlocking; i++) {
  client.getNow("/", resp -> {
   assertEquals(200, resp.statusCode());
   assertEquals("OK", resp.statusMessage());
   latch.countDown();
  });
 }
 awaitLatch(latch);
 long now = System.currentTimeMillis();
 // we sleep for 5 seconds and we expect to be done within 2 + 1 seconds
 // this proves we run in parallel
 long leeway = 2000;
 assertTrue(now - start < pause + leeway);
}

代码示例来源:origin: io.vertx/vertx-web

@Test
public void testBlockingHandlerFailureThrowException() throws Exception {
 List<Thread> threads = new ArrayList<>();
 List<Context> contexts = new ArrayList<>();
 router.route().handler(rc -> {
  threads.add(Thread.currentThread());
  contexts.add(rc.vertx().getOrCreateContext());
  rc.next();
 });
 router.route().blockingHandler(rc -> {
  assertTrue(!threads.get(0).equals(Thread.currentThread()));
  assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext()));
  assertTrue(rc.currentRoute()!=null);
  throw new RuntimeException("foo");
 });
 router.route().failureHandler(rc -> {
  assertTrue(threads.get(0).equals(Thread.currentThread()));
  assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext()));
  assertTrue(rc.currentRoute()!=null);
  Throwable t = rc.failure();
  assertNotNull(t);
  assertTrue(t instanceof RuntimeException);
  assertEquals("foo", t.getMessage());
  rc.response().setStatusCode(500).end();
 });
 testRequest(HttpMethod.GET, "/", 500, "Internal Server Error");
}

代码示例来源:origin: io.vertx/vertx-web

@Test
public void testBlockingHandlerFailure() throws Exception {
 List<Thread> threads = new ArrayList<>();
 List<Context> contexts = new ArrayList<>();
 router.route().handler(rc -> {
  threads.add(Thread.currentThread());
  contexts.add(rc.vertx().getOrCreateContext());
  rc.response().setChunked(true);
  rc.next();
 });
 router.route().blockingHandler(rc -> {
  assertTrue(!threads.get(0).equals(Thread.currentThread()));
  assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext()));
  assertTrue(rc.currentRoute()!=null);
  rc.fail(501);
 });
 router.route().failureHandler(rc -> {
  assertTrue(threads.get(0).equals(Thread.currentThread()));
  assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext()));
  assertTrue(rc.currentRoute()!=null);
  rc.response().setStatusCode(rc.statusCode()).end();
 });
 testRequest(HttpMethod.GET, "/", 501, "Not Implemented");
}

代码示例来源:origin: io.vertx/vertx-web

rc.next();
});
router.route().blockingHandler(rc -> {
 assertTrue(!threads.get(0).equals(Thread.currentThread()));
 assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext()));
 rc.next();
});
router.route().blockingHandler(rc -> {
 assertTrue(!threads.get(0).equals(Thread.currentThread()));
 assertTrue(contexts.get(0).equals(rc.vertx().getOrCreateContext()));

代码示例来源:origin: org.jboss.weld.vertx/weld-vertx-web

case BLOCKING:
  route.blockingHandler(handler, false);
  break;
case FAILURE:

代码示例来源:origin: weld/weld-vertx

case BLOCKING:
  route.blockingHandler(handler, false);
  break;
case FAILURE:

相关文章