KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > monitor > SnapshotRecordingMonitor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.monitor;
23
24 import org.jboss.util.NestedRuntimeException;
25 import org.jboss.logging.Logger;
26 import org.jboss.system.ServiceMBeanSupport;
27
28 import java.util.List JavaDoc;
29
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.MBeanRegistration JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.LinkedList JavaDoc;
36
37 /**
38  * Comment
39  *
40  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
41  * @version $Revision: 37459 $
42  *
43  **/

44 public class SnapshotRecordingMonitor implements Runnable JavaDoc, SnapshotRecordingMonitorMBean, MBeanRegistration JavaDoc
45 {
46    protected Logger log;
47    protected String JavaDoc monitorName;
48    protected ObjectName JavaDoc observedObject;
49    protected String JavaDoc attribute;
50    protected boolean recording;
51    protected long period;
52    protected ArrayList JavaDoc history;
53    protected long startTime;
54    protected long endTime;
55    protected MBeanServer JavaDoc mbeanServer;
56
57    public SnapshotRecordingMonitor()
58    {
59       log = Logger.getLogger(monitorName);
60       history = new ArrayList JavaDoc(100);
61    }
62
63    protected void startMonitorThread()
64    {
65       Thread JavaDoc t = new Thread JavaDoc(this, "JBoss JMX Attribute Snapshot " + monitorName);
66       t.start();
67    }
68
69    public String JavaDoc getMonitorName()
70    {
71       return monitorName;
72    }
73
74    public void setMonitorName(String JavaDoc name)
75    {
76       monitorName = name;
77    }
78
79    public ObjectName JavaDoc getObservedObject()
80    {
81       return observedObject;
82    }
83
84    public void setObservedObject(ObjectName JavaDoc oname)
85    {
86       this.observedObject = oname;
87    }
88
89    public String JavaDoc getObservedAttribute()
90    {
91       return attribute;
92    }
93
94    public void setObservedAttribute(String JavaDoc attr)
95    {
96       attribute = attr;
97    }
98
99    public boolean isRecording() { return recording; }
100    public void setRecording(boolean start)
101    {
102       if (start == recording) return;
103       recording = start;
104
105       if (start)
106       {
107          startMonitorThread();
108       }
109    }
110
111    public long getPeriod()
112    {
113       return period;
114    }
115
116    public void setPeriod(long period)
117    {
118       this.period = period;
119    }
120
121    public ArrayList JavaDoc getData()
122    {
123       return history;
124    }
125
126    public void clearData()
127    {
128       history.clear();
129    }
130
131    public void startSnapshot()
132    {
133       history.clear();
134       setRecording(true);
135    }
136
137    public void endSnapshot()
138    {
139       recording = false;
140    }
141
142    public long getStartTime()
143    {
144       return startTime;
145    }
146
147    public long getEndTime()
148    {
149       return endTime;
150    }
151
152    public void run()
153    {
154       startTime = System.currentTimeMillis();
155       while (recording)
156       {
157          try
158          {
159             Object JavaDoc value = mbeanServer.getAttribute(observedObject, attribute);
160             history.add(value);
161             endTime = System.currentTimeMillis();
162          }
163          catch (Exception JavaDoc ex)
164          {
165             log.error(monitorName + " had error while monitoring", ex);
166          }
167          if (recording)
168          {
169             try
170             {
171                Thread.sleep(period);
172             }
173             catch (InterruptedException JavaDoc ignored)
174             {
175             }
176          }
177       }
178    }
179
180    // MBeanRegistrationImplementation overrides ---------------------
181

182    public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc objectName)
183      throws Exception JavaDoc
184    {
185       mbeanServer = server;
186       return objectName;
187    }
188
189    public void postRegister(Boolean JavaDoc registrationDone)
190    {
191    }
192
193    public void preDeregister()
194      throws Exception JavaDoc
195    {
196    }
197
198    public void postDeregister()
199    {
200    }
201
202
203 }
204
Popular Tags