KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > load > test > Worker


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.load.test;
23
24 import java.util.Hashtable JavaDoc;
25
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
29
30 import javax.ejb.*;
31
32 import org.jboss.test.testbean.interfaces.*;
33
34 /**
35 * Working thread for the Client class. <br>
36 * looks up the number of beans he shall use (creates them if not found)
37 * and reads a value and writes a value for every iteration on every bean.
38 * After completition the beans are removed on fail the beans are not removed.
39 *
40 * @author <a HREF="mailto:daniel.schulze@telkel.com">Daniel Schulze</a>
41 * @version $Id: Worker.java 58115 2006-11-04 08:42:14Z scott.stark@jboss.org $
42 */

43 public class Worker
44 extends Thread JavaDoc
45 {
46    org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass());
47    
48    // succcess or not?
49
Hashtable JavaDoc config;
50
51    Worker (ThreadGroup JavaDoc _group, Hashtable JavaDoc _config)
52    {
53       super (_group, (String JavaDoc)_config.get ("name") + "_" + _config.get("number").toString ());
54       config = _config;
55    }
56
57
58    /** for the client to ask for success */
59    public Hashtable JavaDoc getConfig ()
60    {
61       return config;
62    }
63
64    public void run()
65    {
66       // get the configuration
67
int beans = ((Integer JavaDoc)config.get ("beans")).intValue ();
68       int loops = ((Integer JavaDoc)config.get ("loops")).intValue ();
69       boolean verbose = ((Boolean JavaDoc)config.get ("verbose")).booleanValue ();
70       boolean remove = !((Boolean JavaDoc)config.get ("noremove")).booleanValue ();
71       String JavaDoc logTag = "["+getName ()+"] ";
72
73       int txs = 0;
74       long start = System.currentTimeMillis ();
75
76       EnterpriseEntity[] entity = new EnterpriseEntity[beans];
77       try
78       {
79          // find/create the beans
80
EnterpriseEntityHome home = (EnterpriseEntityHome) new InitialContext JavaDoc ().lookup ("nextgen.EnterpriseEntity");
81          if (home == null)
82          {
83             log.debug (logTag + "EJBHomeInterface lookup returned null?!");
84             log.debug (logTag + "died.");
85             prepareExit (true, txs, start, System.currentTimeMillis ());
86             return;
87          }
88          for (int i = 0; i < beans; ++i)
89          {
90             String JavaDoc key = getName () + "_" + i;
91             try
92             {
93                // first try to find it...
94
entity[i] = home.findByPrimaryKey (key);
95                if (entity[i] == null)
96                {
97                   log.debug (logTag + "EJBHome.findByPrimaryKey () returned null?!");
98                   log.debug (logTag + "died.");
99                   prepareExit (true, txs, start, System.currentTimeMillis ());
100                   return;
101                }
102                ++txs;
103                if (verbose) log.debug (logTag + "reuse bean: "+entity[i].getPrimaryKey ());
104             }
105             catch (FinderException _fe)
106             {
107                // so lets create it...
108
entity[i] = home.create (key);
109                if (entity[i] == null)
110                {
111                   log.debug (logTag + "EJBHome.create () returned null?!");
112                   log.debug (logTag + "died.");
113                   prepareExit (true, txs, start, System.currentTimeMillis ());
114                   return;
115                }
116                ++txs;
117                if (verbose) log.debug (logTag + "create bean: "+entity[i].getPrimaryKey ());
118             }
119          }
120       }
121       catch (Exception JavaDoc _e)
122       {
123          log.debug (logTag+ "problem while finding/creating beans: "+_e.toString ());
124          if (_e instanceof UndeclaredThrowableException JavaDoc)
125             log.debug(logTag+ "target exception: " + ((UndeclaredThrowableException JavaDoc)_e).getUndeclaredThrowable().toString());
126          
127          log.debug (logTag+" died!");
128          prepareExit (true, txs, start, System.currentTimeMillis ());
129          return;
130       }
131
132       // prepare the beans for the test.....
133
for (int b = 0; b < beans; ++b)
134       {
135          try
136          {
137             entity[b].setOtherField (0);
138             ++txs;
139          }
140          catch (Exception JavaDoc _e)
141          {
142             log.debug (logTag+ "cannot prepare bean #"+b+": "+_e.toString ());
143             log.debug (logTag+" died!");
144             prepareExit (true, txs, start, System.currentTimeMillis ());
145             return;
146          }
147       }
148
149       // do the test...
150
for (int l = 0; l < loops; ++l)
151       {
152          for (int b = 0; b < beans; ++b)
153          {
154             int value = -1;
155             try
156             {
157                value = entity[b].getOtherField ();
158                ++txs;
159             }
160             catch (Exception JavaDoc _e)
161             {
162                prepareExit (true, txs, start, System.currentTimeMillis ());
163                log.debug (logTag + "error in getOtherField () on bean #"+b+" in loop "+l+ ": "+_e.toString ());
164                if (value != l)
165                   log.debug (logTag + "unexpected value in bean #"+b+" in loop "+l);
166             }
167
168             try
169             {
170                entity[b].setOtherField (l + 1);
171                ++txs;
172             }
173             catch (Exception JavaDoc _e)
174             {
175                prepareExit (true, txs, start, System.currentTimeMillis ());
176                log.debug (logTag + "error in setOtherField () on bean #"+b+" in loop "+l+ ": "+_e.toString ());
177             }
178          }
179       }
180
181
182       // remove the beans...
183
if (remove)
184       {
185          for (int b = 0; b < beans; ++b)
186          {
187             try
188             {
189                if (verbose) log.debug (logTag + "remove bean #"+b);
190                entity[b].remove ();
191                ++txs;
192             }
193             catch (Exception JavaDoc _e)
194             {
195                log.debug (logTag + "error in removing bean #"+b+": "+_e.toString ());
196                prepareExit (true, txs, start, System.currentTimeMillis ());
197                //_e.printStackTrace (System.out);
198
}
199          }
200       }
201       prepareExit (false, txs, start, System.currentTimeMillis ());
202    }
203    
204    private void prepareExit (boolean _failed, int _transactions, long _start, long _stop)
205    {
206       config.put ("transactions", new Integer JavaDoc (_transactions));
207       config.put ("time", new Long JavaDoc (_stop-_start));
208       config.put ("failed", new Boolean JavaDoc (_failed));
209    }
210    
211 }
212
Popular Tags