- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的目标是支持多个 Web 服务调用者的长轮询,并跟踪哪些调用者当前“停留”在长轮询上(即已连接)。我所说的“长轮询”是指调用者调用 Web 服务,而服务器(Web 服务)不会立即返回,而是让调用者等待一段预设的时间(在我的应用程序中为一个小时),或者更快地返回如果服务器有消息要发送给调用者(在这种情况下,服务器通过调用 asyncResponse.resume("MESSAGE") 返回消息)。
我将把这个问题分成两个问题。
第一个问题:这是“停放”长轮询调用者的合理方式吗?
@GET
@Produces(MediaType.TEXT_PLAIN)
@ManagedAsync
@Path("/poll/{id}")
public Response poller(@Suspended final AsyncResponse asyncResponse, @PathParam("id") String callerId) {
// add this asyncResponse to a HashMap that is persisted across web service calls by Jersey.
// other application components that may have a message to send to a caller will look up the
// caller by callerId in this HashMap and call resume() on its asyncResponse.
callerIdAsyncResponseHashMap.put(callerId, asyncResponse);
asyncResponse.setTimeout(3600, TimeUnit.SECONDS);
asyncResponse.setTimeoutHandler(new TimeoutHandler() {
@Override
public void handleTimeout(AsyncResponse asyncResponse) {
asyncResponse.resume(Response.ok("TIMEOUT").build());
}
});
return Response.ok("COMPLETE").build();
}
这很好用。我只是不确定它是否遵循最佳实践。方法末尾有“return Response...”行似乎很奇怪。该行在调用者首次连接时执行,但是据我了解,“完整”结果实际上从未返回给调用者。当服务器需要通知调用者某个事件时,调用者要么获得“TIMEOUT”响应,要么获得服务器通过 asyncResponse.resume() 发送的其他响应消息。
第二个问题:我当前的挑战是在 HashMap 中准确反射(reflect)当前轮询调用者的数量。当调用者停止轮询时,我需要从 HashMap 中删除其条目。调用者可以出于以下三个原因离开:1) 3600 秒过去,因此超时,2) 另一个应用程序组件在 HashMap 中查找调用者并调用 asyncResponse.resume("MESSAGE"),以及 3) HTTP 连接已关闭由于某种原因损坏,例如有人关闭了运行客户端应用程序的计算机。
因此,JAX-RS 有两个回调,我可以注册以接收连接结束的通知:CompletionCallback(用于我上面的结束轮询原因 #1 和 #2)和 ConnectionCallback(用于我上面的结束轮询原因 #3) .
我可以将这些添加到我的网络服务方法中,如下所示:
@GET
@Produces(MediaType.TEXT_PLAIN)
@ManagedAsync
@Path("/poll/{id}")
public Response poller(@Suspended final AsyncResponse asyncResponse, @PathParam("id") String callerId) {
asyncResponse.register(new CompletionCallback() {
@Override
public void onComplete(Throwable throwable) {
//?
}
});
asyncResponse.register(new ConnectionCallback() {
@Override
public void onDisconnect(AsyncResponse disconnected) {
//?
}
});
// add this asyncResponse to a HashMap that is persisted across web service calls by Jersey.
// other application components that may have a message to send to a caller will look up the
// caller by callerId in this HashMap and call resume() on its asyncResponse.
callerIdAsyncResponseHashMap.put(callerId, asyncResponse);
asyncResponse.setTimeout(3600, TimeUnit.SECONDS);
asyncResponse.setTimeoutHandler(new TimeoutHandler() {
@Override
public void handleTimeout(AsyncResponse asyncResponse) {
asyncResponse.resume(Response.ok("TIMEOUT").build());
}
});
return Response.ok("COMPLETE").build();
}
正如我所说,挑战是使用这两个回调从 HashMap 中删除不再轮询的调用者。 ConnectionCallback 实际上是两者中更容易的一个。由于它接收 asyncResponse 实例作为参数,我可以使用它从 HashMap 中删除相应的条目,如下所示:
asyncResponse.register(new ConnectionCallback() {
@Override
public void onDisconnect(AsyncResponse disconnected) {
Iterator<Map.Entry<String, AsyncResponse>> iterator = callerIdAsyncResponseHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, AsyncResponse> entry = iterator.next();
if (entry.getValue().equals(disconnected)) {
iterator.remove();
break;
}
}
}
});
但是,对于 CompletionCallback,由于在触发回调时 asyncResponse 已经完成或取消,因此没有传入 asyncResponse 参数。因此,似乎唯一的解决方案是运行 HashMap 条目检查完成/取消的并删除它们,如下所示。 (请注意,我不需要知道调用者离开是因为调用了resume() 还是因为超时,因此我不查看“可抛出”参数)。
asyncResponse.register(new CompletionCallback() {
@Override
public void onComplete(Throwable throwable) {
Iterator<Map.Entry<String, AsyncResponse>> iterator = callerIdAsyncResponseHashMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, AsyncResponse> entry = iterator.next();
if (entry.getValue().isDone() || entry.getValue().isCancelled()) {
iterator.remove();
}
}
}
});
如有任何反馈,我们将不胜感激。这种做法看起来合理吗?有更好或更多种 Jersey/JAX-RS 方法吗?
最佳答案
您的 poller() 方法不需要返回 Response 即可参与异步处理。它可以返回 void。但是,如果您在轮询器中执行任何复杂的操作,则应考虑将整个方法包装在 try/catch block 中,该 block 将恢复 AsyncResponse 对象,但会出现异常,以确保任何 RuntimeException 或其他未经检查的 Throwable 不会丢失。将这些异常记录在此处的 catch block 中似乎也是一个好主意。
我目前正在研究如何可靠地捕获客户端取消的异步请求的问题,并阅读了一个问题,该问题表明该机制不适用于提问者[1]。我暂时将这些信息留给其他人来填写。
[1] AsyncResponse ConnectionCallback does not fire in Jersey
关于java - Jersey/JAX-RS 2 AsyncResponse - 如何跟踪当前的长轮询调用者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26624183/
Apache CXF 可以完成任务。但我想在没有任何 spring 依赖的情况下组合它。提前谢谢你! 最佳答案 JAX-WS 和 JAX-RS 都是 Java EE 6 的一部分.因此,您可以在不使用
Apache CXF 可以完成任务。但我想在没有任何 spring 依赖的情况下组合它。提前谢谢你! 最佳答案 JAX-WS 和 JAX-RS 都是 Java EE 6 的一部分.因此,您可以在不使用
所以我的意思是你有一个分类特征 $X$(假设你已经把它变成了整数)并说你想使用特征 $A$ 将它嵌入到某个维度中,其中 $A$ 是 arity x n_embed . 通常的做法是什么?使用 for
所以我的意思是你有一个分类特征 $X$(假设你已经把它变成了整数)并说你想使用特征 $A$ 将它嵌入到某个维度中,其中 $A$ 是 arity x n_embed . 通常的做法是什么?使用 for
我知道网上有很多文档主要描述技术差异。 但是我很想知道您更喜欢一种特定类型而不是其他类型的常见用例是什么? 这些偏好是因为集成模式/产品支持特定类型吗? 最佳答案 谢谢你的回答。然而实际用例差异以及何
我想将 CXF 与 Google Guice 集成。我已经在我的项目中使用 Guice,并且我想避免添加额外的依赖项。 CXF 是我的选择,因为要求之一是能够向服务用户提供 XML、JSON、JSON
来自 JSR-339: For simplicity, JAX-RS implementations are NOT REQUIRED to support processing groups oth
我开始使用 JAX WS 研究 Java Web 服务。我正在阅读的书的第一章展示了如何仅使用 java SE 构建和部署简单的 jax ws web 服务。特别是,Web 服务是通过 Endpoin
在 JAX 的快速入门教程中,我发现可以使用以下代码行为可微函数 fun 高效地计算 Hessian 矩阵: from jax import jacfwd, jacrev def hessian(fu
JAX-RS 提供了 StreamingOutput 接口(interface),我们可以实现它来对我们的响应主体进行原始流处理。 public interface StreamingOutput {
有没有办法获得java.lang.reflect.Method为给定的 @Path 调用的方法(用 HttpServletRequest 注释) ? 这是我的用例:我在 Java EE 中 Filte
我接到了一家公司的任务,该任务向我发送了一台已完成所有设置的虚拟机。任务是我必须创建一个 API 来从数据库中检索人员详细信息并显示它。 问题是,当我运行应用程序时,服务器返回一个包含 hello w
我有一个 POST 方法调用,它接受很少的表单参数。我希望 JAX-RS 能够处理不存在特定表单参数的情况。 示例: @POST @Produces (MediaType.APPLICATION_JS
我有 JAX-RS 网络应用程序,我想记录从获取请求到响应的时间量。在带有 servlet 过滤器的 Spring Boot 中很容易。但是我的应用程序中的过滤器无法正常工作: @Provider p
使用以下网址。 http://doma.in/context/resource/some/.../undefined 我想获取 ../resource 之后的路径名,即 /some/.../undef
我编写了 2 个 Web 服务,一个使用 Jax-WS,一个使用 Jax-RPC。他们只是返回一个字符串。 我使用 jMeter 进行了压力测试,奇怪的是,Jax-RPC 速度更快。 我什么时候才能在
我花了几个小时在嵌入式 Jetty 9.1.0.v20131115 和 RESTEasy 3.0.5.Final 中安装自定义登录服务。我的登录服务将在数据库中查找用户并为他们分配角色。它看起来像这样
有没有办法操纵jaxws中使用的编码器。 我喜欢发送一个在网络服务请求中提交的 cdata,为此我想在这里尝试类似描述的东西:http://odedpeer.blogspot.de/2010/07/j
我想从 java 服务器 (Restful Jax-rs) 发送图像。我的客户是安卓。 @GET public Response getUserImage() { byte[] image =new
我对网络服务很陌生。我找不到 JAX-WS 和 CXF 之间的区别。 据我了解,JAX-WS是java提供的规范,CXF是实现。 如果我错了,请纠正我。 最佳答案 是的,你是对的。 JAX-WS 是基
我是一名优秀的程序员,十分优秀!