001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.monitor;
023:
024: import org.jboss.util.NestedRuntimeException;
025: import org.jboss.logging.Logger;
026: import org.jboss.system.ServiceMBeanSupport;
027:
028: import java.util.List;
029:
030: import javax.management.ObjectName;
031: import javax.management.MBeanServer;
032: import javax.management.MBeanRegistration;
033: import java.util.ArrayList;
034: import java.util.Date;
035: import java.util.LinkedList;
036:
037: /**
038: * Comment
039: *
040: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
041: * @version $Revision: 57210 $
042: *
043: **/
044: public class SnapshotRecordingMonitor implements Runnable,
045: SnapshotRecordingMonitorMBean, MBeanRegistration {
046: protected Logger log;
047: protected String monitorName;
048: protected ObjectName observedObject;
049: protected String attribute;
050: protected boolean recording;
051: protected long period;
052: protected ArrayList history;
053: protected long startTime;
054: protected long endTime;
055: protected MBeanServer mbeanServer;
056:
057: public SnapshotRecordingMonitor() {
058: log = Logger.getLogger(monitorName);
059: history = new ArrayList(100);
060: }
061:
062: protected void startMonitorThread() {
063: Thread t = new Thread(this , "JBoss JMX Attribute Snapshot "
064: + monitorName);
065: t.start();
066: }
067:
068: public String getMonitorName() {
069: return monitorName;
070: }
071:
072: public void setMonitorName(String name) {
073: monitorName = name;
074: }
075:
076: public ObjectName getObservedObject() {
077: return observedObject;
078: }
079:
080: public void setObservedObject(ObjectName oname) {
081: this .observedObject = oname;
082: }
083:
084: public String getObservedAttribute() {
085: return attribute;
086: }
087:
088: public void setObservedAttribute(String attr) {
089: attribute = attr;
090: }
091:
092: public boolean isRecording() {
093: return recording;
094: }
095:
096: public void setRecording(boolean start) {
097: if (start == recording)
098: return;
099: recording = start;
100:
101: if (start) {
102: startMonitorThread();
103: }
104: }
105:
106: public long getPeriod() {
107: return period;
108: }
109:
110: public void setPeriod(long period) {
111: this .period = period;
112: }
113:
114: public ArrayList getData() {
115: return history;
116: }
117:
118: public void clearData() {
119: history.clear();
120: }
121:
122: public void startSnapshot() {
123: history.clear();
124: setRecording(true);
125: }
126:
127: public void endSnapshot() {
128: recording = false;
129: }
130:
131: public long getStartTime() {
132: return startTime;
133: }
134:
135: public long getEndTime() {
136: return endTime;
137: }
138:
139: public void run() {
140: startTime = System.currentTimeMillis();
141: while (recording) {
142: try {
143: Object value = mbeanServer.getAttribute(observedObject,
144: attribute);
145: history.add(value);
146: endTime = System.currentTimeMillis();
147: } catch (Exception ex) {
148: log.error(monitorName + " had error while monitoring",
149: ex);
150: }
151: if (recording) {
152: try {
153: Thread.sleep(period);
154: } catch (InterruptedException ignored) {
155: }
156: }
157: }
158: }
159:
160: // MBeanRegistrationImplementation overrides ---------------------
161:
162: public ObjectName preRegister(MBeanServer server,
163: ObjectName objectName) throws Exception {
164: mbeanServer = server;
165: return objectName;
166: }
167:
168: public void postRegister(Boolean registrationDone) {
169: }
170:
171: public void preDeregister() throws Exception {
172: }
173:
174: public void postDeregister() {
175: }
176:
177: }
|