KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > performance > TestServerInvocationHandler


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.performance;
8
9 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
10 import org.jboss.logging.Logger;
11 import org.jboss.remoting.InvocationRequest;
12 import org.jboss.remoting.InvokerCallbackHandler;
13 import org.jboss.remoting.ServerInvocationHandler;
14 import org.jboss.remoting.ServerInvoker;
15 import org.jboss.remoting.ShutdownListener;
16 import org.jboss.remoting.invocation.RemoteInvocation;
17
18 import javax.management.MBeanServer JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Timer JavaDoc;
24 import java.util.TimerTask JavaDoc;
25
26 /**
27  * MockServerInvocationHandler
28  *
29  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
30  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
31 <<<<<<< TestServerInvocationHandler.java
32  * @version $Revision: 1.3.2.2 $
33 =======
34  * @version $Revision: 1.3.2.2 $
35 >>>>>>> 1.6
36  */

37 public class TestServerInvocationHandler implements ServerInvocationHandler
38 {
39    private ServerInvoker invoker;
40    private List JavaDoc listeners = new ArrayList JavaDoc();
41    private Map JavaDoc clientListeners = new HashMap JavaDoc();
42
43    int totalCount = 0;
44    SynchronizedInt duplicateCount = new SynchronizedInt(0);
45    SynchronizedInt receivedCount = new SynchronizedInt(0);
46
47    private boolean[] counterArray = null;
48
49    private final Object JavaDoc waitObj = new Object JavaDoc();
50    private Timer JavaDoc processingCompleteTimer;
51    private boolean processingWork = false;
52    private ShutdownListener shutdownListener;
53    private static final Logger log = Logger.getLogger(TestServerInvocationHandler.class);
54
55    private Object JavaDoc lastParam = null;
56
57    public TestServerInvocationHandler(ShutdownListener listener)
58    {
59       this.shutdownListener = listener;
60    }
61
62
63    /**
64     * set the invoker that owns this handler
65     *
66     * @param invoker
67     */

68    public void setInvoker(ServerInvoker invoker)
69    {
70       this.invoker = invoker;
71    }
72
73    /**
74     * set the mbean server that the handler can reference
75     *
76     * @param server
77     */

78    public void setMBeanServer(MBeanServer JavaDoc server)
79    {
80    }
81
82    public Object JavaDoc invoke(InvocationRequest invocation)
83          throws Throwable JavaDoc
84    {
85       Object JavaDoc param = invocation.getParameter();
86       String JavaDoc methodName = "";
87       Object JavaDoc[] params = null;
88       String JavaDoc[] sig = null;
89
90       if(param instanceof RemoteInvocation)
91       {
92          RemoteInvocation rminvo = (RemoteInvocation) param;
93          methodName = rminvo.getMethodName();
94          params = rminvo.getParameters();
95       }
96       else
97       {
98          throw new Exception JavaDoc("Unknown invocation payload (" + param + "). " +
99                              "Should be instance of RemoteInvocation.");
100       }
101
102       String JavaDoc sessionId = invocation.getSessionId();
103       String JavaDoc subsystem = invocation.getSubsystem();
104
105 // log.debug("invoke() called with method: " + methodName +
106
// "\tsessionId: " + sessionId + "\tsubsystem:" + subsystem);
107

108       Object JavaDoc ret = null;
109       if(methodName.equals("totalCallCount"))
110       {
111          String JavaDoc totalCountStr = (String JavaDoc) params[0];
112          int totalCount = Integer.parseInt(totalCountStr);
113          System.out.println("received totalCallCount call with total count of " + totalCount);
114          createTotalCount(totalCount);
115       }
116       else if(methodName.equals("serverTotalCallCount"))
117       {
118          // Need to block thread till done getting all server calls processed so can return total
119
int svrTotalCount = getServerTotalCount();
120          System.out.println("Returning server total count: " + svrTotalCount);
121          ret = String.valueOf(svrTotalCount);
122       }
123       else if(methodName.equals("saveInvocationParameter"))
124       {
125          if(params != null)
126          {
127             lastParam = params[0];
128             try
129             {
130                if(lastParam instanceof String JavaDoc)
131                {
132                   int localClientInvokeCount = Integer.parseInt((String JavaDoc) lastParam);
133                   verifyClientInvokeCount(localClientInvokeCount);
134                }
135             }
136             catch(NumberFormatException JavaDoc e)
137             {
138             }
139             finally
140             {
141                ret = lastParam;
142             }
143
144          }
145       }
146       else if(methodName.equals("getLastInvocationParameter"))
147       {
148          ret = lastParam;
149       }
150       else
151       {
152          if(params != null)
153          {
154             String JavaDoc paramCount = (String JavaDoc) params[0];
155             try
156             {
157                int localClientInvokeCount = Integer.parseInt(paramCount);
158                verifyClientInvokeCount(localClientInvokeCount);
159                // just passing return, even though not needed to make sure gets thrown away before getting to client
160
ret = paramCount;
161             }
162             catch(NumberFormatException JavaDoc e)
163             {
164                ret = paramCount;
165             }
166          }
167          else
168          {
169             log.error("no parameter passed");
170          }
171       }
172       return ret;
173    }
174
175    private int getServerTotalCount()
176    {
177       synchronized(waitObj)
178       {
179          try
180          {
181             waitObj.wait(5 * 60 * 1000); // timeout if not notified
182
}
183          catch(InterruptedException JavaDoc e)
184          {
185
186          }
187       }
188       processingCompleteTimer.cancel();
189       return receivedCount.get();
190    }
191
192    private void serverProcessingComplete()
193    {
194       System.out.println("server processing complete.");
195
196       synchronized(waitObj)
197       {
198          waitObj.notify();
199       }
200       if(shutdownListener != null)
201       {
202          try
203          {
204             shutdownListener.shutdownTest();
205          }
206          catch(Exception JavaDoc e)
207          {
208             e.printStackTrace();
209          }
210       }
211    }
212
213    private synchronized void createTotalCount(int totalCount)
214    {
215       counterArray = new boolean[totalCount + 1];
216       this.totalCount = totalCount;
217       receivedCount.set(0);
218       duplicateCount.set(0);
219       startTimer();
220    }
221
222    private void startTimer()
223    {
224       processingCompleteTimer = new Timer JavaDoc();
225       processingCompleteTimer.schedule(new CompetedTimerTask(), 1000, 1000);
226    }
227
228    private void verifyClientInvokeCount(int localClientInvokeCount)
229    {
230       processingWork = true;
231       boolean duplicate = addToReceivedCount(localClientInvokeCount);
232       if(duplicate)
233       {
234          duplicateCount.increment();
235       }
236       else
237       {
238          receivedCount.increment();
239       }
240
241       int currentDuplicateCount = duplicateCount.get();
242       int currentReceivedCount = receivedCount.get();
243       if((currentReceivedCount % 100) == 0)
244       {
245          System.out.println("Received count: " + currentReceivedCount);
246          System.out.println("Duplicate count: " + currentDuplicateCount);
247          System.out.println("Total count: " + totalCount);
248       }
249       if((currentReceivedCount + currentDuplicateCount) == totalCount)
250       {
251          System.out.println("\n\n*****************************\n" +
252                             " Test Finished\n" +
253                             "*****************************\n" +
254                             " Received Count = " + currentReceivedCount + "\n" +
255                             " Duplicate Count = " + currentDuplicateCount + "\n" +
256                             "*****************************\n\n");
257       }
258    }
259
260    private synchronized boolean addToReceivedCount(int localClientInvokeCount)
261    {
262       boolean isDuplicate = false;
263       if(counterArray == null)
264       {
265          System.out.println("Error! Have not received invoke for method 'totalCallCount', so can not process count.");
266       }
267       else
268       {
269          try
270          {
271             isDuplicate = counterArray[localClientInvokeCount];
272             if(!isDuplicate)
273             {
274                counterArray[localClientInvokeCount] = true;
275             }
276          }
277          catch(ArrayIndexOutOfBoundsException JavaDoc e)
278          {
279             System.err.println("Got ArrayIndexOutOfBoundsException");
280             System.err.println("Counter array size = " + counterArray.length);
281             System.err.println("Received count = " + localClientInvokeCount);
282             e.printStackTrace();
283          }
284       }
285       return isDuplicate;
286    }
287
288    public void addListener(InvokerCallbackHandler callbackHandler)
289    {
290       listeners.add(callbackHandler);
291       log.debug("added listener " + callbackHandler);
292    }
293
294    public void removeListener(InvokerCallbackHandler callbackHandler)
295    {
296       listeners.remove(callbackHandler);
297       log.debug("removed listener " + callbackHandler);
298    }
299
300    public class CompetedTimerTask extends TimerTask JavaDoc
301    {
302       private int numOfPasses = 0;
303
304       /**
305        * The action to be performed by this timer task.
306        */

307       public void run()
308       {
309          if(processingWork)
310          {
311             processingWork = false;
312             numOfPasses = 0;
313          }
314          else
315          {
316             if(numOfPasses > 5)
317             {
318                serverProcessingComplete();
319             }
320             else
321             {
322                numOfPasses++;
323             }
324          }
325       }
326
327    }
328
329 }
330
Popular Tags