001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.geronimo.samples.daytrader.client.ws; 018 019 import java.net.URL; 020 import javax.naming.*; 021 022 /* ServiceFactory */ 023 import javax.xml.rpc.*; 024 import javax.xml.namespace.QName; 025 026 /** 027 * Web Services J2EE client for Trade. 028 */ 029 public class ClientScenario extends Thread { 030 031 /** 032 * A flag to determine how to get the port. If true, use JNDI 033 * lookup to get a Service. 034 * To use a ServiceFactory to obtain a Service, set useJNDI false. 035 * If useJNDI is true, the client must be run in a J2EE client container. 036 */ 037 protected static final boolean useJNDI = false; 038 039 //Properties pertaining to this thread instance in the scenario 040 // Request statistics 041 private long numReqs = 0; 042 private long numErrs = 0; 043 private long numStatReqs = 0; // The number of requests since the last statistics cdear 044 private long totResp = 0; 045 private boolean stop = false; 046 047 //Properties pertaining to the entire scenario 048 // remove ?wsdl 049 private static String servicePort = "http://localhost:8080/daytrader/services/TradeWSServices"; 050 private static long numThreads = 0; // # threads in scenario 051 private static long reqPerThread; // # requests for each thread to process 052 private static long startTime = 0; // Time (millis) when the scen. started 053 private static long statStartTime = 0; 054 // Time (millis) demarking the begin time for the stats window 055 private static long intervalStartTime = 0; 056 // Time when the last stats interval started 057 058 private static long totReqsAtLastInterval = 0; 059 060 private static long minResp = Long.MAX_VALUE; 061 private static long maxResp = 0; 062 063 private static final String jndiName = "java:comp/env/service/Trade"; 064 065 public static String symbol = "s:1"; 066 067 private static TradeWSServices tradeSingleton = null; 068 069 public ClientScenario() { 070 } 071 072 public ClientScenario(int reqPerThreadIn) { 073 System.out.println( 074 "Thread " 075 + this.getName() 076 + " ready to execute " 077 + reqPerThread 078 + " iterations"); 079 reqPerThread = reqPerThreadIn; 080 } 081 082 public void run() { 083 TradeWSServices ts; 084 try { 085 ts = getTrade(); 086 for (int i = 0; i < reqPerThread; i++) { 087 try { 088 if (isStop()) { 089 System.out.println("Thread " + this +" stopping"); 090 return; 091 } 092 //TODO -- scenario and need random quote 093 long start = System.currentTimeMillis(); 094 095 //performScenario 096 QuoteDataBean resp = ts.getQuote(symbol); 097 098 //update Statistics 099 long end = System.currentTimeMillis(); 100 long respTime = end - start; 101 totResp += respTime; 102 numReqs++; 103 numStatReqs++; 104 if ((respTime < minResp)) 105 setMinResp(respTime); 106 if (respTime > maxResp) 107 setMaxResp(respTime); 108 } catch (Exception e) { 109 System.out.println("Thread error -- scenario = xxx" + e.toString()); 110 e.printStackTrace(); 111 numErrs++; 112 } 113 } 114 } catch (Exception e) { 115 e.printStackTrace(); 116 } 117 } 118 119 protected long clearStats() { 120 maxResp = totResp = 0; 121 minResp = Long.MAX_VALUE; 122 totResp = 0; 123 numStatReqs = 0; 124 return numReqs; 125 } 126 127 /** 128 * Get a web services port that represents the Trade services. 129 * First try JSR 109 lookup, then fall back on error to JSR 101. 130 * 131 * @return Trade Services Interface 132 * @exception javax.xml.rpc.ServiceException if accessing the service or 133 * port fails. 134 * @exception java.net.MalformedURLException if an invalid URL is obtained. 135 */ 136 protected static TradeWSServices getTrade() throws javax.xml.rpc.ServiceException, java.net.MalformedURLException { 137 TradeWSServices trade; 138 139 // JSR 109 lookup 140 try { 141 InitialContext context = new InitialContext(); 142 143 Trade tradeService1 = (Trade)context.lookup(jndiName); 144 trade = tradeService1.getTradeWSServices(); 145 } 146 catch (Exception e) { 147 System.out.println("JSR 109 lookup failed .. defaulting to JSR 101"); 148 // JSR 101 lookup 149 URL wsdlLoc = new URL(getServicePort()); 150 QName serviceName = new QName("http://daytrader.samples.geronimo.apache.org", "Trade"); 151 Service tService = ServiceFactory.newInstance().createService(wsdlLoc, serviceName); 152 QName portName = new QName("http://daytrader.samples.geronimo.apache.org", "TradeWSServices"); 153 trade = (TradeWSServices)tService.getPort(portName, TradeWSServices.class); 154 } 155 ((Stub)trade)._setProperty("javax.xml.rpc.service.endpoint.address", getServicePort()); 156 return trade; 157 } 158 159 public static TradeWSServices getTradeSingleton() throws Exception { 160 if (tradeSingleton == null) { 161 tradeSingleton = getTrade(); 162 } 163 return tradeSingleton; 164 } 165 166 /** 167 * Returns the intervalStartTime. 168 * @return long 169 */ 170 public static long getIntervalStartTime() { 171 return intervalStartTime; 172 } 173 174 /** 175 * Returns the maxResp. 176 * @return long 177 */ 178 public static long getMaxResp() { 179 return maxResp; 180 } 181 182 /** 183 * Returns the minResp. 184 * @return long 185 */ 186 public static long getMinResp() { 187 return minResp; 188 } 189 190 /** 191 * Returns the numThreads. 192 * @return long 193 */ 194 public static long getNumThreads() { 195 return numThreads; 196 } 197 198 /** 199 * Returns the reqPerThread. 200 * @return long 201 */ 202 public static long getReqPerThread() { 203 return reqPerThread; 204 } 205 206 /** 207 * Returns the startTime. 208 * @return long 209 */ 210 public static long getStartTime() { 211 return startTime; 212 } 213 214 /** 215 * Returns the statStartTime. 216 * @return long 217 */ 218 public static long getStatStartTime() { 219 return statStartTime; 220 } 221 222 /** 223 * Returns the totReqsAtLastInterval. 224 * @return long 225 */ 226 public static long getTotReqsAtLastInterval() { 227 return totReqsAtLastInterval; 228 } 229 230 /** 231 * Returns the numErrs. 232 * @return long 233 */ 234 public long getNumErrs() { 235 return numErrs; 236 } 237 238 /** 239 * Returns the numReqs. 240 * @return long 241 */ 242 public long getNumReqs() { 243 return numReqs; 244 } 245 246 /** 247 * Returns the stop. 248 * @return boolean 249 */ 250 public boolean isStop() { 251 return stop; 252 } 253 254 /** 255 * Returns the totResp. 256 * @return long 257 */ 258 public long getTotResp() { 259 return totResp; 260 } 261 262 /** 263 * Sets the intervalStartTime. 264 * @param intervalStartTime The intervalStartTime to set 265 */ 266 public static void setIntervalStartTime(long intervalStartTime) { 267 ClientScenario.intervalStartTime = intervalStartTime; 268 } 269 270 /** 271 * Sets the maxResp. 272 * @param maxResp The maxResp to set 273 */ 274 public synchronized static void setMaxResp(long maxResp) { 275 ClientScenario.maxResp = maxResp; 276 } 277 278 /** 279 * Sets the minResp. 280 * @param minResp The minResp to set 281 */ 282 public synchronized static void setMinResp(long minResp) { 283 if (minResp > 0 ) 284 ClientScenario.minResp = minResp; 285 } 286 287 /** 288 * Sets the numThreads. 289 * @param numThreads The numThreads to set 290 */ 291 public static void setNumThreads(long numThreads) { 292 ClientScenario.numThreads = numThreads; 293 } 294 295 /** 296 * Sets the reqPerThread. 297 * @param reqPerThread The reqPerThread to set 298 */ 299 public static void setReqPerThread(long reqPerThread) { 300 ClientScenario.reqPerThread = reqPerThread; 301 } 302 303 /** 304 * Sets the startTime. 305 * @param startTime The startTime to set 306 */ 307 public static void setStartTime(long startTime) { 308 ClientScenario.startTime = startTime; 309 } 310 311 /** 312 * Sets the statStartTime. 313 * @param statStartTime The statStartTime to set 314 */ 315 public static void setStatStartTime(long statStartTime) { 316 ClientScenario.statStartTime = statStartTime; 317 } 318 319 /** 320 * Sets the totReqsAtLastInterval. 321 * @param totReqsAtLastInterval The totReqsAtLastInterval to set 322 */ 323 public static void setTotReqsAtLastInterval(long totReqsAtLastInterval) { 324 ClientScenario.totReqsAtLastInterval = totReqsAtLastInterval; 325 } 326 327 328 /** 329 * Sets the numErrs. 330 * @param numErrs The numErrs to set 331 */ 332 public void setNumErrs(long numErrs) { 333 this.numErrs = numErrs; 334 } 335 336 /** 337 * Sets the numReqs. 338 * @param numReqs The numReqs to set 339 */ 340 public void setNumReqs(long numReqs) { 341 this.numReqs = numReqs; 342 } 343 344 /** 345 * Sets the stop. 346 * @param stop The stop to set 347 */ 348 public void setStop(boolean stop) { 349 this.stop = stop; 350 } 351 352 /** 353 * Sets the totResp. 354 * @param totResp The totResp to set 355 */ 356 public void setTotResp(long totResp) { 357 this.totResp = totResp; 358 } 359 360 /** 361 * Returns the totalNumRequests. 362 * @return long 363 */ 364 public static long getTotalNumRequests() { 365 return numThreads * reqPerThread; 366 } 367 368 369 370 /** 371 * Returns the numStatReqs. 372 * @return long 373 */ 374 public long getNumStatReqs() { 375 return numStatReqs; 376 } 377 378 /** 379 * Sets the numStatReqs. 380 * @param numStatReqs The numStatReqs to set 381 */ 382 public void setNumStatReqs(long numStatReqs) { 383 this.numStatReqs = numStatReqs; 384 } 385 386 /** 387 * Returns the servicePort. 388 * @return String 389 */ 390 public static String getServicePort() { 391 return servicePort; 392 } 393 394 /** 395 * Sets the servicePort. 396 * @param servicePort The servicePort to set 397 */ 398 public static void setServicePort(String servicePort) { 399 ClientScenario.servicePort = servicePort; 400 tradeSingleton = null; 401 } 402 403 }