1 7 package org.jboss.remoting.callback.pull.memory.nullstore; 8 9 import org.jboss.dtf.MultipleTestRunner; 10 import org.jboss.remoting.AbstractInvokerTest; 11 import org.jboss.remoting.Callback; 12 import org.jboss.remoting.Client; 13 import org.jboss.remoting.HandleCallbackException; 14 import org.jboss.remoting.InvocationRequest; 15 import org.jboss.remoting.InvokerCallbackHandler; 16 import org.jboss.remoting.InvokerLocator; 17 import org.jboss.remoting.transport.Connector; 18 import org.jboss.samples.callback.CallbackServer; 19 20 import java.util.List ; 21 22 25 public class CallbackTestClient extends AbstractInvokerTest 26 { 27 private static String transport = "socket"; 29 private static String host = "localhost"; 30 private static int port = 5411; 31 32 private String locatorURI = null; 33 34 private Client remotingClient; 35 private CallbackHandler pullCallbackHandler; 36 38 private boolean isCallbackDone = false; 39 40 private int numberOfCallbacks = 520; 41 42 public CallbackTestClient(String name) 43 { 44 super(name); 45 } 46 47 public CallbackTestClient(String name, String locatorURI) 48 { 49 super(name); 50 this.locatorURI = locatorURI; 51 } 52 53 public CallbackTestClient(String name, int numberOfInstances) 54 { 55 super(name, numberOfInstances); 56 } 57 58 public CallbackTestClient(String name, String transport, int port) 59 { 60 super(name, transport, port); 61 } 62 63 public CallbackTestClient(String name, String transport, int port, int numberOfInstances) 64 { 65 super(name, transport, port, numberOfInstances); 66 } 67 68 69 public void createRemotingClient() throws Exception 70 { 71 InvokerLocator locator = new InvokerLocator(locatorURI); 72 System.out.println("Calling remoting server with locator uri of: " + locatorURI); 73 74 remotingClient = new Client(locator, null); 77 78 } 79 80 public void makeInvocation(String param) throws Throwable 81 { 82 Object response = remotingClient.invoke(param, null); 83 System.out.println("Invocation response: " + response); 84 } 85 86 public void testPullCallback() throws Throwable 87 { 88 createRemotingClient(); 89 numberOfCallbacks = calculateNumberOfCallbacks(); 90 System.out.println("Number of callbacks need to activate persitence: " + numberOfCallbacks); 91 pullCallbackHandler = new CallbackHandler(); 92 remotingClient.addListener(pullCallbackHandler); 94 95 97 makeInvocation("" + numberOfCallbacks); 99 100 makeInvocation("Do something"); 102 103 boolean didItWork = checkForCallback(); 104 105 System.out.println("Did id work = " + didItWork); 106 107 int totalCallbacks = 0; 108 if(didItWork) 109 { 110 boolean gotExpectedException = false; 111 112 try 113 { 114 int callbacksReceived = getAllCallbacks(); 116 117 System.out.println("callbacks received = " + callbacksReceived); 118 totalCallbacks = totalCallbacks + callbacksReceived; 119 120 } 121 catch(RuntimeException re) 122 { 123 System.out.println("Got exception as expected - " + re); 124 gotExpectedException = true; 125 } 126 catch(Throwable thr) 127 { 128 System.out.println("Got unexpected exception - " + thr); 129 } 130 131 if(gotExpectedException) 132 { 133 System.out.println("PASSED"); 134 } 135 else 136 { 137 System.out.println("FAILED"); 138 } 139 assertTrue(gotExpectedException); 140 } 141 142 System.out.println("total callbacks received: " + totalCallbacks); 143 System.out.println("total callbacks expected: " + numberOfCallbacks); 144 145 146 149 150 151 remotingClient.removeListener(pullCallbackHandler); 168 remotingClient.disconnect(); 169 remotingClient = null; 170 } 171 172 177 private int calculateNumberOfCallbacks() 178 { 179 long max = Runtime.getRuntime().maxMemory(); 180 int targetMem = (int) (max * 0.3); 181 int num = targetMem / 102400; 182 return num; 183 } 184 185 private int getAllCallbacks() throws Throwable 186 { 187 int counter = 0; 188 List callbacks = null; 189 190 callbacks = remotingClient.getCallbacks(); 191 while(callbacks.size() > 0) 192 { 193 System.out.println("callbacks.size() = " + callbacks.size()); 194 counter = counter + callbacks.size(); 195 for(int i = 0; i < callbacks.size(); i++) 196 { 197 ((Callback) callbacks.get(i)).getCallbackObject(); 198 } 199 200 Thread.currentThread().sleep(2000); 202 callbacks = remotingClient.getCallbacks(); 203 } 204 return counter; 205 } 206 207 private boolean checkForCallback() throws Throwable 208 { 209 boolean isComplete = false; 210 211 int waitPeriod = 1000; 212 int numOfWaits = 360; for(int x = 0; x < numOfWaits; x++) 214 { 215 isComplete = ((Boolean ) remotingClient.invoke("getdone")).booleanValue(); 217 if(!isComplete) 218 { 219 try 220 { 221 Thread.currentThread().sleep(waitPeriod); 222 } 223 catch(InterruptedException e) 224 { 225 e.printStackTrace(); 226 } 227 } 228 else 229 { 230 break; 231 } 232 } 233 return isComplete; 234 } 235 236 253 public void setupServer(InvokerLocator locator) throws Exception 254 { 255 System.out.println("Starting remoting server with locator uri of: " + locator); 256 Connector connector = new Connector(); 257 connector.setInvokerLocator(locator.getLocatorURI()); 258 connector.start(); 259 260 CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler(); 261 connector.addInvocationHandler("sample", invocationHandler); 263 } 264 265 266 272 public static void main(String [] args) 273 { 274 if(args != null && args.length == 2) 275 { 276 transport = args[0]; 277 port = Integer.parseInt(args[1]); 278 } 279 String locatorURI = transport + "://" + host + ":" + port; 280 CallbackTestClient client = new CallbackTestClient(CallbackTestClient.class.getName(), locatorURI); 281 try 282 { 283 MultipleTestRunner runner = new MultipleTestRunner(); 284 runner.doRun(client, true); 285 } 286 catch(Throwable e) 287 { 288 e.printStackTrace(); 289 System.exit(1); 290 } 291 System.exit(0); 292 } 293 294 public class PushCallbackHandler extends CallbackHandler 295 { 296 297 } 298 299 public class CallbackHandler implements InvokerCallbackHandler 300 { 301 boolean isComplete = false; 302 303 311 public void handleCallback(InvocationRequest invocation) throws HandleCallbackException 312 { 313 if(invocation instanceof Callback) 314 { 315 Callback callback = (Callback) invocation; 316 System.out.println("Received callback value of: " + callback.getCallbackObject()); 317 System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject()); 318 System.out.println("Received callback server invoker of: " + callback.getServerLocator()); 319 } 320 else 321 { 322 System.out.println("Received callback value of: " + invocation.getParameter()); 323 } 324 isComplete = true; 325 } 326 327 public boolean isComplete() 328 { 329 return isComplete; 330 } 331 } 332 333 } | Popular Tags |