KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > sampler > CpuSamplerTask


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.activemq.tool.sampler;
18
19 import org.apache.activemq.tool.sampler.plugins.CpuSamplerPlugin;
20 import org.apache.activemq.tool.sampler.plugins.LinuxCpuSamplerPlugin;
21 import org.apache.activemq.tool.reports.AbstractPerfReportWriter;
22
23 import java.io.IOException JavaDoc;
24
25 public class CpuSamplerTask extends AbstractPerformanceSampler {
26
27     private CpuSamplerPlugin plugin = null;
28
29     public void createPlugin() throws IOException JavaDoc {
30         createPlugin(System.getProperty("os.name"));
31     }
32
33     public void createPlugin(String JavaDoc osName) throws IOException JavaDoc {
34         if (osName == null) {
35             throw new IOException JavaDoc("No defined OS name found. Found: " + osName);
36         }
37
38         if (osName.equalsIgnoreCase(CpuSamplerPlugin.LINUX)) {
39             plugin = new LinuxCpuSamplerPlugin(getInterval());
40         } else {
41             throw new IOException JavaDoc("No CPU Sampler Plugin found for OS: " + osName + ". CPU Sampler will not be started.");
42         }
43     }
44
45     public void sampleData() {
46         if (plugin != null && perfReportWriter != null) {
47             perfReportWriter.writeCsvData(AbstractPerfReportWriter.REPORT_PLUGIN_CPU, "index=" + sampleIndex + "," + plugin.getCpuUtilizationStats());
48         }
49     }
50
51     protected void onRampUpStart() {
52         super.onRampUpStart();
53         if (plugin != null) {
54             plugin.start();
55         }
56     }
57
58     protected void onRampDownEnd() {
59         super.onRampDownEnd();
60         if (plugin != null) {
61             plugin.stop();
62         }
63     }
64 }
65
Popular Tags