HttpSessionListenerの例 - 監視

HttpSessionListenerの例–監視

1. 概要

このチュートリアルでは、metricsを使用して、register a javax.servlet.http.HttpSessionListenerを実行し、Webアプリケーションでアクティブなセッションの数を追跡する方法を示します。

2. リスナーの定義

HTTPセッションリスナーをweb.xmlに登録できます。


    
        org.example.web.SessionListenerWithMetrics
    

または、サーブレット3環境では、we can use the @WebListenerを使用してリスナーも登録します。 この場合、メインのSpringBootApplicationクラスに@ServletComponentScan.アノテーションを付ける必要があります。

最後に、Java構成by declaring a ServletListenerRegistrationBean beanを使用してリスナーを登録することもできます。

@Bean
public ServletListenerRegistrationBean sessionListenerWithMetrics() {
   ServletListenerRegistrationBean listenerRegBean =
     new ServletListenerRegistrationBean<>();

   listenerRegBean.setListener(new SessionListenerWithMetrics());
   return listenerRegBean;
}

3. 基本的なリスナー

単純なリスナーは常にkeep track of the number of active sessionsになります。

public class SessionListenerWithMetrics implements HttpSessionListener {

    private final AtomicInteger activeSessions;

    public SessionListenerWithMetrics() {
        super();

        activeSessions = new AtomicInteger();
    }

    public int getTotalActiveSession() {
        return activeSessions.get();
    }

    public void sessionCreated(final HttpSessionEvent event) {
        activeSessions.incrementAndGet();
    }
    public void sessionDestroyed(final HttpSessionEvent event) {
        activeSessions.decrementAndGet();
    }
}

セッションリスナーは、セッションが作成されたときにトリガーされます–sessionCreated

HttpSession session = request.getSession();

そして破壊された–sessionDestroyed

session.invalidate();

このメカニズムにより、現在のセッションカウントをリスナーから取得できますが、real-time monitoring and transparencyを取得するには、実際に値を取得して公開するための追加のロジックが必要です。

これがメトリクスライブラリの出番です。several out of the box reportersが付属しているため、わずかな労力でこのメトリクスを公開できます。

4. メトリックを持つリスナー

そのため、独自のカスタム監視ソリューションを展開する代わりに、the metrics libraryを活用します。それをpomに追加する必要があります。


    com.codahale.metrics
    metrics-core
    3.0.1

クラスパスでメトリックコアを使用できるので、Counterオブジェクトを使用して同じHttpSessionListenerを書き込むことができます。

public class SessionListenerWithMetrics implements HttpSessionListener {

    private final Counter counterOfActiveSessions;

    public SessionListenerWithMetrics() {
        super();
        counterOfActiveSessions = MetricRegistrySingleton.metrics.counter("web.sessions.active.count");
    }

    public void sessionCreated(final HttpSessionEvent event) {
        counterOfActiveSessions.inc();
    }
    public void sessionDestroyed(final HttpSessionEvent event) {
        counterOfActiveSessions.dec();
    }
}

MetricRegistry(すべてのアプリケーションメトリックの中央レジストリ)は、アプリケーション全体の静的フィールドで単純に参照されます。

public final class MetricRegistrySingleton {
    public static final MetricRegistry metrics = new MetricRegistry();
}

このメトリックを公開し、たとえばアプリケーションの標準のログシステムに監視できるようにすることは簡単です。

Logger logger = LoggerFactory.getLogger("org.example.monitoring");
Slf4jReporter reporter = Slf4jReporter.forRegistry(metrics).outputTo(logger).
  convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
reporter.start(5, TimeUnit.MINUTES);

5. 結論

このチュートリアルでは、Webアプリケーションのデプロイメント記述子にHttpSessionListenerを登録する方法と、2つのメカニズムを使用してアクティブなセッション数を監視する方法について説明しました。 最初のメカニズムは手巻きカウンターで、2番目のメカニズムは成熟したmetricsライブラリに基づいています。

実装はthe example github projectにあります。