我正在尝试使用vertx executeBlocking来模拟我在
之后所做的实时场景vertx.setPeriodic(1000, id ->{
counter += 1;
LOGGER.info("invoked method {} ",counter);
vertx.executeBlocking(future -> {
int counterFinal = counter;
String result = service.blockingMethod("cycle "+counterFinal+" executed");
future.complete(result);
}, res -> {
LOGGER.info(String.format("The result is: %s", res.result()));
});
阻止方法非常简单
public String blockingMethod(String result){
block(2);
return result;
}
这就是结果
07:50:27.742 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.AsyncExperimentalVerticle - invoked method 1
07:50:28.742 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.AsyncExperimentalVerticle - invoked method 2
07:50:29.740 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.AsyncExperimentalVerticle - invoked method 3
07:50:29.764 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.AsyncExperimentalVerticle - The result is: cycle 1 executed
07:50:30.739 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.AsyncExperimentalVerticle - invoked method 4
07:50:31.739 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.AsyncExperimentalVerticle - invoked method 5
07:50:31.773 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.AsyncExperimentalVerticle - The result is: cycle 3 executed
07:50:32.751 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.AsyncExperimentalVerticle - invoked method 6
07:50:33.748 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.AsyncExperimentalVerticle - invoked method 7
07:50:33.789 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.AsyncExperimentalVerticle - The result is: cycle 5 executed
它显然平均缺少两个事件,因为延迟设置为2秒。 然后我将阻塞方法包装在一个类中,然后以下面的方式执行
vertx.setPeriodic(1000, id ->{
counter++;
LOGGER.info("invoked method {} ",counter);
service.wrapperMethod("Hello", counter, new Handler<AsyncClass>() {
@Override
public void handle(AsyncClass event) {
vertx.executeBlocking(future -> {
String result = event.result();
future.complete(result);
}, res -> {
LOGGER.info(String.format("The result is: %s", res.result()));
});
}
});
});
并且以这种方式设计了包装器方法
public void wrapperMethod(String input, int cycle, Handler<AsyncClass> execute) {
AsyncClass instance = new AsyncClass(input,String.valueOf(cycle)); // my custom class where the result method has a 2 sec delay
execute.handle(instance);
}
然后我得到了预期的结果。
08:08:27.358 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.TestVerticle2 - invoked method 1
08:08:27.368 [vert.x-worker-thread-0] INFO lab.async.base.support.AsyncClass - Invoking method inside AsyncClass class
08:08:28.338 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.TestVerticle2 - invoked method 2
08:08:29.345 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.TestVerticle2 - invoked method 3
08:08:29.384 [vert.x-worker-thread-0] INFO lab.async.base.support.AsyncClass - Invoking method inside AsyncClass class
08:08:29.386 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.TestVerticle2 - The result is: Hello world of cycle 1
08:08:30.347 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.TestVerticle2 - invoked method 4
08:08:31.351 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.TestVerticle2 - invoked method 5
08:08:31.391 [vert.x-worker-thread-0] INFO lab.async.base.support.AsyncClass - Invoking method inside AsyncClass class
08:08:31.391 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.TestVerticle2 - The result is: Hello world of cycle 2
08:08:32.341 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.TestVerticle2 - invoked method 6
08:08:33.343 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.TestVerticle2 - invoked method 7
08:08:33.396 [vert.x-worker-thread-0] INFO lab.async.base.support.AsyncClass - Invoking method inside AsyncClass class
08:08:33.397 [vert.x-eventloop-thread-0] INFO lab.async.base.verticle.TestVerticle2 - The result is: Hello world of cycle 3
现在我看到异步执行而不会错过任何一个事件。我无法找到可能的解释。 即使在包装方法中,如果我延迟n秒,它也会按预期错过事件。
有人请帮助我理解这种行为。
UPDATE1:
对于第二种情况,AsyncClass
的结构在下面给出
public class AsyncClass {
private static final Logger LOGGER = LoggerFactory.getLogger(AsyncClass.class);
private String input;
private String cycle;
public AsyncClass(String input, String cycle) {
this.input = input;
this.cycle = cycle;
}
public String result(){
LOGGER.info("Invoking method inside AsyncClass class");
block(2);
return input+" world of cycle "+cycle;
}
private void block(int pauseLimitInSecond){
try {
TimeUnit.SECONDS.sleep(pauseLimitInSecond);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOGGER.error("exception - > ", e);
}
}
}
答案 0 :(得分:1)
您使用的executeBlocking
method可确保按顺序执行阻止任务(一个接一个)。
因此,当执行第二个阻塞任务时(两秒钟后),counter
变量已经增加了两次。因此1
,3
,5
系列。
在使用包装类的其他尝试中,在调用counter
之前捕获executeBlocking
变量值。因此,当执行阻塞任务时,您将获得所期望的值。
答案 1 :(得分:1)
观察到的行为归因于Lambda body variable capture,其中捕获了外部类引用(this
)。
外部类实例变量counter
将在评估 Lambda body 表达式时更改其值(在初始值上增加1
),这会产生错觉意外行为。
保持相同的程序序列,您可以用Handler<Future<String>>
实现替换 Lambda表达式体,其中counter
外部实例变量存储在另一个实例变量中在处理程序执行体中使用:
private static final class BlockingHandler implements Handler<Future<String>> {
private final YourBlockingService service;
private final int counter;
public BlockingHandler(int counter, YourBlockingService service) {
this.counter = counter;
this.service = service;
}
@Override
public void handle(Future<String> event) {
String result = service.blockingMethod("cycle " + this.counter + " executed", 2);
event.complete(result);
}
}
您的Verticle代码将如下所示:
this.vertx.setPeriodic(
1000,
id -> {
counter += 1;
LOGGER.info("invoked method {}", counter);
vertx.executeBlocking(
new YourBlockingHandler(this.counter, this.service),
res -> LOGGER.info(String.format("The result is: %s", res.result()))
);
}
);
要恢复,上述行为仅与闭包语义有关,与 Vert.x 内部结构无关。