2012-03-12 62 views
1

我在寫一個使用Atmosphere插件的Grails應用程序。這個連接可以工作,但每次我在瀏覽器中更新頁面時,我都會看到我的Web服務器添加了一個新的守護程序線程,這個線程從未被釋放。斷開Grails中的大氣插座

線程數達到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工具套件)。

Daemon threads

如果溶液是不平凡的我希望詳細的一步一步的指示或一個例子。

UPDATE:部署在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, 
[email protected], 
broadcaster=org.atmosphere.cpr.DefaultBroadcaster, 
[email protected], 
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, 
[email protected], 
broadcaster=org.atmosphere.cpr.DefaultBroadcaster, 
[email protected], 
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, 
[email protected], 
broadcaster=org.atmosphere.cpr.DefaultBroadcaster, 
[email protected], 
serializer=null, 
isInScope=true, 
useWriter=true, 
listeners=[]} 

.... 

回答

1

薩呂,

您使用該Web服務器?聽起來像WebServer在瀏覽器關閉連接時不會偵測到。您可以添加,在web.xml,下面超時檢測

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

讓我知道如何去。

A +

- Jeanfrancois

+0

不幸的是,它不起作用:'沒有這樣的屬性:類:org的maxInactiveActivity。atmosphere.cpr.CometSupport' – conceptacid 2012-03-13 09:19:15

+0

我的web服務器是tomcat。 – conceptacid 2012-03-13 09:23:48

0

我相信你的問題是你卸載事件。 「$(窗口).unload」。至少我知道,在卸載或卸載事件之前,你無法做很多事情。因此,您的瀏覽器可能永遠不會觸發取消訂閱()。如果你看到Atmospheres jquery pubsub樣本,你可以在連接前看到退訂, function connect(){ unsubscribe();} ...

您可以編碼循環來檢查廣播公司,通過推送微不足道的數據來定期驗證廣播公司是否正在清理。我需要更多的研究氣氛,希望有更好的解決方案。希望您可以在刷新創建新連接時清理線程,並讓舊用戶在用戶離開時使用會話過期。

+0

感謝您的關注!我不認爲它是取消訂閱:我試圖通過單擊按鈕明確調用取消訂閱。我認爲這個問題在某種程度上與Tomcat servlet在SpringSource Tool Suite中運行的環境Grails + AtmospherePlugin綁定,或者至少與Grails + Tomcat綁定。後來,我決定通過簡單地將我的所有網站結構更改爲ajax來解決這個問題(幸運的是,這只是一個小型演示而不是真正的產品),但最終我嘗試使用Jetty 8,並使用Jetty 8監控網絡資源jmx控制檯 - 問題沒有被觀察到! – conceptacid 2012-04-24 07:52:53

+0

我們在Wicket + Atmosphere + Tomcat組合中遇到了完全相同的問題 - 所以它看起來像Tomcat + Atmosphere(連接器的默認設置 - BlockingIOCometSupport) – 2014-02-26 13:01:13