1 25 26 package org.objectweb.jonas.stests.bank; 27 28 import java.rmi.RemoteException ; 29 import java.rmi.ServerException ; 30 31 import javax.ejb.RemoveException ; 32 import javax.naming.Context ; 33 import javax.rmi.PortableRemoteObject ; 34 import javax.transaction.TransactionRolledbackException ; 35 36 public class A_thread extends Thread { 37 String managerName; 38 String name; 39 int ope; 40 int accmin; 41 int accmax; 42 int amount; 43 int loops; 44 int num; 45 boolean pf; 46 Context ictx; 47 Manager mgr = null; 48 ManagerHome mgrHome = null; 49 50 public A_thread(String mname, int num, Context ictx, int ope, int accmin, int accmax, int loops, int amount, boolean pf) { 51 this.managerName = mname; 52 name = managerName + "." + ope + "." + num; 53 setName(name); 54 this.num = num; 55 this.ope = ope; 56 this.ictx = ictx; 57 this.accmin = accmin; 58 this.accmax = accmax; 59 this.loops = loops; 60 this.amount = amount; 61 this.pf = pf; 62 } 63 64 public void run() { 65 66 try { 68 mgrHome = (ManagerHome) PortableRemoteObject.narrow(ictx.lookup(managerName), ManagerHome.class); 69 mgr = mgrHome.create(A_bank.initialValue, pf); 70 } catch (Exception e) { 71 System.out.println("Cannot Create Session:" + e); 72 return; 73 } 74 75 try { 76 switch (ope) { 77 case A_bank.OP_READ: 78 opRead(false); 79 break; 80 case A_bank.OP_READTX: 81 opRead(true); 82 break; 83 case A_bank.OP_MOVE: 84 opMove(); 85 break; 86 case A_bank.OP_MOVETO: 87 opMoveFromTo(amount/10 + num); 88 break; 89 case A_bank.OP_ONEMOVE: 90 if (num == 1) { 91 opMove(); 92 } else { 93 opRead(false); 94 } 95 break; 96 case A_bank.OP_ONEMOVETX: 97 if (num == 1) { 98 opMove(); 99 } else { 100 opRead(true); 101 } 102 break; 103 case A_bank.OP_CREATE: 104 opCreate(); 105 break; 106 case A_bank.OP_REMOVE: 107 opRemove(); 108 break; 109 default: 110 System.out.println("Bad OP: " + ope); 111 return; 112 } 113 } catch (RemoteException e) { 114 System.out.println("Thread " + name + " : " + e); 115 A_bank.threadfail = true; 116 } catch (RemoveException e) { 117 System.out.println("Thread " + name + " : " + e); 118 A_bank.threadfail = true; 119 } finally { 120 try { 121 mgr.remove(); 123 } catch (Exception e) { 124 System.out.println("Thread " + name + " : " + e); 125 A_bank.threadfail = true; 126 } 127 } 128 } 129 130 private void opRead(boolean tx) throws RemoteException { 131 int acc = accmin + num; 132 for (int i = 0; i < loops; i++) { 133 acc++; 134 if (acc > accmax) { 135 acc = accmin; 136 } 137 int bal = tx ? mgr.readBalanceTx(acc) : mgr.readBalance(acc); 138 if (bal < 0) { 139 System.out.println("Thread " + name + " : account " + acc + ", negative balance = " + bal); 140 } 141 } 142 } 143 144 private void opMoveFromTo(int delay) throws RemoteException { 145 int cre = (num % 2) == 0 ? accmin : accmax; 146 int deb = (num % 2) == 0 ? accmax : accmin; 147 try { 148 mgr.move(deb, cre, amount, delay); 149 } catch (TransactionRolledbackException e) { 150 System.out.println("Thread " + name + " : " + e); 152 } catch (ServerException e) { 153 if (e.detail instanceof TransactionRolledbackException ) { 154 System.out.println("Thread " + name + " : " + e.detail); 155 } else { 156 throw e; 157 } 158 } 159 } 160 161 private void opMove() throws RemoteException { 162 int incr = num % (accmax - accmin); 163 int cre = accmin + incr; 164 int deb = accmin + incr + 1; 165 for (int i = 0; i < loops; i++) { 166 cre++; 167 if (cre > accmax) { 168 cre = accmin; 169 } 170 deb += 2; 171 if (deb > accmax) { 172 deb = accmin + 1; 173 } 174 try { 175 mgr.move(deb, cre, amount, 0); 176 } catch (TransactionRolledbackException e) { 177 System.out.println("Thread " + name + " : " + e); 179 } catch (ServerException e) { 180 if (e.detail instanceof TransactionRolledbackException ) { 181 System.out.println("Thread " + name + " : " + e.detail); 182 } else { 183 throw e; 184 } 185 } 186 } 187 } 188 189 private void opCreate() throws RemoteException { 190 int acc = accmin + 10 * num; 191 for (int i = 0; i < loops; i++) { 192 acc++; 193 if (acc > accmax) { 194 acc = accmin; 195 } 196 int bal = mgr.readBalanceTx(acc); 197 if (bal < 0) { 198 System.out.println("Thread " + name + " : account " + acc + ", negative balance = " + bal); 199 } 200 } 201 } 202 203 private void opRemove() throws RemoteException , RemoveException { 204 int acc = accmin + 20 * num; 205 for (int i = 0; i < loops; i++) { 206 acc++; 207 if (acc > accmax) { 208 acc = accmin; 209 } 210 mgr.delAccount(acc); 211 } 212 } 213 214 } 215 216 217 | Popular Tags |