KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > management > beans > sessions > SessionMonitor


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.management.beans.sessions;
5
6 import java.util.Iterator JavaDoc;
7 import java.util.LinkedList JavaDoc;
8
9 import javax.management.NotCompliantMBeanException JavaDoc;
10
11 import com.tc.management.AbstractTerracottaMBean;
12 import com.tc.management.stats.AggregateInteger;
13
14 public final class SessionMonitor extends AbstractTerracottaMBean implements SessionMonitorMBean {
15
16   final AggregateInteger requestsProcessed;
17   final AggregateInteger sessionsCreated;
18   final AggregateInteger sessionsDestroyed;
19   private final LinkedList JavaDoc sessionsControllers;
20
21   public SessionMonitor() throws NotCompliantMBeanException JavaDoc {
22     super(SessionMonitorMBean.class, false);
23     requestsProcessed = new AggregateInteger("RequestsProcessed", 100);
24     sessionsCreated = new AggregateInteger("CreatedSessionCount", 100);
25     sessionsDestroyed = new AggregateInteger("DestroyedSessionCount", 100);
26     sessionsControllers = new LinkedList JavaDoc();
27   }
28
29   public int getRequestCount() {
30     return requestsProcessed.getN();
31   }
32
33   public int getRequestRatePerSecond() {
34     return requestsProcessed.getSampleRate(1000);
35   }
36
37   public int getCreatedSessionCount() {
38     return sessionsCreated.getN();
39   }
40
41   public int getSessionCreationRatePerMinute() {
42     return sessionsCreated.getSampleRate(1000 * 60);
43   }
44
45   public int getDestroyedSessionCount() {
46     return sessionsDestroyed.getN();
47   }
48
49   public int getSessionDestructionRatePerMinute() {
50     return sessionsDestroyed.getSampleRate(1000 * 60);
51   }
52
53   public synchronized void reset() {
54     requestsProcessed.reset();
55     sessionsCreated.reset();
56     sessionsDestroyed.reset();
57     synchronized (sessionsControllers) {
58       sessionsControllers.clear();
59     }
60   }
61
62   public boolean expireSession(String JavaDoc sessionId) {
63     boolean sessionExpired = false;
64     synchronized (sessionsControllers) {
65       for (Iterator JavaDoc iter = sessionsControllers.iterator(); iter.hasNext();) {
66         SessionsComptroller controller = (SessionsComptroller) iter.next();
67         sessionExpired = sessionExpired || controller.killSession(sessionId);
68       }
69     }
70     return sessionExpired;
71   }
72
73   public synchronized void sessionCreated() {
74     if (isEnabled()) sessionsCreated.addSample(1);
75   }
76
77   public synchronized void sessionDestroyed() {
78     if (isEnabled()) sessionsDestroyed.addSample(1);
79   }
80
81   public synchronized void requestProcessed() {
82     if (isEnabled()) requestsProcessed.addSample(1);
83   }
84
85   public synchronized void registerSessionsController(SessionsComptroller controller) {
86     if (isEnabled()) {
87       synchronized (sessionsControllers) {
88         sessionsControllers.add(controller);
89       }
90     }
91   }
92
93 }
94
Popular Tags