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