KickJava   Java API By Example, From Geeks To Geeks.

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


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_remthread.java,v 1.3 2003/06/19 07:33: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 create/remove
36  * @author Philippe Durieux
37  */

38 public class A_remthread 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_remthread(String JavaDoc mName, int num, Context JavaDoc ictx, int accounts, int loops) {
46         managerName = mName;
47         name = managerName + "_remac_" + 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),
70                                                                 ManagerHome.class);
71             mgr = mgrHome.create(F_remove.initialValue);
72         } catch (Exception JavaDoc e) {
73             System.out.println("Cannot Create Session:" + e);
74             return;
75         }
76
77         try {
78             // main loop
79
boolean ok = true;
80             for (int i = 0; i < loops; i++) {
81                 // Choose an account randomly
82
int a1 = random(accounts);
83                 // Remove it or Create it randomly
84
int value = random(100);
85                 if (value < 10) {
86                     mgr.delAccount(a1);
87                 } else {
88                     mgr.getAccount(a1);
89                 }
90             }
91
92             // delete Session
93
mgr.remove();
94         } catch (RemoteException JavaDoc e) {
95             System.out.println("Thread " + name + " : " + e);
96             F_remove.threadfail = true;
97         } catch (RemoveException JavaDoc e) {
98             System.out.println("Thread " + name + " Cannot remove bean: " + e);
99             F_remove.threadfail = true;
100         }
101     }
102 }
103
Popular Tags