KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > AbstractJmsClientSystem


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

18 package org.apache.activemq.tool;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.activemq.tool.sampler.ThroughputSamplerTask;
23 import org.apache.activemq.tool.sampler.CpuSamplerTask;
24 import org.apache.activemq.tool.reports.PerformanceReportWriter;
25 import org.apache.activemq.tool.reports.XmlFilePerfReportWriter;
26 import org.apache.activemq.tool.reports.VerbosePerfReportWriter;
27 import org.apache.activemq.tool.properties.JmsClientSystemProperties;
28 import org.apache.activemq.tool.properties.AbstractObjectProperties;
29 import org.apache.activemq.tool.properties.JmsFactoryProperties;
30 import org.apache.activemq.tool.properties.ReflectionUtil;
31 import org.apache.activemq.tool.properties.JmsClientProperties;
32 import org.apache.activemq.tool.spi.SPIConnectionFactory;
33
34 import javax.jms.ConnectionFactory JavaDoc;
35 import javax.jms.JMSException JavaDoc;
36 import javax.jms.ConnectionMetaData JavaDoc;
37 import java.util.Properties JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.File JavaDoc;
41 import java.io.FileInputStream JavaDoc;
42
43 public abstract class AbstractJmsClientSystem extends AbstractObjectProperties {
44     private static final Log log = LogFactory.getLog(AbstractJmsClientSystem.class);
45
46     private int clientDestIndex, clientDestCount;
47
48     protected ThreadGroup JavaDoc clientThreadGroup;
49     protected ConnectionFactory jmsConnFactory;
50
51     // Properties
52
protected JmsFactoryProperties factory = new JmsFactoryProperties();
53     protected ThroughputSamplerTask tpSampler = new ThroughputSamplerTask();
54     protected CpuSamplerTask cpuSampler = new CpuSamplerTask();
55
56     public void runSystemTest() throws JMSException JavaDoc {
57         // Create connection factory
58
jmsConnFactory = loadJmsFactory(getSysTest().getSpiClass(), factory.getFactorySettings());
59
60         setProviderMetaData(jmsConnFactory.createConnection().getMetaData(), getJmsClientProperties());
61
62         // Create performance sampler
63
PerformanceReportWriter writer = createPerfWriter();
64         tpSampler.setPerfReportWriter(writer);
65         cpuSampler.setPerfReportWriter(writer);
66
67         writer.openReportWriter();
68         writer.writeProperties("jvmSettings", System.getProperties());
69         writer.writeProperties("testSystemSettings", ReflectionUtil.retrieveObjectProperties(getSysTest()));
70         writer.writeProperties("jmsFactorySettings", ReflectionUtil.retrieveObjectProperties(jmsConnFactory));
71         writer.writeProperties("jmsClientSettings", ReflectionUtil.retrieveObjectProperties(getJmsClientProperties()));
72         writer.writeProperties("tpSamplerSettings", ReflectionUtil.retrieveObjectProperties(tpSampler));
73         writer.writeProperties("cpuSamplerSettings", ReflectionUtil.retrieveObjectProperties(cpuSampler));
74
75         clientThreadGroup = new ThreadGroup JavaDoc(getSysTest().getClientPrefix() + " Thread Group");
76         for (int i=0; i<getSysTest().getNumClients(); i++) {
77             distributeDestinations(getSysTest().getDestDistro(), i, getSysTest().getNumClients(), getSysTest().getTotalDests());
78
79             final String JavaDoc clientName = getSysTest().getClientPrefix() + i;
80             final int clientDestIndex = this.clientDestIndex;
81             final int clientDestCount = this.clientDestCount;
82             Thread JavaDoc t = new Thread JavaDoc(clientThreadGroup, new Runnable JavaDoc() {
83                 public void run() {
84                     runJmsClient(clientName, clientDestIndex, clientDestCount);
85                 }
86             });
87             t.setName(getSysTest().getClientPrefix() + i + " Thread");
88             t.start();
89         }
90
91         // Run samplers
92
if (getSysTest().getSamplers().indexOf(JmsClientSystemProperties.SAMPLER_TP) > -1) {
93             tpSampler.startSampler();
94         }
95
96         if (getSysTest().getSamplers().indexOf(JmsClientSystemProperties.SAMPLER_CPU) > -1) {
97             try {
98                 cpuSampler.createPlugin();
99                 cpuSampler.startSampler();
100             } catch (IOException JavaDoc e) {
101                 log.warn("Unable to start CPU sampler plugin. Reason: " + e.getMessage());
102             }
103         }
104
105         tpSampler.waitUntilDone();
106         cpuSampler.waitUntilDone();
107
108         writer.closeReportWriter();
109     }
110
111     public ThroughputSamplerTask getTpSampler() {
112         return tpSampler;
113     }
114
115     public void setTpSampler(ThroughputSamplerTask tpSampler) {
116         this.tpSampler = tpSampler;
117     }
118
119     public CpuSamplerTask getCpuSampler() {
120         return cpuSampler;
121     }
122
123     public void setCpuSampler(CpuSamplerTask cpuSampler) {
124         this.cpuSampler = cpuSampler;
125     }
126
127     public JmsFactoryProperties getFactory() {
128         return factory;
129     }
130
131     public void setFactory(JmsFactoryProperties factory) {
132         this.factory = factory;
133     }
134
135     public abstract JmsClientSystemProperties getSysTest();
136     public abstract void setSysTest(JmsClientSystemProperties sysTestProps);
137     public abstract JmsClientProperties getJmsClientProperties();
138
139     protected PerformanceReportWriter createPerfWriter() {
140         if (getSysTest().getReportType().equalsIgnoreCase(JmsClientSystemProperties.REPORT_XML_FILE)) {
141             String JavaDoc reportName;
142
143             if ((reportName = getSysTest().getReportName()) == null) {
144                 reportName = getSysTest().getClientPrefix() + "_" +
145                              "numClients" + getSysTest().getNumClients() + "_" +
146                              "numDests" + getSysTest().getTotalDests() + "_" +
147                              getSysTest().getDestDistro();
148             }
149             return new XmlFilePerfReportWriter(getSysTest().getReportDir(), reportName);
150         } else if (getSysTest().getReportType().equalsIgnoreCase(JmsClientSystemProperties.REPORT_VERBOSE)) {
151             return new VerbosePerfReportWriter();
152         } else {
153             // Use verbose if unknown report type
154
return new VerbosePerfReportWriter();
155         }
156     }
157
158     protected void distributeDestinations(String JavaDoc distroType, int clientIndex, int numClients, int numDests) {
159         if (distroType.equalsIgnoreCase(JmsClientSystemProperties.DEST_DISTRO_ALL)) {
160             clientDestCount = numDests;
161             clientDestIndex = 0;
162         } else if (distroType.equalsIgnoreCase(JmsClientSystemProperties.DEST_DISTRO_EQUAL)) {
163             int destPerClient = (numDests / numClients);
164             // There are equal or more destinations per client
165
if (destPerClient > 0) {
166                 clientDestCount = destPerClient;
167                 clientDestIndex = destPerClient * clientIndex;
168             // If there are more clients than destinations, share destinations per client
169
} else {
170                 clientDestCount = 1; // At most one destination per client
171
clientDestIndex = clientIndex % numDests;
172             }
173         } else if (distroType.equalsIgnoreCase(JmsClientSystemProperties.DEST_DISTRO_DIVIDE)) {
174             int destPerClient = (numDests / numClients);
175             // There are equal or more destinations per client
176
if (destPerClient > 0) {
177                 int remain = numDests % numClients;
178                 int nextIndex;
179                 if (clientIndex < remain) {
180                     destPerClient++;
181                     nextIndex = clientIndex * destPerClient;
182                 } else {
183                     nextIndex = (clientIndex * destPerClient) + remain;
184                 }
185
186                 clientDestCount = destPerClient;
187                 clientDestIndex = nextIndex;
188
189             // If there are more clients than destinations, share destinations per client
190
} else {
191                 clientDestCount = 1; // At most one destination per client
192
clientDestIndex = clientIndex % numDests;
193             }
194
195         // Send to all for unknown behavior
196
} else {
197             log.warn("Unknown destination distribution type: " + distroType);
198             clientDestCount = numDests;
199             clientDestIndex = 0;
200         }
201     }
202
203     protected ConnectionFactory loadJmsFactory(String JavaDoc spiClass, Properties factorySettings) throws JMSException JavaDoc {
204         try {
205             Class JavaDoc spi = Class.forName(spiClass);
206             SPIConnectionFactory spiFactory = (SPIConnectionFactory)spi.newInstance();
207             ConnectionFactory jmsFactory = spiFactory.createConnectionFactory(factorySettings);
208             log.info("Created: " + jmsFactory.getClass().getName() + " using SPIConnectionFactory: " + spiFactory.getClass().getName());
209             return jmsFactory;
210         } catch (Exception JavaDoc e) {
211             e.printStackTrace();
212             throw new JMSException JavaDoc(e.getMessage());
213         }
214     }
215
216     protected void setProviderMetaData(ConnectionMetaData JavaDoc metaData, JmsClientProperties props) throws JMSException JavaDoc {
217         props.setJmsProvider(metaData.getJMSProviderName() + "-" + metaData.getProviderVersion());
218         props.setJmsVersion(metaData.getJMSVersion());
219
220         String JavaDoc jmsProperties = "";
221         Enumeration JavaDoc jmsProps = metaData.getJMSXPropertyNames();
222         while (jmsProps.hasMoreElements()) {
223             jmsProperties += (jmsProps.nextElement().toString() + ",");
224         }
225         if (jmsProperties.length() > 0) {
226             // Remove the last comma
227
jmsProperties = jmsProperties.substring(0, jmsProperties.length()-1);
228         }
229         props.setJmsProperties(jmsProperties);
230     }
231
232     protected abstract void runJmsClient(String JavaDoc clientName, int clientDestIndex, int clientDestCount);
233
234     protected static Properties parseStringArgs(String JavaDoc[] args) {
235         File JavaDoc configFile = null;
236         Properties props = new Properties();
237
238         if (args == null || args.length == 0) {
239             return props; // Empty properties
240
}
241
242         for (int i=0; i<args.length; i++) {
243             String JavaDoc arg = args[i];
244             if (arg.startsWith("-D") || arg.startsWith("-d")) {
245                 arg = arg.substring(2);
246             }
247             int index = arg.indexOf("=");
248             String JavaDoc key = arg.substring(0, index);
249             String JavaDoc val = arg.substring(index + 1);
250
251             if (key.equalsIgnoreCase("sysTest.propsConfigFile")) {
252                 if (!val.endsWith(".properties")) {
253                     val += ".properties";
254                 }
255                 configFile = new File JavaDoc(val);
256             }
257             props.setProperty(key, val);
258         }
259
260         Properties fileProps = new Properties();
261         try {
262             if (configFile != null) {
263                 log.info("Loading properties file: " + configFile.getAbsolutePath());
264                 fileProps.load(new FileInputStream JavaDoc(configFile));
265             }
266         } catch (IOException JavaDoc e) {
267             e.printStackTrace();
268         }
269         // Overwrite file settings with command line settings
270
fileProps.putAll(props);
271         return fileProps;
272     }
273 }
274
Popular Tags