1 25 26 package org.objectweb.jonas.jtests.clients.distribution; 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 import org.objectweb.jonas.jtests.beans.bank.Manager; 36 import org.objectweb.jonas.jtests.beans.bank.ManagerHome; 37 38 public class A_thread extends Thread { 39 String managerName; 40 String name; 41 int ope; 42 int accmin; 43 int accmax; 44 int amount; 45 int loops; 46 int num; 47 boolean pf; 48 Context ictx; 49 Manager mgr = null; 50 ManagerHome mgrHome = null; 51 52 public A_thread(String mname, int num, Context ictx, int ope, int accmin, int accmax, int loops, int amount, boolean pf) { 53 this.managerName = mname; 54 name = managerName + "." + ope + "." + num; 55 setName(name); 56 this.num = num; 57 this.ope = ope; 58 this.ictx = ictx; 59 this.accmin = accmin; 60 this.accmax = accmax; 61 this.loops = loops; 62 this.amount = amount; 63 this.pf = pf; 64 } 65 66 public void run() { 67 68 try { 70 mgrHome = (ManagerHome) PortableRemoteObject.narrow(ictx.lookup(managerName), ManagerHome.class); 71 mgr = mgrHome.create(A_bank.initialValue, pf); 72 } catch (Exception e) { 73 System.out.println("Cannot Create Session:" + e); 74 return; 75 } 76 77 try { 78 switch (ope) { 79 case A_bank.OP_READ: 80 opRead(false); 81 break; 82 case A_bank.OP_READTX: 83 opRead(true); 84 break; 85 case A_bank.OP_MOVE: 86 opMove(); 87 break; 88 case A_bank.OP_MOVETO: 89 opMoveFromTo(amount/10 + num); 90 break; 91 case A_bank.OP_ONEMOVE: 92 if (num == 1) { 93 opMove(); 94 } else { 95 opRead(false); 96 } 97 break; 98 case A_bank.OP_ONEMOVETX: 99 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 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.readBalance(acc); 140 if (bal < 0) { 141 System.out.println("Thread " + name + " : account " + acc + ", negative balance = " + bal); 142 } 143 } 144 } 145 146 private void opMoveFromTo(int delay) throws RemoteException { 147 int cre = (num % 2) == 0 ? accmin : accmax; 148 int deb = (num % 2) == 0 ? accmax : accmin; 149 try { 150 mgr.move(deb, cre, amount, delay); 151 } catch (TransactionRolledbackException e) { 152 System.out.println("Thread " + name + " : " + e); 154 } catch (ServerException e) { 155 if (e.detail instanceof TransactionRolledbackException ) { 156 System.out.println("Thread " + name + " : " + e.detail); 157 } else { 158 throw e; 159 } 160 } 161 } 162 163 private void opMove() throws RemoteException { 164 int incr = num % (accmax - accmin); 165 int cre = accmin + incr; 166 int deb = accmin + incr + 1; 167 for (int i = 0; i < loops; i++) { 168 cre++; 169 if (cre > accmax) { 170 cre = accmin; 171 } 172 deb += 2; 173 if (deb > accmax) { 174 deb = accmin + 1; 175 } 176 try { 177 mgr.move(deb, cre, amount, 0); 178 } catch (TransactionRolledbackException e) { 179 System.out.println("Thread " + name + " : " + e); 181 } catch (ServerException e) { 182 if (e.detail instanceof TransactionRolledbackException ) { 183 System.out.println("Thread " + name + " : " + e.detail); 184 } else { 185 throw e; 186 } 187 } 188 } 189 } 190 191 private void opCreate() throws RemoteException { 192 int acc = accmin + 100 * num; 193 for (int i = 0; i < loops; i++) { 194 acc++; 195 if (acc > accmax) { 196 acc = accmin; 197 } 198 199 int bal = mgr.readBalanceTx(acc); 200 if (bal < 0) { 201 System.out.println("Thread " + name + " : account " + acc + ", negative balance = " + bal); 202 } 203 } 204 } 205 206 private void opRemove() throws RemoteException , RemoveException { 207 int acc = accmin + 20 * num; 208 for (int i = 0; i < loops; i++) { 209 acc++; 210 if (acc > accmax) { 211 acc = accmin; 212 } 213 mgr.delAccount(acc); 214 } 215 } 216 217 } 218 219 220 | Popular Tags |