1 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 ; 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Timer ; 24 import java.util.TimerTask ; 25 26 37 public class TestServerInvocationHandler implements ServerInvocationHandler 38 { 39 private ServerInvoker invoker; 40 private List listeners = new ArrayList (); 41 private Map clientListeners = new HashMap (); 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 waitObj = new Object (); 50 private Timer processingCompleteTimer; 51 private boolean processingWork = false; 52 private ShutdownListener shutdownListener; 53 private static final Logger log = Logger.getLogger(TestServerInvocationHandler.class); 54 55 private Object lastParam = null; 56 57 public TestServerInvocationHandler(ShutdownListener listener) 58 { 59 this.shutdownListener = listener; 60 } 61 62 63 68 public void setInvoker(ServerInvoker invoker) 69 { 70 this.invoker = invoker; 71 } 72 73 78 public void setMBeanServer(MBeanServer server) 79 { 80 } 81 82 public Object invoke(InvocationRequest invocation) 83 throws Throwable 84 { 85 Object param = invocation.getParameter(); 86 String methodName = ""; 87 Object [] params = null; 88 String [] 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 ("Unknown invocation payload (" + param + "). " + 99 "Should be instance of RemoteInvocation."); 100 } 101 102 String sessionId = invocation.getSessionId(); 103 String subsystem = invocation.getSubsystem(); 104 105 108 Object ret = null; 109 if(methodName.equals("totalCallCount")) 110 { 111 String totalCountStr = (String ) 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 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 ) 131 { 132 int localClientInvokeCount = Integer.parseInt((String ) lastParam); 133 verifyClientInvokeCount(localClientInvokeCount); 134 } 135 } 136 catch(NumberFormatException 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 paramCount = (String ) params[0]; 155 try 156 { 157 int localClientInvokeCount = Integer.parseInt(paramCount); 158 verifyClientInvokeCount(localClientInvokeCount); 159 ret = paramCount; 161 } 162 catch(NumberFormatException 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); } 183 catch(InterruptedException 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 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 (); 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 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 301 { 302 private int numOfPasses = 0; 303 304 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 |