KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > stests > bank > A_thread


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: A_thread.java,v 1.6 2004/03/19 11:57:16 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.stests.bank;
27
28 import java.rmi.RemoteException JavaDoc;
29 import java.rmi.ServerException JavaDoc;
30
31 import javax.ejb.RemoveException JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.rmi.PortableRemoteObject JavaDoc;
34 import javax.transaction.TransactionRolledbackException JavaDoc;
35
36 public class A_thread extends Thread JavaDoc {
37     String JavaDoc managerName;
38     String JavaDoc name;
39     int ope;
40     int accmin;
41     int accmax;
42     int amount;
43     int loops;
44     int num;
45     boolean pf;
46     Context JavaDoc ictx;
47     Manager mgr = null;
48     ManagerHome mgrHome = null;
49
50     public A_thread(String JavaDoc mname, int num, Context JavaDoc 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         // Create a session bean
67
try {
68             mgrHome = (ManagerHome) PortableRemoteObject.narrow(ictx.lookup(managerName), ManagerHome.class);
69             mgr = mgrHome.create(A_bank.initialValue, pf);
70         } catch (Exception JavaDoc 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 JavaDoc e) {
114             System.out.println("Thread " + name + " : " + e);
115             A_bank.threadfail = true;
116         } catch (RemoveException JavaDoc e) {
117             System.out.println("Thread " + name + " : " + e);
118             A_bank.threadfail = true;
119         } finally {
120             try {
121                 // delete Session
122
mgr.remove();
123             } catch (Exception JavaDoc 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 JavaDoc {
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 JavaDoc {
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 JavaDoc e) {
150             // a possible rollback must not be considered as an error.
151
System.out.println("Thread " + name + " : " + e);
152         } catch (ServerException JavaDoc e) {
153             if (e.detail instanceof TransactionRolledbackException JavaDoc) {
154                 System.out.println("Thread " + name + " : " + e.detail);
155             } else {
156                 throw e;
157             }
158         }
159     }
160
161     private void opMove() throws RemoteException JavaDoc {
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 JavaDoc e) {
177                 // a possible rollback must not be considered as an error.
178
System.out.println("Thread " + name + " : " + e);
179             } catch (ServerException JavaDoc e) {
180                 if (e.detail instanceof TransactionRolledbackException JavaDoc) {
181                     System.out.println("Thread " + name + " : " + e.detail);
182                 } else {
183                     throw e;
184                 }
185             }
186         }
187     }
188
189     private void opCreate() throws RemoteException JavaDoc {
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 JavaDoc, RemoveException JavaDoc {
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