与 Spring MVC 框架的大气集成:
我成功地制作了 Spring MVC 和 Atmosphere 框架之间集成的简单小示例。它工作得很好,但它的行为方式我无法理解:
在我的尝试示例中,我试图每 10 秒广播一次 Date
字符串,所以如果我在 10:10 启动我的应用程序:20 下一条消息将是 10:10:30 等....
但在这段时间内我有 4 个回调,所以我的消息流如下所示:10:10:20, 10:10:21、10:10:22、10:24、10:10:30。此行为在同一时间段内按顺序发生:
为什么我在 10 秒内有 4 次回调,而不是每 10 秒一次?
我的相关代码是:
@RequestMapping(value="/websockets", method=RequestMethod.GET)
@ResponseBody
public void websockets(final AtmosphereResource<HttpServletRequest,HttpServletResponse> event) {
final HttpServletRequest req = event.getRequest();
final HttpServletResponse res = event.getResponse();
event.suspend();
final Broadcaster bc = event.getBroadcaster();
bc.scheduleFixedBroadcast(new Callable<String>() {
public String call() throws Exception {
return (new Date()).toString();
}
}, 10, TimeUnit.SECONDS);
}
在我的客户端代码上我正在执行以下操作:
<script type="text/javascript">
$(document).ready(function() {
var callbackAdded = false;
function callback(response)
{
$.atmosphere.log('info', ["response.state: " + response.state]);
$.atmosphere.log('info', ["response.transport: " + response.transport]);
if (response.transport != 'polling' && response.state != 'connected' && response.state != 'closed') {
$.atmosphere.log('info', ["response.responseBody: " + response.responseBody]);
if (response.status == 200) {
var data = response.responseBody;
if (data) {
$("#date").text(data);
}
}
}
}
$.atmosphere.subscribe("websockets",
!callbackAdded? callback : null,
$.atmosphere.request = {transport: 'websocket'});
connectedEndpoint = $.atmosphere.response;
callbackAdded = true;
});
</script>
I succeeded to make simple and small example which integrates between Spring MVC and Atmosphere framework. It works very well, but it behaves the way I'm failing to understand:
In my try example i'm trying to broadcust Date
string every 10 seconds, so If I start my app at 10:10:20 the next message will be 10:10:30, etc....
but instead of it I have 4 callbacks within this period of time so my message flow looks like this: 10:10:20, 10:10:21, 10:10:22, 10:24, 10:10:30. This behavior occurs sequentially with the same perion of time:
Why do I have 4 callbacks in 10 seconds instead of single one every 10 seconds?
My relevant code is:
@RequestMapping(value="/websockets", method=RequestMethod.GET)
@ResponseBody
public void websockets(final AtmosphereResource<HttpServletRequest,HttpServletResponse> event) {
final HttpServletRequest req = event.getRequest();
final HttpServletResponse res = event.getResponse();
event.suspend();
final Broadcaster bc = event.getBroadcaster();
bc.scheduleFixedBroadcast(new Callable<String>() {
public String call() throws Exception {
return (new Date()).toString();
}
}, 10, TimeUnit.SECONDS);
}
and on my client code I'm doing the following:
<script type="text/javascript">
$(document).ready(function() {
var callbackAdded = false;
function callback(response)
{
$.atmosphere.log('info', ["response.state: " + response.state]);
$.atmosphere.log('info', ["response.transport: " + response.transport]);
if (response.transport != 'polling' && response.state != 'connected' && response.state != 'closed') {
$.atmosphere.log('info', ["response.responseBody: " + response.responseBody]);
if (response.status == 200) {
var data = response.responseBody;
if (data) {
$("#date").text(data);
}
}
}
}
$.atmosphere.subscribe("websockets",
!callbackAdded? callback : null,
$.atmosphere.request = {transport: 'websocket'});
connectedEndpoint = $.atmosphere.response;
callbackAdded = true;
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加非阻塞支持后,我从 Web 应用程序收到以下消息:
在这条消息之后,我像以前一样收到超时错误。
您可以从日志中看到服务器现在正在使用http-nio-8080。
我现在该怎么办?
After adding the Non-Blocking support I get the following message from the web application:
And after this message I get the timeout errors like before.
You can see from the log now the Server is using http-nio-8080.
What should I do now ?
我知道你的代码有什么问题。
每个请求都会启动一个广播线程,每 10 秒将广播一次您的约会。
每次发送新请求(启动新浏览器)时,都会启动一个新线程。
不幸的是,线程似乎永远不会停止。
我不知道大气层如何处理这种情况。
谁负责停止广播线程。
我将阅读更多相关内容。
你可以简单地测试我所说的,重新启动网络服务器,只需启动一个浏览器,你应该每 10 秒只看到一个调用。
就我而言,它是这样工作的。我启动第二个第三个浏览器,然后我得到与浏览器客户端一样多的广播。
在我停止关闭浏览器客户端后,网络服务器永远不会停止广播线程。
第二种情况是,我实际上只有 1 秒的等待时间,而不是 10 秒。
我只能用 Tomcat 很难重现,所以我尝试了另一个 Web 服务器:JBOSS 7.0。
使用 JBOSS 总是可重现的。如果您启动第二个浏览器请求,那么第一个问题仍然存在,那么您第二次会收到 2 个调用:)。
I know whats the problem with this your code.
Every request with start a broadcast Thread what every 10 seconds will brodcast your date.
Every time you send a new request (start a new browser) a new Thread will start.
Unfortunately seems the Threads will never stop.
I dont know how the Atmosphere handles such cases.
Who is responsable to stop the brodcast threads.
I will read more about this.
You can test easy what I said, restart web-server, just start one browser and you should see only one call every 10 seconds.
In my case it works like this. I start second third browser then I get brodcast as many times as browser clients.
After I stop close the browser clients the web server never stops the brodcast threads.
Second case was when really instead of 10 seconds wait time I had only 1 second.
This I could only hard reproduce with Tomcat so I was trying another web-server: JBOSS 7.0.
With JBOSS was always reproducable. The first problems remain if you starts a second browser request then you get 2 calls pro second :).