- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们有一组 Java 应用程序,它们最初是使用普通的同步方法编写的,但在任何有意义的地方都已大部分转换为异步 Vert.x(常规 API,而非 Rx)。我们在同步代码和异步代码之间的边界上遇到了一些麻烦,尤其是当我们有一个必须同步的方法(推理在下面解释)并且我们想从中调用一个异步方法时。
之前在 Stack Overflow 上提出了许多类似的问题,但实际上所有这些问题都在 C# 上下文中,而且答案似乎并不适用。
除其他外,我们还使用了 Geotools 和 Apache Shiro。两者都使用他们定义的严格同步的 API 通过扩展提供定制。作为一个具体的例子,我们为 Shiro 定制的授权领域需要访问我们的用户数据存储,为此我们创建了一个异步 DAO API。我们必须编写的 Shiro 方法称为 doGetAuthorizationInfo;
它应该返回一个 AuthorizationInfo
。但是似乎没有可靠的方法从异步 DAO API 的另一端访问授权数据。
在线程不是由 Vert.x 创建的特定情况下,使用 CompletableFuture
是一个可行的解决方案:同步 doGetAuthorizationInfo
会将异步工作推送到一个 Vert.x 线程,然后在 CompletableFuture.get()
中阻塞当前线程,直到结果可用。
不幸的是,Shiro(或 Geotools,或其他)方法可能会在 Vert.x 线程上被调用。在这种情况下,阻塞当前线程是非常糟糕的:如果它是事件循环线程,那么我们就违反了黄金法则,而如果它是工作线程(比如,通过 Vertx.executeBlocking
),那么阻止它会阻止工作人员从其队列中获取更多东西 - 这意味着阻止将是永久性的。
是否有解决此问题的“标准”解决方案?在我看来,只要在可扩展的同步库下使用 Vert.x,它就会突然出现。这只是人们避免的情况吗?
...更详细一点。这是 org.apache.shiro.realm.AuthorizingRealm 的一个片段:
/**
* Retrieves the AuthorizationInfo for the given principals from the underlying data store. When returning
* an instance from this method, you might want to consider using an instance of
* {@link org.apache.shiro.authz.SimpleAuthorizationInfo SimpleAuthorizationInfo}, as it is suitable in most cases.
*
* @param principals the primary identifying principals of the AuthorizationInfo that should be retrieved.
* @return the AuthorizationInfo associated with this principals.
* @see org.apache.shiro.authz.SimpleAuthorizationInfo
*/
protected abstract AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals);
我们的数据访问层有这样的方法:
void loadUserAccount(String id, Handler<AsyncResult<UserAccount>> handler);
我们如何从前者调用后者?如果我们知道 doGetAuthorizationInfo
在非 Vert.x 线程中被调用,那么我们可以这样做:
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
CompletableFuture<UserAccount> completable = new CompletableFuture<>();
vertx.<UserAccount>executeBlocking(vertxFuture -> {
loadUserAccount((String) principals.getPrimaryPrincipal(), vertxFuture);
}, res -> {
if (res.failed()) {
completable.completeExceptionally(res.cause());
} else {
completable.complete(res.result());
}
});
// Block until the Vert.x worker thread provides its result.
UserAccount ua = completable.get();
// Build up authorization info from the user account
return new SimpleAuthorizationInfo(/* etc...*/);
}
但是如果在 Vert.x 线程中调用 doGetAuthorizationInfo
那么事情就完全不同了。上面的技巧会阻塞一个事件循环线程,所以这是不行的。或者,如果它是一个工作线程,那么 executeBlocking
调用会将 loadUserAccount
任务放入同一个工作线程的队列中(我相信),因此随后的 completable.get ()
将永久阻止。
最佳答案
我打赌你已经知道答案了,但希望不是这样——如果调用 GeoTools 或 Shiro 需要阻塞等待来自某物的响应,那么你不应该在Vert.x 线程。
您应该创建一个带有线程池的 ExecutorService
,您应该使用它来执行这些调用,安排每个提交的任务在完成时发送一条 Vert.x 消息。
您可以灵活调整移入线程池的 block 的大小。您可以将更大的东西移到调用堆栈的更高位置,而不是将这些调用紧密包装起来。您可能会根据必须更改的代码量做出此决定。由于使方法异步通常意味着无论如何都要更改其调用堆栈中的所有同步方法(这是这种异步模型的不幸的基本问题),您可能希望在堆栈的高处进行。
您可能最终会得到一个适配器层,它为各种同步服务提供 Vert.x API。
关于java - 从必要的同步方法调用异步方法(Vert.x,Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55252198/
我是 Vert.X 新手。 Vert.x 是否有内置的集中式过滤器?我的意思是您将在 J2EE 应用程序上使用的过滤器类型。 例如,所有页面都必须经过身份验证过滤器或类似的操作。 在 Vert.x 中
正在关注 similar question在 Spring 。我希望能够在路由器中获得响应。并进行响应刷新,这样我就可以在不延长 RTT 的情况下继续在服务器上工作 意思是,做一些类似 Spring
我正在我的一个工作线程中进行阻塞服务调用,并记录了警告。这是通过增加时间限制来“解决”的,但是,我更好奇如何读取日志行中线程的命名 - vert.x-worker-thread-3,5,main。完整
我是 vert.x 的新手。我不寻找代码片段/示例,因为 vert.x github 页面充满了它们。 我正在寻找一些事实和最佳实践。 我正在编写一个应用程序,其主要verticle是一个处理Rest
顶点中的 TCP 服务器/网络服务器和 HTTP 服务器有什么区别? 每个的用例是什么? 我尝试谷歌搜索并浏览了官方网站,他们都没有明确的解释。 最佳答案 首先,在通用网络中,有两种常见的处理连接类型
如上所示,我需要构建一个 Vert.x Java 应用程序,该应用程序将作为 HTTP 服务器/虚拟主机(TLS Http 流量、Web 套接字流量),它将重定向/ channel 特定域流量到其他
我正在尝试为 Vert.x 开发一个概念验证——一个简单的实时浏览器游戏。 让我们想象一个类似于在线扑克的游戏 - 您有一个大厅,里面有许多您可以加入的现有游戏。您还可以创建一个新游戏(因此该大厅中的
我的问题是在内联列表之后有一个垂直列表 li {display:inline;} li.vert (color:red;} 如何让 li.vert 垂直显示它显示红色 OK 但内联 最佳答案
其实网上有大量讨论HTTP长连接的文章,而且Idle Timeout和KeepAlive Timeout都是HTTP协议上的事情,跟Vert.x本身没有太大关系,只不过最近在项目上遇到了一些问题,用到
是否可以为路径添加 2 个处理程序? 我需要加载 html文件夹中的内容并检查 session我访问时的值 /小路。 如果我放置 router.route().handler(StaticHandle
在 vertx docs他们提到有可能限制每个 vertx 实例的事件循环线程数: Instead of a single event loop, each Vertx instance mainta
考虑其中一个顶点抛出未捕获异常的情况。 接下来发生什么? 如果从系统中删除了 Verticle 状态,是否有一些类似于 erlang supervisor 的机制来重新启动 Verticle? 关于这
我正在使用vert.x 2.1.5版本。我试图在项目中使用事件循环。下面给出的示例代码 JsonObject result = null; //loop starting for (int i=0;i
我确实有同时使用协程和Vert.x的项目。 我正在尝试编写包装器函数以在vertx工作线程池上运行阻塞代码 就像是: suspend inline fun executeOnWorkerThread
Closed. This question is not reproducible or was caused by typos。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-to
我最近在java中使用vert.x框架,但我对vert.x还是新手。 通常当我们按照以下方式启动 future 时 SMObj自动分配给promise1 promise 。 Future future
查看 Vert.x 应用程序的日志,我注意到在取消部署 verticle 时出现以下异常(所有 3 个异常都是相同的): Failed to undeploy netsci.graphservice.
我正在将超时上下文传递给 Server.Shutdown(http 包)。我看不出我需要调用返回的取消函数,所以我忽略了它。但是当我运行 go vet 时,它说 context.WithTimeout
在这篇文章中,我们将会看到怎样在vert.x应用中使用HSQL,当然也可以使用任意JDBC,以及使用vertx-jdbc-client提供的异步的API,这篇文章的代码在github。 异步?
在我正在实现的 Vert.x verticle 中,我有一个之前加载到内存中的缓冲区,现在我想将它转储到磁盘中。 据我所知,我们应该使用 Pump 来确保不会使 WriteStream 过载。 但我没
我是一名优秀的程序员,十分优秀!