KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > distribution > 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.2 2005/02/03 12:16:09 coqp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.distribution;
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 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 JavaDoc {
39     String JavaDoc managerName;
40     String JavaDoc name;
41     int ope;
42     int accmin;
43     int accmax;
44     int amount;
45     int loops;
46     int num;
47     boolean pf;
48     Context JavaDoc ictx;
49     Manager mgr = null;
50     ManagerHome mgrHome = null;
51
52     public A_thread(String JavaDoc mname, int num, Context JavaDoc 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         // Create a session bean
69
try {
70             mgrHome = (ManagerHome) PortableRemoteObject.narrow(ictx.lookup(managerName), ManagerHome.class);
71             mgr = mgrHome.create(A_bank.initialValue, pf);
72         } catch (Exception JavaDoc 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 JavaDoc e) {
116             System.out.println("Thread " + name + " : " + e);
117             A_bank.threadfail = true;
118         } catch (RemoveException JavaDoc 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 JavaDoc 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 JavaDoc {
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 JavaDoc {
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 JavaDoc e) {
152             // a possible rollback must not be considered as an error.
153
System.out.println("Thread " + name + " : " + e);
154         } catch (ServerException JavaDoc e) {
155             if (e.detail instanceof TransactionRolledbackException JavaDoc) {
156                 System.out.println("Thread " + name + " : " + e.detail);
157             } else {
158                 throw e;
159             }
160         }
161     }
162
163     private void opMove() throws RemoteException JavaDoc {
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 JavaDoc e) {
179                 // a possible rollback must not be considered as an error.
180
System.out.println("Thread " + name + " : " + e);
181             } catch (ServerException JavaDoc e) {
182                 if (e.detail instanceof TransactionRolledbackException JavaDoc) {
183                     System.out.println("Thread " + name + " : " + e.detail);
184                 } else {
185                     throw e;
186                 }
187             }
188         }
189     }
190
191     private void opCreate() throws RemoteException JavaDoc {
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 JavaDoc, RemoveException JavaDoc {
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