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 JBossASCallbackTestClient 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 JBossASCallbackTestClient(String name, String locatorURI) 43 { 44 this.locatorURI = locatorURI; 45 } 46 47 public void createRemotingClient() throws Exception 48 { 49 InvokerLocator locator = new InvokerLocator(locatorURI); 50 System.out.println("Calling remoting server with locator uri of: " + locatorURI); 51 52 remotingClient = new Client(locator, null); 55 56 } 57 58 public void makeInvocation(String param) throws Throwable 59 { 60 Object response = remotingClient.invoke(param, null); 61 System.out.println("Invocation response: " + response); 62 } 63 64 public void testPullCallback() throws Throwable 65 { 66 createRemotingClient(); 67 numberOfCallbacks = calculateNumberOfCallbacks(); 68 System.out.println("Number of callbacks need to activate persitence: " + numberOfCallbacks); 69 pullCallbackHandler = new CallbackHandler(); 70 remotingClient.addListener(pullCallbackHandler); 72 73 75 makeInvocation("" + numberOfCallbacks); 77 78 makeInvocation("Do something"); 80 81 boolean didItWork = checkForCallback(); 82 83 System.out.println("Did id work = " + didItWork); 84 85 int totalCallbacks = 0; 86 if(didItWork) 87 { 88 boolean gotExpectedException = false; 89 90 try 91 { 92 int callbacksReceived = getAllCallbacks(); 94 95 System.out.println("callbacks received = " + callbacksReceived); 96 totalCallbacks = totalCallbacks + callbacksReceived; 97 98 } 99 catch(RuntimeException re) 100 { 101 System.out.println("Got exception as expected - " + re); 102 gotExpectedException = true; 103 } 104 catch(Throwable thr) 105 { 106 System.out.println("Got unexpected exception - " + thr); 107 } 108 109 if(gotExpectedException) 110 { 111 System.out.println("PASSED"); 112 } 113 else 114 { 115 System.out.println("FAILED"); 116 } 117 } 118 119 System.out.println("total callbacks received: " + totalCallbacks); 120 System.out.println("total callbacks expected: " + numberOfCallbacks); 121 122 123 126 127 128 remotingClient.removeListener(pullCallbackHandler); 145 remotingClient.disconnect(); 146 remotingClient = null; 147 } 148 149 154 private int calculateNumberOfCallbacks() 155 { 156 long max = Runtime.getRuntime().maxMemory(); 157 int targetMem = (int) (max * 0.3); 158 int num = targetMem / 102400; 159 return num; 160 } 161 162 private int getAllCallbacks() throws Throwable 163 { 164 int counter = 0; 165 List callbacks = null; 166 167 callbacks = remotingClient.getCallbacks(); 168 while(callbacks.size() > 0) 169 { 170 System.out.println("callbacks.size() = " + callbacks.size()); 171 counter = counter + callbacks.size(); 172 for(int i = 0; i < callbacks.size(); i++) 173 { 174 ((Callback) callbacks.get(i)).getCallbackObject(); 175 } 176 177 Thread.currentThread().sleep(2000); 179 callbacks = remotingClient.getCallbacks(); 180 } 181 return counter; 182 } 183 184 private boolean checkForCallback() throws Throwable 185 { 186 boolean isComplete = false; 187 188 int waitPeriod = 1000; 189 int numOfWaits = 360; for(int x = 0; x < numOfWaits; x++) 191 { 192 isComplete = ((Boolean ) remotingClient.invoke("getdone")).booleanValue(); 194 if(!isComplete) 195 { 196 try 197 { 198 Thread.currentThread().sleep(waitPeriod); 199 } 200 catch(InterruptedException e) 201 { 202 e.printStackTrace(); 203 } 204 } 205 else 206 { 207 break; 208 } 209 } 210 return isComplete; 211 } 212 213 230 public void setupServer(InvokerLocator locator) throws Exception 231 { 232 System.out.println("Starting remoting server with locator uri of: " + locator); 233 Connector connector = new Connector(); 234 connector.setInvokerLocator(locator.getLocatorURI()); 235 connector.start(); 236 237 CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler(); 238 connector.addInvocationHandler("sample", invocationHandler); 240 } 241 242 243 249 public static void main(String [] args) 250 { 251 if(args != null && args.length == 2) 252 { 253 transport = args[0]; 254 port = Integer.parseInt(args[1]); 255 } 256 String locatorURI = transport + "://" + host + ":" + port; 257 JBossASCallbackTestClient client = new JBossASCallbackTestClient(JBossASCallbackTestClient.class.getName(), locatorURI); 258 try 259 { 260 client.testPullCallback(); 261 } 262 catch(Throwable e) 263 { 264 e.printStackTrace(); 265 System.exit(1); 266 } 267 System.exit(0); 268 } 269 270 public class PushCallbackHandler extends CallbackHandler 271 { 272 273 } 274 275 public class CallbackHandler implements InvokerCallbackHandler 276 { 277 boolean isComplete = false; 278 279 287 public void handleCallback(InvocationRequest invocation) throws HandleCallbackException 288 { 289 if(invocation instanceof Callback) 290 { 291 Callback callback = (Callback) invocation; 292 System.out.println("Received callback value of: " + callback.getCallbackObject()); 293 System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject()); 294 System.out.println("Received callback server invoker of: " + callback.getServerLocator()); 295 } 296 else 297 { 298 System.out.println("Received callback value of: " + invocation.getParameter()); 299 } 300 isComplete = true; 301 } 302 303 public boolean isComplete() 304 { 305 return isComplete; 306 } 307 } 308 309 } | Popular Tags |