KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > stests > manac > A_readthread


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_readthread.java,v 1.1 2003/09/15 13:18:27 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 with read only operations
36  * @author Philippe Durieux
37  */

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

57     private int random(int max) throws RemoteException JavaDoc {
58         double d = Math.random();
59         int ret = (int) (max * d);
60         return ret;
61     }
62
63     public void run() {
64
65         // Create a session bean
66
ManagerHome mgrHome = null;
67         Manager mgr = null;
68         try {
69             mgrHome = (ManagerHome) PortableRemoteObject.narrow(ictx.lookup(managerName), ManagerHome.class);
70             mgr = mgrHome.create(F_read.initialValue);
71         } catch (Exception JavaDoc e) {
72             System.out.println("Cannot Create Session:" + e);
73             return;
74         }
75
76         try {
77
78             // Choose the 4 accounts randomly
79
int d1 = random(accounts);
80             int d2 = random(accounts);
81             int c1 = random(accounts);
82             int c2 = random(accounts);
83             // Set these accounts in session bean
84
mgr.setAccounts(d1, d2, c1, c2);
85
86             // main loop
87
boolean ok = true;
88             for (int i = 0; i < loops; i++) {
89                 int minval = mgr.readBalances();
90                 if (minval < 0) {
91                     System.out.println("Bad read value " + minval);
92                     F_read.threadfail = true;
93                     break;
94                 }
95             }
96             // delete Session
97
mgr.remove();
98         } catch (RemoteException JavaDoc e) {
99             System.out.println("Thread " + name + " : " + e);
100             F_read.threadfail = true;
101         } catch (RemoveException JavaDoc e) {
102             System.out.println("Thread " + name + " Cannot remove session: " + e);
103             F_read.threadfail = true;
104         }
105     }
106 }
107
Popular Tags