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.tm.mbean2;
023:
024: import org.jboss.system.ServiceMBeanSupport;
025: import org.jboss.test.tm.resource.MTOperation;
026:
027: /**
028: * Server Side MultiThread TM test.
029: *
030: * Based on mbean.TMTest
031: *
032: * @author <a href="dimitris@jboss.org">Dimitris Andreadis</a>
033: * @version $Revision: 57211 $
034: */
035: public class MTTest extends ServiceMBeanSupport implements MTTestMBean {
036: public void testMTOperations(String test, MTOperation[][] ops)
037: throws Exception {
038: log.info("*** Starting test: " + test);
039: MTOperation.init(log);
040:
041: // find out how many MTOperation[]
042: // we'll execute each in a separate thread
043: int numOfThreads = ops.length;
044: log.info("Number of Threads: " + numOfThreads);
045:
046: Thread[] threads = new Thread[numOfThreads];
047: ExecTask[] tasks = new ExecTask[numOfThreads];
048: for (int i = 0; i < numOfThreads; i++) {
049: tasks[i] = new ExecTask(i, ops[i]);
050: threads[i] = new Thread(tasks[i]);
051: threads[i].start();
052: }
053:
054: // join the threads before returning and check
055: // if any of threads exited with an exception
056: Exception caughtException = null;
057: for (int i = 0; i < numOfThreads; i++) {
058: try {
059: threads[i].join();
060: if (tasks[i].exception != null) {
061: // remember any exception caught; order here is not important,
062: // since we don't know which thread finished first.
063: caughtException = tasks[i].exception;
064: }
065: } catch (InterruptedException e) {
066: // retry
067: i--;
068: }
069: }
070: log.info("*** Finished test: " + test);
071: MTOperation.destroy();
072:
073: if (caughtException != null) {
074: throw caughtException;
075: }
076: }
077:
078: private class ExecTask implements Runnable {
079: int threadId;
080: MTOperation[] ops;
081: Exception exception;
082:
083: public ExecTask(int threadId, MTOperation[] ops) {
084: this .threadId = threadId;
085: this .ops = ops;
086: }
087:
088: public void run() {
089: log.info("Starting thread: "
090: + Thread.currentThread().getName());
091: try {
092: for (int i = 0; i < ops.length; ++i) {
093: ops[i].perform();
094: }
095: } catch (Exception e) {
096: exception = e;
097: } finally {
098: log.info("Finished thread: "
099: + Thread.currentThread().getName());
100: }
101: }
102: }
103: }
|