gpt4 book ai didi

ajax - 断开Grails中的 Atmosphere socket

转载 作者:行者123 更新时间:2023-12-02 14:46:15 27 4
gpt4 key购买 nike

我正在编写一个使用Atmosphere插件的Grails应用程序。连接有效,但是每次我在浏览器中更新页面时,都会看到Web服务器添加了一个新的Daemon线程,此线程以后再也不会释放。

线程数达到200后,Web服务器将冻结。

似乎没有文档说明使用Atmosphere插件处理资源(断开连接)的正确方法是什么?

我的客户代码这样做:

var connectedEndpoint = null;

$(function()
{
function callback(response)
{
if (response.transport != 'polling' && response.state != 'connected' && response.state != 'closed') {
if (response.status == 200) {
eval(response.responseBody);
}
}
}

$.atmosphere.subscribe('${resource(dir: '/atmosphere/channel')}', callback, $.atmosphere.request = {transport: 'streaming'});
connectedEndpoint = $.atmosphere.response;

});

$(window).unload( function ()
{
$.atmosphere.unsubscribe();
connectedEndpoint = null;
});

我在服务器端使用了气氛处理程序;
package demo

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.atmosphere.cpr.AtmosphereHandler
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceEvent;

class DemoController implements AtmosphereHandler<HttpServletRequest, HttpServletResponse> {

@Override
public void destroy() {
println "destroy"
}

@Override
public void onRequest( AtmosphereResource<HttpServletRequest, HttpServletResponse> event) throws IOException
{
event.suspend()
}

@Override
public void onStateChange( AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) throws IOException
{
if (event.isSuspended())
{
event.resource.response.writer.with {
def message = event.message
write "set${message.paramName}(\"${message.id}\",\"${message.value}\");"
flush()
}
}
}
}

永远不会调用处理程序的 destroy函数!

下图显示我正在运行23个线程。当我启动我的应用程序时,大约有6个,并且每当我按F5键时它们就会被添加!如果禁用大气,则不会添加新线程,因此此问题与大气有关。 (我在Windows7上使用SpringSource Tools Suite)。

如果解决方案不简单,我将不胜感激,详细的分步说明或示例。

更新:在Tomcat中部署后,每20秒出现以下错误:
Apr 02, 2012 2:35:15 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor host-manager.xml
Apr 02, 2012 2:35:16 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor manager.xml
Apr 02, 2012 2:35:16 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory docs
Apr 02, 2012 2:35:16 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory examples
Apr 02, 2012 2:35:17 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory ROOT
Apr 02, 2012 2:35:17 PM org.apache.coyote.http11.Http11AprProtocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Apr 02, 2012 2:35:17 PM org.apache.coyote.ajp.AjpAprProtocol start
INFO: Starting Coyote AJP/1.3 on ajp-8009
Apr 02, 2012 2:35:17 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 11401 ms
2012-04-02 14:41:17,122 [http-8080-39] ERROR cpr.AsynchronousProcessor - failed
to timeout resource AtmosphereResourceImpl{, hasCode-1035775543,
action=org.atmosphere.cpr.AtmosphereServlet$Action@f2718e,
broadcaster=org.atmosphere.cpr.DefaultBroadcaster,
cometSupport=org.atmosphere.container.TomcatCometSupport@107fff7,
serializer=null,
isInScope=true,
useWriter=true,
listeners=[]}
2012-04-02 14:42:15,034 [http-8080-69] ERROR cpr.AsynchronousProcessor - failed
to timeout resource AtmosphereResourceImpl{, hasCode-58082012,
action=org.atmosphere.cpr.AtmosphereServlet$Action@ae4dd4,
broadcaster=org.atmosphere.cpr.DefaultBroadcaster,
cometSupport=org.atmosphere.container.TomcatCometSupport@107fff7,
serializer=null,
isInScope=true,
useWriter=true,
listeners=[]}
2012-04-02 14:44:41,159 [http-8080-13] ERROR cpr.AsynchronousProcessor - failed
to timeout resource AtmosphereResourceImpl{, hasCode648226529,
action=org.atmosphere.cpr.AtmosphereServlet$Action@507e61,
broadcaster=org.atmosphere.cpr.DefaultBroadcaster,
cometSupport=org.atmosphere.container.TomcatCometSupport@107fff7,
serializer=null,
isInScope=true,
useWriter=true,
listeners=[]}

....

最佳答案

您正在使用哪个Web服务器?听起来好像Web服务器没有检测到浏览器何时关闭连接。您可以在web.xml中添加以下超时检测器

org.atmosphere.cpr.CometSupport.maxInactiveActivity=30000 //30 seconds

关于ajax - 断开Grails中的 Atmosphere socket ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9668909/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com