001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: A_thread.java 3862 2003-12-09 12:37:39Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.stests.manac;
027:
028: import java.rmi.RemoteException;
029: import javax.ejb.RemoveException;
030: import javax.naming.Context;
031: import javax.rmi.PortableRemoteObject;
032:
033: /**
034: * Stress Test
035: * @author Philippe Durieux
036: */
037: public class A_thread extends Thread {
038: String managerName;
039: String name;
040: int accounts;
041: int delay;
042: int loops;
043: int amount;
044: Context ictx;
045: int checklevel;
046:
047: public A_thread(String mName, int num, Context ictx, int accounts,
048: int delay, int loops, int amount, int checklevel) {
049: managerName = mName;
050: name = managerName + "_manac_" + num;
051: setName(name);
052: this .ictx = ictx;
053: this .accounts = accounts;
054: this .delay = delay;
055: this .loops = loops;
056: this .amount = amount;
057: this .checklevel = checklevel;
058: }
059:
060: /**
061: * random returns an integer between 0 and max - 1
062: */
063: private int random(int max) throws RemoteException {
064: double d = Math.random();
065: int ret = (int) (max * d);
066: return ret;
067: }
068:
069: public void run() {
070:
071: // Create a session bean
072: ManagerHome mgrHome = null;
073: Manager mgr = null;
074: try {
075: mgrHome = (ManagerHome) PortableRemoteObject.narrow(ictx
076: .lookup(managerName), ManagerHome.class);
077: mgr = mgrHome.create(A_manac.initialValue);
078: } catch (Exception e) {
079: System.out.println("Cannot Create Session:" + e);
080: return;
081: }
082:
083: try {
084: // Set delay and value to move.
085: if (delay > 0) {
086: mgr.setDelay(delay);
087: }
088: if (amount > 0) {
089: mgr.setValue(amount);
090: }
091:
092: // main loop
093: boolean ok = true;
094: for (int i = 0; i < loops; i++) {
095: // Choose the 4 accounts randomly
096: int d1 = random(accounts);
097: int d2 = random(accounts);
098: int c1 = random(accounts);
099: int c2 = random(accounts);
100: if (amount < 0) {
101: // make a simple withdraw.
102: try {
103: mgr.withdraw(d1, c1, -amount);
104: } catch (RemoteException r) {
105: System.out
106: .println("Exception (ignored) raised on withdraw:"
107: + r);
108: }
109: } else {
110: // Set these accounts in session bean
111: try {
112: mgr.setAccounts(d1, d2, c1, c2);
113: } catch (RemoteException r) {
114: System.out.println("Bad Account Setting:" + r);
115: A_manac.threadfail = true;
116: break;
117: }
118: // make the transfert
119: try {
120: mgr.movement();
121: } catch (RemoteException r) {
122: // movement raised exception. ignoring...
123: System.out
124: .println("Exception (ignored) raised on movement:"
125: + r);
126: }
127: }
128: if (checklevel >= 2) {
129: // Check accounts that were debited
130: if (!mgr.checkAccount(d1)) {
131: System.out
132: .println("Bad Account after move on account "
133: + d1);
134: ok = false;
135: }
136: if (!mgr.checkAccount(d2)) {
137: System.out
138: .println("Bad Account after move on account "
139: + d2);
140: ok = false;
141: }
142: if (!ok) {
143: System.out
144: .println("Stopping this session because some accounts are not ok");
145: A_manac.threadfail = true;
146: break;
147: }
148: }
149: }
150: // delete Session
151: mgr.remove();
152: } catch (RemoteException e) {
153: System.out.println("Thread " + name + " : " + e);
154: A_manac.threadfail = true;
155: } catch (RemoveException e) {
156: System.out.println("Thread " + name
157: + " Cannot remove session: " + e);
158: A_manac.threadfail = true;
159: }
160: }
161: }
|