Java 重头戏1.3事件流实现
因此,我一直在尝试使用Play Framework 1.3实现一个合适的EventStream解决方案,但我就是无法让它正常工作。不断抛出异常,但我不确定原因是什么 有谁能提供一个示例实现或为我指出一个方向 这是我在控制器中使用的方法Java 重头戏1.3事件流实现,java,playframework,playframework-1.x,server-sent-events,Java,Playframework,Playframework 1.x,Server Sent Events,因此,我一直在尝试使用Play Framework 1.3实现一个合适的EventStream解决方案,但我就是无法让它正常工作。不断抛出异常,但我不确定原因是什么 有谁能提供一个示例实现或为我指出一个方向 这是我在控制器中使用的方法 public static void stream() { response.contentType = "text/event-stream"; response.encoding = "UTF-8"; response.status =
public static void stream() {
response.contentType = "text/event-stream";
response.encoding = "UTF-8";
response.status = 200;
response.chunked = true;
// a record of the last ID that this request... requested
Long lastId = 0L;
// since this request has only been initiated, the first thing to do is return a list of whats in the queue
List<F.IndexedEvent> archive = StreamQueue.getRecentNotifications(lastId);
if (!archive.isEmpty()) {
// write that list out in the response
response.writeChunk(//a string representing the entire list as a single event);
// update out internal reference to the lastId
lastId = archive.get(archive.size() - 1).id;
}
// keep this connection alive
while (true) {
// await the promise of more notifications for the stream
List<F.IndexedEvent<StreamQueue.Notification>> notifications = await(StreamQueue.getNextNotification(lastId));
// for each notification, write it out as a separate event
for (int i = 0; i < notifications.size(); i++) {
response.writeChunk(//a string representing a single event using notifications.get(i));
// update out internal reference to the lastId
lastId = notifications.get(i).id;
}
}
}
异常似乎是由于我从StreamQueue.getNextNotification(…)
调用的ArchivedEventStream.nextEvents(…)
而引发的。原因似乎是Long
被转换为Map
,但我从未尝试这样做
提前感谢您的帮助
更新
所以我在Play 1.2.5中尝试了我的项目,流媒体效果非常好。但是,项目要求我们使用播放1.3。原因在堆栈跟踪的底部:
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to java.util.Map
我认为您正在尝试强制转换
通知。时间戳
映射到StreamController
时间戳实际上没有被使用(以前代码的遗留部分),但异常似乎发生在ArchivedEventStream.nextEvents(…);
Internal Server Error (500) for request GET /stream
Execution exception
InvocationTargetException occured : null
play.exceptions.JavaExecutionException
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:230)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.reflect.InvocationTargetException
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:524)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:475)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:451)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:446)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:160)
... 1 more
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to java.util.Map
at controllers.StreamController.stream(StreamController.java)
... 6 more
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to java.util.Map