001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jca.mbean;
023:
024: import org.jboss.system.ServiceMBeanSupport;
025:
026: /**
027: * Server Side MultiThread TM test.
028: *
029: * Based on mbean.TMTest
030: *
031: * @author <a href="dimitris@jboss.org">Dimitris Andreadis</a>
032: * @version $Revision: 57211 $
033: */
034: public class MTTest extends ServiceMBeanSupport implements MTTestMBean {
035: public void testMTOperations(String test, MTOperation[][] ops)
036: throws Exception {
037: log.info("*** Starting test: " + test);
038: MTOperation.init(log);
039:
040: // find out how many MTOperation[]
041: // we'll execute each in a separate thread
042: int numOfThreads = ops.length;
043: log.info("Number of Threads: " + numOfThreads);
044:
045: Thread[] threads = new Thread[numOfThreads];
046: ExecTask[] tasks = new ExecTask[numOfThreads];
047: for (int i = 0; i < numOfThreads; i++) {
048: tasks[i] = new ExecTask(i, ops[i]);
049: threads[i] = new Thread(tasks[i]);
050: threads[i].start();
051: }
052:
053: // join the threads before returning and check
054: // if any of threads exited with an exception
055: Exception caughtException = null;
056: for (int i = 0; i < numOfThreads; i++) {
057: try {
058: threads[i].join();
059: if (tasks[i].exception != null) {
060: // remember any exception caught; order here is not important,
061: // since we don't know which thread finished first.
062: caughtException = tasks[i].exception;
063: }
064: } catch (InterruptedException e) {
065: // retry
066: i--;
067: }
068: }
069: log.info("*** Finished test: " + test);
070: MTOperation.destroy();
071:
072: if (caughtException != null) {
073: throw caughtException;
074: }
075: }
076:
077: private class ExecTask implements Runnable {
078: int threadId;
079: MTOperation[] ops;
080: Exception exception;
081:
082: public ExecTask(int threadId, MTOperation[] ops) {
083: this .threadId = threadId;
084: this .ops = ops;
085: }
086:
087: public void run() {
088: log.info("Starting thread: "
089: + Thread.currentThread().getName());
090: try {
091: for (int i = 0; i < ops.length; ++i) {
092: ops[i].perform();
093: }
094: } catch (Exception e) {
095: exception = e;
096: } finally {
097: log.info("Finished thread: "
098: + Thread.currentThread().getName());
099: }
100: }
101: }
102: }
|