GWTクライアントサーバーアプリケーションがあり、実行中のクライアントの数をクライアントブラウザーのタイトルに表示したいと思います。接続しているクライアントの数を追跡する方法は知っていますが、閉じている接続の数を追跡するにはどうすればよいですか?
public class MyServiceImpl extends RemoteServiceServelt implements MyService
{
[...]
static int runningClients=0;
@Override public Integer runningClients() {return ++runningClients;}
// TODO: track closed connections
}
編集:提案されたように、私はHTTPSessionListenerを実装しました。残念ながら、ヒットとconnectedClientsの両方で常に0を返します。
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class ActiveClientsListener implements HttpSessionListener
{
private static long hits = 0;
private static long numberOfConnectedClients = 0;
public static long hits() {return hits;}
public static long numberOfConnectedClients() {return numberOfConnectedClients;}
@Override public synchronized void sessionCreated(HttpSessionEvent arg0)
{
hits++;
numberOfConnectedClients++;
}
@Override public synchronized void sessionDestroyed(HttpSessionEvent arg0)
{
numberOfConnectedClients--;
}
}
web.xml
...
<listener>
<listener-class>org.mypackage.server.ActiveClientsListener</listener-class>
</listener>
...
PS:私の実装はスレッドセーフですか、volatile
それともフィールドを作成するか、別の方法で同期する必要がありますか?