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 4406 2004-03-19 11:57:20Z benoitf $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.stests.bank;
027:
028: import java.rmi.RemoteException;
029: import java.rmi.ServerException;
030:
031: import javax.ejb.RemoveException;
032: import javax.naming.Context;
033: import javax.rmi.PortableRemoteObject;
034: import javax.transaction.TransactionRolledbackException;
035:
036: public class A_thread extends Thread {
037: String managerName;
038: String name;
039: int ope;
040: int accmin;
041: int accmax;
042: int amount;
043: int loops;
044: int num;
045: boolean pf;
046: Context ictx;
047: Manager mgr = null;
048: ManagerHome mgrHome = null;
049:
050: public A_thread(String mname, int num, Context ictx, int ope,
051: int accmin, int accmax, int loops, int amount, boolean pf) {
052: this .managerName = mname;
053: name = managerName + "." + ope + "." + num;
054: setName(name);
055: this .num = num;
056: this .ope = ope;
057: this .ictx = ictx;
058: this .accmin = accmin;
059: this .accmax = accmax;
060: this .loops = loops;
061: this .amount = amount;
062: this .pf = pf;
063: }
064:
065: public void run() {
066:
067: // Create a session bean
068: try {
069: mgrHome = (ManagerHome) PortableRemoteObject.narrow(ictx
070: .lookup(managerName), ManagerHome.class);
071: mgr = mgrHome.create(A_bank.initialValue, pf);
072: } catch (Exception e) {
073: System.out.println("Cannot Create Session:" + e);
074: return;
075: }
076:
077: try {
078: switch (ope) {
079: case A_bank.OP_READ:
080: opRead(false);
081: break;
082: case A_bank.OP_READTX:
083: opRead(true);
084: break;
085: case A_bank.OP_MOVE:
086: opMove();
087: break;
088: case A_bank.OP_MOVETO:
089: opMoveFromTo(amount / 10 + num);
090: break;
091: case A_bank.OP_ONEMOVE:
092: if (num == 1) {
093: opMove();
094: } else {
095: opRead(false);
096: }
097: break;
098: case A_bank.OP_ONEMOVETX:
099: if (num == 1) {
100: opMove();
101: } else {
102: opRead(true);
103: }
104: break;
105: case A_bank.OP_CREATE:
106: opCreate();
107: break;
108: case A_bank.OP_REMOVE:
109: opRemove();
110: break;
111: default:
112: System.out.println("Bad OP: " + ope);
113: return;
114: }
115: } catch (RemoteException e) {
116: System.out.println("Thread " + name + " : " + e);
117: A_bank.threadfail = true;
118: } catch (RemoveException e) {
119: System.out.println("Thread " + name + " : " + e);
120: A_bank.threadfail = true;
121: } finally {
122: try {
123: // delete Session
124: mgr.remove();
125: } catch (Exception e) {
126: System.out.println("Thread " + name + " : " + e);
127: A_bank.threadfail = true;
128: }
129: }
130: }
131:
132: private void opRead(boolean tx) throws RemoteException {
133: int acc = accmin + num;
134: for (int i = 0; i < loops; i++) {
135: acc++;
136: if (acc > accmax) {
137: acc = accmin;
138: }
139: int bal = tx ? mgr.readBalanceTx(acc) : mgr
140: .readBalance(acc);
141: if (bal < 0) {
142: System.out.println("Thread " + name + " : account "
143: + acc + ", negative balance = " + bal);
144: }
145: }
146: }
147:
148: private void opMoveFromTo(int delay) throws RemoteException {
149: int cre = (num % 2) == 0 ? accmin : accmax;
150: int deb = (num % 2) == 0 ? accmax : accmin;
151: try {
152: mgr.move(deb, cre, amount, delay);
153: } catch (TransactionRolledbackException e) {
154: // a possible rollback must not be considered as an error.
155: System.out.println("Thread " + name + " : " + e);
156: } catch (ServerException e) {
157: if (e.detail instanceof TransactionRolledbackException) {
158: System.out.println("Thread " + name + " : " + e.detail);
159: } else {
160: throw e;
161: }
162: }
163: }
164:
165: private void opMove() throws RemoteException {
166: int incr = num % (accmax - accmin);
167: int cre = accmin + incr;
168: int deb = accmin + incr + 1;
169: for (int i = 0; i < loops; i++) {
170: cre++;
171: if (cre > accmax) {
172: cre = accmin;
173: }
174: deb += 2;
175: if (deb > accmax) {
176: deb = accmin + 1;
177: }
178: try {
179: mgr.move(deb, cre, amount, 0);
180: } catch (TransactionRolledbackException e) {
181: // a possible rollback must not be considered as an error.
182: System.out.println("Thread " + name + " : " + e);
183: } catch (ServerException e) {
184: if (e.detail instanceof TransactionRolledbackException) {
185: System.out.println("Thread " + name + " : "
186: + e.detail);
187: } else {
188: throw e;
189: }
190: }
191: }
192: }
193:
194: private void opCreate() throws RemoteException {
195: int acc = accmin + 10 * num;
196: for (int i = 0; i < loops; i++) {
197: acc++;
198: if (acc > accmax) {
199: acc = accmin;
200: }
201: int bal = mgr.readBalanceTx(acc);
202: if (bal < 0) {
203: System.out.println("Thread " + name + " : account "
204: + acc + ", negative balance = " + bal);
205: }
206: }
207: }
208:
209: private void opRemove() throws RemoteException, RemoveException {
210: int acc = accmin + 20 * num;
211: for (int i = 0; i < loops; i++) {
212: acc++;
213: if (acc > accmax) {
214: acc = accmin;
215: }
216: mgr.delAccount(acc);
217: }
218: }
219:
220: }
|