如何停止 vertx 线程?

huangapple 未分类评论54阅读模式
英文:

How to stop vertx threads?

问题

以下是翻译好的内容:

所以现在的情况是:我正在使用 vertx-redis 实现我们的 web 应用程序的缓存(之前我们使用过 lettuce)。机制相当简单,我们在端点上使用一个注解,该注解负责调用 Redis 客户端(无论我们使用哪种实现方式),如果对于给定的键存在缓存信息,则应将其用作响应主体,并且请求应在不进行任何处理的情况下完成。

但是 vertx-redis 实现中存在一个非常恼人的行为,即结束请求并不会停止处理过程。我发出请求,由于有缓存的信息,所以得到了快速的响应,但是我仍然可以在日志中看到应用程序继续进行处理,就好像请求仍然是打开的。我认为这是因为我在 Redis 客户端调用的处理程序中结束了响应,就像这样:

client.get("key", onResponse -> {
    if (onResponse.succeeded() && onResponse.result() != null) {
        // 从这里结束请求
    }
});

我意识到,如果我可以像以前那样做一些事情,可能可以复现这种行为:

String cachedInfo = client.get("key").map(onResponse -> onResponse.result());
// 结束响应

但正如我们所知,vertx-redis 是一个语义化的 API,每个方法都返回 RedisClient 的同一实例。我也考虑过像这样做:

private String cachedInfo; 

...

client.get("key", onResult -> {
    if (onResponse.succeeded()) {
        this.cachedInfo = onResponse.result();
    }
});

if (cachedInfo != null) { // 由于 lambda 在其他线程中运行,因此该值可能未设置
    // 结束请求
}

真的不知道该怎么办,有没有办法将 AsyncResult 的内容返回给变量,或者以某种方式将其同步设置为变量?我也一直在寻找一些停止当前请求整个流程的方法,但迄今为止没有找到令人满意且不侵略性的解决方案,但我确实对这个选项非常开放。

英文:

So here's the situation: I'm implementing the caching of our webapp using vertx-redis (we were formerly using lettuce). Pretty simple mechanism, there is an anotation we use on endpoints which is responsible to invoke the redis-client (whatever implementation we are using) and, if there is cached info for the given key it should be used as response body and the request should be finished with no processing.

But there's this really annoying behavior with the vertx-redis implementation in which ending the request doesn't stop the processing. I make the request, get the quick response since there was cached info, but I can still see in the logs that the app keeps the processing going on, as if the request was still open. I believe that it's because I'm ending the response inside the handler for the Redis client call, like this:

client.get("key", onResponse -> {
    if (onResponse.succeeded() && onResponse.result() != null) {
        //ending request from here
    }
});

I realize that I could maybe reproduce the behavior as it was before if I could do something like this:

String cachedInfo = client.get("key").map(onResponse -> onResponse.result());
// endResponse

But as we know, vertx-redis is a semantic API and every method returns the same instance of RedisClient. I also thought about doing something like this:

private String cachedInfo; 

...

client.get("key", onResult -> {
    if (onResponse.succeeded()) {
        this.cachedInfo = onResponse.result();
    }
});

if (cachedInfo != null) { // The value could be unset since the lambda is running in other thread 
    //end request
}

Really don't know what to do, is there a way to return the contents of the AsyncResult to a variable or maybe set it to a variable synchronously somehow? I've also been searching for ways to somehow stop the whole flow of the current request but couldn't find any satisfactory, non-aggressive solution so far, but I'm really open to this option either.

huangapple
  • 本文由 发表于 2020年4月7日 23:05:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/61083179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定