1 7 package org.jboss.test.remoting.callback.pull.memory.nullstore; 8 9 import java.util.List ; 10 import org.jboss.remoting.Client; 11 import org.jboss.remoting.InvokerLocator; 12 import org.jboss.remoting.callback.Callback; 13 import org.jboss.remoting.callback.HandleCallbackException; 14 import org.jboss.remoting.callback.InvokerCallbackHandler; 15 import org.jboss.remoting.samples.callback.CallbackServer; 16 import org.jboss.remoting.transport.Connector; 17 18 21 public class JBossASCallbackTestClient 22 { 23 private static String transport = "socket"; 25 private static String host = "localhost"; 26 private static int port = 5411; 27 28 private String locatorURI = null; 29 30 private Client remotingClient; 31 private CallbackHandler pullCallbackHandler; 32 33 private boolean isCallbackDone = false; 34 35 private int numberOfCallbacks = 520; 36 37 public JBossASCallbackTestClient(String name, String locatorURI) 38 { 39 this.locatorURI = locatorURI; 40 } 41 42 public void createRemotingClient() throws Exception 43 { 44 InvokerLocator locator = new InvokerLocator(locatorURI); 45 System.out.println("Calling remoting server with locator uri of: " + locatorURI); 46 47 remotingClient = new Client(locator, null); 50 51 } 52 53 public void makeInvocation(String param) throws Throwable 54 { 55 Object response = remotingClient.invoke(param, null); 56 System.out.println("Invocation response: " + response); 57 } 58 59 public void testPullCallback() throws Throwable 60 { 61 createRemotingClient(); 62 numberOfCallbacks = calculateNumberOfCallbacks(); 63 System.out.println("Number of callbacks need to activate persitence: " + numberOfCallbacks); 64 pullCallbackHandler = new CallbackHandler(); 65 remotingClient.addListener(pullCallbackHandler); 67 68 makeInvocation("" + numberOfCallbacks); 70 71 makeInvocation("Do something"); 73 74 boolean didItWork = checkForCallback(); 75 76 System.out.println("Did id work = " + didItWork); 77 78 int totalCallbacks = 0; 79 if(didItWork) 80 { 81 boolean gotExpectedException = false; 82 83 try 84 { 85 int callbacksReceived = getAllCallbacks(pullCallbackHandler); 87 88 System.out.println("callbacks received = " + callbacksReceived); 89 totalCallbacks = totalCallbacks + callbacksReceived; 90 91 } 92 catch(RuntimeException re) 93 { 94 System.out.println("Got exception as expected - " + re); 95 gotExpectedException = true; 96 } 97 catch(Throwable thr) 98 { 99 System.out.println("Got unexpected exception - " + thr); 100 } 101 102 if(gotExpectedException) 103 { 104 System.out.println("PASSED"); 105 } 106 else 107 { 108 System.out.println("FAILED"); 109 } 110 } 111 112 System.out.println("total callbacks received: " + totalCallbacks); 113 System.out.println("total callbacks expected: " + numberOfCallbacks); 114 115 remotingClient.removeListener(pullCallbackHandler); 116 remotingClient.disconnect(); 117 remotingClient = null; 118 } 119 120 125 private int calculateNumberOfCallbacks() 126 { 127 long max = Runtime.getRuntime().maxMemory(); 128 int targetMem = (int) (max * 0.3); 129 int num = targetMem / 102400; 130 return num; 131 } 132 133 private int getAllCallbacks(CallbackHandler pullCallbackHandler) throws Throwable 134 { 135 int counter = 0; 136 List callbacks = null; 137 138 callbacks = remotingClient.getCallbacks(pullCallbackHandler); 139 while(callbacks.size() > 0) 140 { 141 System.out.println("callbacks.size() = " + callbacks.size()); 142 counter = counter + callbacks.size(); 143 for(int i = 0; i < callbacks.size(); i++) 144 { 145 ((Callback) callbacks.get(i)).getCallbackObject(); 146 } 147 148 Thread.currentThread().sleep(2000); 150 callbacks = remotingClient.getCallbacks(pullCallbackHandler); 151 } 152 return counter; 153 } 154 155 private boolean checkForCallback() throws Throwable 156 { 157 boolean isComplete = false; 158 159 int waitPeriod = 1000; 160 int numOfWaits = 360; for(int x = 0; x < numOfWaits; x++) 162 { 163 isComplete = ((Boolean ) remotingClient.invoke("getdone")).booleanValue(); 165 if(!isComplete) 166 { 167 try 168 { 169 Thread.currentThread().sleep(waitPeriod); 170 } 171 catch(InterruptedException e) 172 { 173 e.printStackTrace(); 174 } 175 } 176 else 177 { 178 break; 179 } 180 } 181 return isComplete; 182 } 183 184 public void setupServer(InvokerLocator locator) throws Exception 185 { 186 System.out.println("Starting remoting server with locator uri of: " + locator); 187 Connector connector = new Connector(); 188 connector.setInvokerLocator(locator.getLocatorURI()); 189 connector.start(); 190 191 CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler(); 192 connector.addInvocationHandler("sample", invocationHandler); 194 } 195 196 197 203 public static void main(String [] args) 204 { 205 if(args != null && args.length == 2) 206 { 207 transport = args[0]; 208 port = Integer.parseInt(args[1]); 209 } 210 String locatorURI = transport + "://" + host + ":" + port; 211 JBossASCallbackTestClient client = new JBossASCallbackTestClient(JBossASCallbackTestClient.class.getName(), locatorURI); 212 try 213 { 214 client.testPullCallback(); 215 } 216 catch(Throwable e) 217 { 218 e.printStackTrace(); 219 System.exit(1); 220 } 221 System.exit(0); 222 } 223 224 public class PushCallbackHandler extends CallbackHandler 225 { 226 227 } 228 229 public class CallbackHandler implements InvokerCallbackHandler 230 { 231 boolean isComplete = false; 232 233 241 public void handleCallback(Callback callback) throws HandleCallbackException 242 { 243 System.out.println("Received callback value of: " + callback.getCallbackObject()); 244 System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject()); 245 System.out.println("Received callback server invoker of: " + callback.getServerLocator()); 246 isComplete = true; 247 } 248 249 public boolean isComplete() 250 { 251 return isComplete; 252 } 253 } 254 255 } | Popular Tags |