1 22 package org.jboss.test.threading.mbean; 23 24 import javax.naming.InitialContext ; 25 import org.jboss.test.threading.interfaces.EJBThreads; 26 import org.jboss.test.threading.interfaces.EJBThreadsHome; 27 import java.rmi.RemoteException ; 28 29 import java.util.Random ; 30 31 43 44 public class Threads 45 implements ThreadsMBean 46 { 47 org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass()); 48 49 51 53 private int numberOfThreads = 0; 54 private int loops = 10; 55 private long wait = 100; 56 public boolean runMe = true; 57 private Random random = new Random (); 58 private Runnable test; 59 private int threadsFinished = 0; 60 62 64 66 public void setWait(long wait) {this.wait = wait;} 67 public long getWait() {return wait;} 68 69 70 public void setLoops(int loops) {this.loops = loops;} 71 public int getLoops() {return loops;} 72 73 public void setNumberOfThreads(int numberOfThreads) 74 { 75 if (this.numberOfThreads > 0) stopMe(); 76 77 this.numberOfThreads=numberOfThreads; 78 try { 80 81 if (numberOfThreads> 0) startMe(); 82 } 83 catch (Exception e) 84 { 85 log.debug("failed", e); 86 } 87 } 88 public int getNumberOfThreads() { return numberOfThreads;} 89 90 public void startMe() 91 throws Exception 92 { 93 runMe = true; 94 threadsFinished = 0; 95 if (numberOfThreads >0) { 96 97 for (int i = 0; i < numberOfThreads ; i++) 98 { 99 Thread t = new Thread (new Test()); 100 log.debug("started new thread " +t.hashCode()); 101 102 t.start(); 103 104 }; 105 } 106 } 107 108 public void stopMe() 109 { 110 log.debug("Stop called"); 111 runMe = false; 112 }; 113 114 public class Test implements Runnable { 115 116 public void run() { 117 118 try { 119 120 InitialContext ic = new InitialContext (); 121 122 EJBThreadsHome testHome = (EJBThreadsHome) ic.lookup("threads"); 123 124 EJBThreads ejbTest; 125 while(runMe) 126 { 127 128 ejbTest = null; 129 130 try { 131 132 ejbTest = testHome.findByPrimaryKey("test1"); 133 } 134 catch (Exception e) 135 { 136 try { 138 ejbTest = testHome.create("test1"); 139 } 140 141 catch (Exception e2) 142 { 143 log.debug("****Create exception: " + e2); 144 } 145 } 146 147 if (ejbTest != null) try { 148 149 int value = random.nextInt(100); 151 152 if (value <10) { 154 ejbTest.remove(); 155 } 156 else if (value<45) { 158 ejbTest.test(); 159 } 160 else if (value<60) { 162 ejbTest.testBusinessException(); 163 } 164 else if (value <75) { 166 ejbTest.testRuntimeException(); 167 } 168 else if (value <90) { 170 ejbTest.testNonTransactional(); 171 } 172 else { 174 ejbTest.testTimeOut(); 175 } 176 177 synchronized (this) { 178 this.wait(wait); 180 } 181 } 182 catch (NullPointerException ignored) {} 183 catch (RemoteException ignored) {} 184 catch (Exception ex) 185 { 186 log.debug("***Exception thrown: " + ex); 187 } 188 189 } log.debug(Thread.currentThread() + " is finished!!!!"); 191 log.debug("Num threads finished is: " + ++threadsFinished); 192 } 193 catch (Exception e) {log.debug("Exception for thread"+Thread.currentThread()); log.debug("failed", e);} 194 195 } 196 } 197 } 198 | Popular Tags |