KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > mbean > MTTest


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.jca.mbean;
23
24 import org.jboss.system.ServiceMBeanSupport;
25
26 /**
27  * Server Side MultiThread TM test.
28  *
29  * Based on mbean.TMTest
30  *
31  * @author <a HREF="dimitris@jboss.org">Dimitris Andreadis</a>
32  * @version $Revision: 37406 $
33  */

34 public class MTTest extends ServiceMBeanSupport
35    implements MTTestMBean
36 {
37    public void testMTOperations(String JavaDoc test, MTOperation[][] ops) throws Exception JavaDoc
38    {
39       log.info("*** Starting test: " + test);
40       MTOperation.init(log);
41       
42       // find out how many MTOperation[]
43
// we'll execute each in a separate thread
44
int numOfThreads = ops.length;
45       log.info("Number of Threads: " + numOfThreads);
46          
47       Thread JavaDoc[] threads = new Thread JavaDoc[numOfThreads];
48       ExecTask[] tasks = new ExecTask[numOfThreads];
49       for (int i = 0; i < numOfThreads; i++)
50       {
51          tasks[i] = new ExecTask(i, ops[i]);
52          threads[i] = new Thread JavaDoc(tasks[i]);
53          threads[i].start();
54       }
55       
56       // join the threads before returning and check
57
// if any of threads exited with an exception
58
Exception JavaDoc caughtException = null;
59       for (int i = 0; i < numOfThreads; i++)
60       {
61          try
62          {
63             threads[i].join();
64             if (tasks[i].exception != null)
65             {
66                // remember any exception caught; order here is not important,
67
// since we don't know which thread finished first.
68
caughtException = tasks[i].exception;
69             }
70          }
71          catch (InterruptedException JavaDoc e)
72          {
73             // retry
74
i--;
75          }
76       }
77       log.info("*** Finished test: " + test);
78       MTOperation.destroy();
79       
80       if (caughtException != null)
81       {
82          throw caughtException;
83       }
84    }
85    
86    private class ExecTask implements Runnable JavaDoc
87    {
88       int threadId;
89       MTOperation[] ops;
90       Exception JavaDoc exception;
91       
92       public ExecTask(int threadId, MTOperation[] ops)
93       {
94          this.threadId = threadId;
95          this.ops = ops;
96       }
97       
98       public void run()
99       {
100          log.info("Starting thread: " + Thread.currentThread().getName());
101          try
102          {
103             for (int i = 0; i < ops.length; ++i)
104             {
105                ops[i].perform();
106             }
107          }
108          catch (Exception JavaDoc e)
109          {
110             exception = e;
111          }
112          finally
113          {
114             log.info("Finished thread: " + Thread.currentThread().getName());
115          }
116       }
117    }
118 }
119
Popular Tags