1 7 package org.jboss.remoting.callback.pull.memory.callbackstore; 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 = 5412; 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 int callbacksReceived = getAllCallbacks(); 92 93 System.out.println("callbacks received = " + callbacksReceived); 94 totalCallbacks = totalCallbacks + callbacksReceived; 95 } 96 97 System.out.println("total callbacks received: " + totalCallbacks); 98 System.out.println("total callbacks expected: " + numberOfCallbacks); 99 100 101 104 105 106 remotingClient.removeListener(pullCallbackHandler); 123 remotingClient.disconnect(); 124 remotingClient = null; 125 } 126 127 132 private int calculateNumberOfCallbacks() 133 { 134 long max = Runtime.getRuntime().maxMemory(); 135 int targetMem = (int) (max * 0.3); 136 int num = targetMem / 102400; 137 return num; 138 } 139 140 private int getAllCallbacks() throws Throwable 141 { 142 int counter = 0; 143 List callbacks = null; 144 145 callbacks = remotingClient.getCallbacks(); 146 while(callbacks.size() > 0) 147 { 148 System.out.println("callbacks.size() = " + callbacks.size()); 149 counter = counter + callbacks.size(); 150 for(int i = 0; i < callbacks.size(); i++) 151 { 152 ((Callback) callbacks.get(i)).getCallbackObject(); 153 } 154 155 Thread.currentThread().sleep(2000); 157 callbacks = remotingClient.getCallbacks(); 158 } 159 return counter; 160 } 161 162 private boolean checkForCallback() throws Throwable 163 { 164 boolean isComplete = false; 165 166 int waitPeriod = 5000; 167 while(true) 168 { 169 isComplete = ((Boolean ) remotingClient.invoke("getdone")).booleanValue(); 171 if(!isComplete) 172 { 173 try 174 { 175 Thread.currentThread().sleep(waitPeriod); 176 } 177 catch(InterruptedException e) 178 { 179 e.printStackTrace(); 180 } 181 } 182 else 183 { 184 break; 185 } 186 } 187 return isComplete; 188 } 189 190 207 public void setupServer(InvokerLocator locator) throws Exception 208 { 209 System.out.println("Starting remoting server with locator uri of: " + locator); 210 Connector connector = new Connector(); 211 connector.setInvokerLocator(locator.getLocatorURI()); 212 connector.start(); 213 214 CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler(); 215 connector.addInvocationHandler("sample", invocationHandler); 217 } 218 219 220 226 public static void main(String [] args) 227 { 228 if(args != null && args.length == 2) 229 { 230 transport = args[0]; 231 port = Integer.parseInt(args[1]); 232 } 233 String locatorURI = transport + "://" + host + ":" + port; 234 JBossASCallbackTestClient client = new JBossASCallbackTestClient(JBossASCallbackTestClient.class.getName(), locatorURI); 235 try 236 { 237 client.testPullCallback(); 238 } 239 catch(Throwable e) 240 { 241 e.printStackTrace(); 242 System.exit(1); 243 } 244 System.exit(0); 245 } 246 247 public class PushCallbackHandler extends CallbackHandler 248 { 249 250 } 251 252 public class CallbackHandler implements InvokerCallbackHandler 253 { 254 boolean isComplete = false; 255 256 264 public void handleCallback(InvocationRequest invocation) throws HandleCallbackException 265 { 266 if(invocation instanceof Callback) 267 { 268 Callback callback = (Callback) invocation; 269 System.out.println("Received callback value of: " + callback.getCallbackObject()); 270 System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject()); 271 System.out.println("Received callback server invoker of: " + callback.getServerLocator()); 272 } 273 else 274 { 275 System.out.println("Received callback value of: " + invocation.getParameter()); 276 } 277 isComplete = true; 278 } 279 280 public boolean isComplete() 281 { 282 return isComplete; 283 } 284 } 285 286 } | Popular Tags |