Filter
を使用して、各要求の時刻を記録し、その時刻をユーザーの集計使用時間に加算するとします。すでに利用可能なセッションがあると仮定して、ユーザーのHttpSession
にそれを保存することができます。
セッションに保存するだけでセッションが終了すると、ユーザーの累積使用時間が失われることに注意してください。セッションの有効期限を過ぎても情報が保持されるようにするには、ディスク上のファイルやリレーショナルデータベースなどの他の場所で使用時間を書き込む必要があります。
上記のコメントの@angryipはFilter
の良い例ですが、リクエストを実際に実行する方法の良い例ではありません。 Filter
のdoFilter
メソッドは非常に簡単です。このようなもの:
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
HttpSession session = null;
if(request instanceof HttpServletRequest)
session = ((HttpServletRequest)request).getSession(false);
// Start the clock
long elapsed = System.currentTimeMillis();
// Use try/finally because the request can throw an exception;
// We still want to capture the usage-time even if an error
// occurs.
try {
chain.doFilter(request, response);
} finally {
// Stop the clock
elapsed = System.currentTimeMillis() - elapsed;
// Store the cumulative usage time in the session
if(null != session) {
Long usage = (Long)session.getAttribute("usage");
if(null != usage)
elapsed = elapsed + usage.getLong();
session.setAttribute("usage", Long.valueOf(elapsed));
}
}
}
セッションの情報を取得するのはあなた次第です。 JMXを使用してその識別子でセッションを要求し、次に "usage"という引数を指定して "getAttribute"を呼び出すことでそれを観察できます。
ここに記載されている解決策を使用する必要があります:http://stackoverflow.com/a/26427149/5655414その時点で行う必要があるのは、現在の時刻をログアウトするだけです。それはあなたが探しているものですか? – angryip