1 7 package org.jboss.test.remoting.callback.pull.memory.callbackstore; 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 = 5412; 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 int callbacksReceived = getAllCallbacks(pullCallbackHandler); 85 86 System.out.println("callbacks received = " + callbacksReceived); 87 totalCallbacks = totalCallbacks + callbacksReceived; 88 } 89 90 System.out.println("total callbacks received: " + totalCallbacks); 91 System.out.println("total callbacks expected: " + numberOfCallbacks); 92 93 remotingClient.removeListener(pullCallbackHandler); 94 remotingClient.disconnect(); 95 remotingClient = null; 96 } 97 98 103 private int calculateNumberOfCallbacks() 104 { 105 long max = Runtime.getRuntime().maxMemory(); 106 int targetMem = (int) (max * 0.3); 107 int num = targetMem / 102400; 108 return num; 109 } 110 111 private int getAllCallbacks(CallbackHandler pullCallbackHandler) throws Throwable 112 { 113 int counter = 0; 114 List callbacks = null; 115 116 callbacks = remotingClient.getCallbacks(pullCallbackHandler); 117 while(callbacks.size() > 0) 118 { 119 System.out.println("callbacks.size() = " + callbacks.size()); 120 counter = counter + callbacks.size(); 121 for(int i = 0; i < callbacks.size(); i++) 122 { 123 ((Callback) callbacks.get(i)).getCallbackObject(); 124 } 125 126 Thread.currentThread().sleep(2000); 128 callbacks = remotingClient.getCallbacks(pullCallbackHandler); 129 } 130 return counter; 131 } 132 133 private boolean checkForCallback() throws Throwable 134 { 135 boolean isComplete = false; 136 137 int waitPeriod = 5000; 138 while(true) 139 { 140 isComplete = ((Boolean ) remotingClient.invoke("getdone")).booleanValue(); 142 if(!isComplete) 143 { 144 try 145 { 146 Thread.currentThread().sleep(waitPeriod); 147 } 148 catch(InterruptedException e) 149 { 150 e.printStackTrace(); 151 } 152 } 153 else 154 { 155 break; 156 } 157 } 158 return isComplete; 159 } 160 161 public void setupServer(InvokerLocator locator) throws Exception 162 { 163 System.out.println("Starting remoting server with locator uri of: " + locator); 164 Connector connector = new Connector(); 165 connector.setInvokerLocator(locator.getLocatorURI()); 166 connector.start(); 167 168 CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler(); 169 connector.addInvocationHandler("sample", invocationHandler); 171 } 172 173 174 180 public static void main(String [] args) 181 { 182 if(args != null && args.length == 2) 183 { 184 transport = args[0]; 185 port = Integer.parseInt(args[1]); 186 } 187 String locatorURI = transport + "://" + host + ":" + port; 188 JBossASCallbackTestClient client = new JBossASCallbackTestClient(JBossASCallbackTestClient.class.getName(), locatorURI); 189 try 190 { 191 client.testPullCallback(); 192 } 193 catch(Throwable e) 194 { 195 e.printStackTrace(); 196 System.exit(1); 197 } 198 System.exit(0); 199 } 200 201 public class PushCallbackHandler extends CallbackHandler 202 { 203 204 } 205 206 public class CallbackHandler implements InvokerCallbackHandler 207 { 208 boolean isComplete = false; 209 210 218 public void handleCallback(Callback callback) throws HandleCallbackException 219 { 220 System.out.println("Received callback value of: " + callback.getCallbackObject()); 221 System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject()); 222 System.out.println("Received callback server invoker of: " + callback.getServerLocator()); 223 isComplete = true; 224 } 225 226 public boolean isComplete() 227 { 228 return isComplete; 229 } 230 } 231 232 } | Popular Tags |