KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > stests > manac > 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.7 2003/12/09 12:37:36 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.stests.manac;
27
28 import java.rmi.RemoteException JavaDoc;
29 import javax.ejb.RemoveException JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.rmi.PortableRemoteObject JavaDoc;
32
33
34 /**
35  * Stress Test
36  * @author Philippe Durieux
37  */

38 public class A_thread extends Thread JavaDoc {
39     String JavaDoc managerName;
40     String JavaDoc name;
41     int accounts;
42     int delay;
43     int loops;
44     int amount;
45     Context JavaDoc ictx;
46     int checklevel;
47
48     public A_thread(String JavaDoc mName, int num, Context JavaDoc ictx, int accounts, int delay, int loops, int amount, int checklevel) {
49         managerName = mName;
50         name = managerName + "_manac_" + num;
51         setName(name);
52         this.ictx = ictx;
53         this.accounts = accounts;
54         this.delay = delay;
55         this.loops = loops;
56         this.amount = amount;
57         this.checklevel = checklevel;
58     }
59
60     /**
61      * random returns an integer between 0 and max - 1
62      */

63     private int random(int max) throws RemoteException JavaDoc {
64         double d = Math.random();
65         int ret = (int) (max * d);
66         return ret;
67     }
68
69     public void run() {
70
71         // Create a session bean
72
ManagerHome mgrHome = null;
73         Manager mgr = null;
74         try {
75             mgrHome = (ManagerHome) PortableRemoteObject.narrow(ictx.lookup(managerName),
76                                                                 ManagerHome.class);
77             mgr = mgrHome.create(A_manac.initialValue);
78         } catch (Exception JavaDoc e) {
79             System.out.println("Cannot Create Session:" + e);
80             return;
81         }
82
83         try {
84             // Set delay and value to move.
85
if (delay > 0) {
86                 mgr.setDelay(delay);
87             }
88             if (amount > 0) {
89                 mgr.setValue(amount);
90             }
91
92             // main loop
93
boolean ok = true;
94             for (int i = 0; i < loops; i++) {
95                 // Choose the 4 accounts randomly
96
int d1 = random(accounts);
97                 int d2 = random(accounts);
98                 int c1 = random(accounts);
99                 int c2 = random(accounts);
100                 if (amount < 0) {
101                     // make a simple withdraw.
102
try {
103                         mgr.withdraw(d1, c1, -amount);
104                     } catch (RemoteException JavaDoc r) {
105                         System.out.println("Exception (ignored) raised on withdraw:" + r);
106                     }
107                 } else {
108                     // Set these accounts in session bean
109
try {
110                         mgr.setAccounts(d1, d2, c1, c2);
111                     } catch (RemoteException JavaDoc r) {
112                         System.out.println("Bad Account Setting:" + r);
113                         A_manac.threadfail = true;
114                         break;
115                     }
116                     // make the transfert
117
try {
118                         mgr.movement();
119                     } catch (RemoteException JavaDoc r) {
120                         // movement raised exception. ignoring...
121
System.out.println("Exception (ignored) raised on movement:" + r);
122                     }
123                 }
124                 if (checklevel >= 2) {
125                     // Check accounts that were debited
126
if (! mgr.checkAccount(d1)) {
127                         System.out.println("Bad Account after move on account " + d1);
128                         ok = false;
129                     }
130                     if (! mgr.checkAccount(d2)) {
131                         System.out.println("Bad Account after move on account " + d2);
132                         ok = false;
133                     }
134                     if (!ok) {
135                         System.out.println("Stopping this session because some accounts are not ok");
136                         A_manac.threadfail = true;
137                         break;
138                     }
139                 }
140             }
141             // delete Session
142
mgr.remove();
143         } catch (RemoteException JavaDoc e) {
144             System.out.println("Thread " + name + " : " + e);
145             A_manac.threadfail = true;
146         } catch (RemoveException JavaDoc e) {
147             System.out.println("Thread " + name + " Cannot remove session: " + e);
148             A_manac.threadfail = true;
149         }
150     }
151 }
152
Popular Tags