KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > threading > mbean > Threads


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.test.threading.mbean;
23
24 import javax.naming.InitialContext JavaDoc;
25 import org.jboss.test.threading.interfaces.EJBThreads;
26 import org.jboss.test.threading.interfaces.EJBThreadsHome;
27 import java.rmi.RemoteException JavaDoc;
28
29 import java.util.Random JavaDoc;
30
31 /**
32 * This test is there to make sure that the multithreaded version doesn't lock the container
33 * It works in VM and spawns many threads that will ping the server.
34 *
35 * @see <related>
36 * @author <a HREF="mailto:marc@jboss.org">Marc Fleury</a>
37 * @version $Revision: 58115 $
38 *
39 * Revisions:
40 *
41 * 20010524 marc fleury: Initial version
42 */

43
44 public class Threads
45    implements ThreadsMBean
46 {
47    org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass());
48    
49    // Constants -----------------------------------------------------
50

51    // Attributes ----------------------------------------------------
52

53    private int numberOfThreads = 0;
54    private int loops = 10;
55    private long wait = 100;
56    public boolean runMe = true;
57    private Random JavaDoc random = new Random JavaDoc();
58    private Runnable JavaDoc test;
59    private int threadsFinished = 0;
60    // Static --------------------------------------------------------
61

62    // Constructors --------------------------------------------------
63

64     // Public --------------------------------------------------------
65

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       //restart
79
try {
80                 
81          if (numberOfThreads> 0) startMe();
82       }
83       catch (Exception JavaDoc e)
84       {
85          log.debug("failed", e);
86       }
87    }
88    public int getNumberOfThreads() { return numberOfThreads;}
89     
90    public void startMe()
91       throws Exception JavaDoc
92    {
93       runMe = true;
94       threadsFinished = 0;
95       if (numberOfThreads >0) {
96             
97          for (int i = 0; i < numberOfThreads ; i++)
98          {
99             Thread JavaDoc t = new Thread JavaDoc(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 JavaDoc {
115         
116       public void run() {
117             
118          try {
119                 
120             InitialContext JavaDoc ic = new InitialContext JavaDoc();
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 JavaDoc e)
135                {
136                   // Bean wasn't found create it
137
try {
138                      ejbTest = testHome.create("test1");
139                   }
140                     
141                   catch (Exception JavaDoc e2)
142                   {
143                      log.debug("****Create exception: " + e2);
144                   }
145                }
146                     
147                if (ejbTest != null) try {
148                         
149                   // get a random value between 1 and 100
150
int value = random.nextInt(100);
151                         
152                   // 10% removal
153
if (value <10) {
154                      ejbTest.remove();
155                   }
156                   // 35% normal
157
else if (value<45) {
158                      ejbTest.test();
159                   }
160                   // 15% business exception
161
else if (value<60) {
162                      ejbTest.testBusinessException();
163                   }
164                   // 15 % runtime excpetion
165
else if (value <75) {
166                      ejbTest.testRuntimeException();
167                   }
168                   // 15 % nonTransactional
169
else if (value <90) {
170                      ejbTest.testNonTransactional();
171                   }
172                   // 10% timeout
173
else {
174                      ejbTest.testTimeOut();
175                   }
176                         
177                   synchronized (this) {
178                      //Thread.currentThread().yield();
179
this.wait(wait);
180                   }
181                }
182                catch (NullPointerException JavaDoc ignored) {}
183                catch (RemoteException JavaDoc ignored) {}
184                catch (Exception JavaDoc ex)
185                {
186                   log.debug("***Exception thrown: " + ex);
187                }
188                 
189             } // while(runMe)
190
log.debug(Thread.currentThread() + " is finished!!!!");
191             log.debug("Num threads finished is: " + ++threadsFinished);
192          }
193          catch (Exception JavaDoc e) {log.debug("Exception for thread"+Thread.currentThread()); log.debug("failed", e);}
194         
195       }
196    }
197 }
198
Popular Tags