1 7 package org.jboss.test.remoting.callback.pull.memory.callbackstore; 8 9 import java.util.List ; 10 import org.jboss.logging.Logger; 11 import org.jboss.remoting.Client; 12 import org.jboss.remoting.InvokerLocator; 13 import org.jboss.remoting.callback.Callback; 14 import org.jboss.remoting.callback.HandleCallbackException; 15 import org.jboss.remoting.callback.InvokerCallbackHandler; 16 17 import junit.framework.TestCase; 18 19 22 public class CallbackTestClient extends TestCase 23 { 24 private static String transport = "socket"; 26 private static String host = "localhost"; 27 private static int port = 5412; 28 29 private String locatorURI = transport + "://" + host + ":" + port; 30 31 private Client remotingClient; 32 private CallbackHandler pullCallbackHandler; 33 34 private boolean isCallbackDone = false; 35 36 private int numberOfCallbacks = 520; 37 38 private static final Logger log = Logger.getLogger(CallbackTestClient.class); 39 40 public void createRemotingClient() throws Exception 41 { 42 InvokerLocator locator = new InvokerLocator(locatorURI); 43 System.out.println("Calling remoting server with locator uri of: " + locatorURI); 44 45 remotingClient = new Client(locator, null); 48 49 } 50 51 public void makeInvocation(String param) throws Throwable 52 { 53 Object response = remotingClient.invoke(param, null); 54 System.out.println("Invocation response: " + response); 55 } 56 57 public void setUp() throws Exception 58 { 59 createRemotingClient(); 60 } 61 62 public void tearDown() throws Exception 63 { 64 if(remotingClient != null) 65 { 66 if(pullCallbackHandler != null) 67 { 68 try 69 { 70 remotingClient.removeListener(pullCallbackHandler); 71 } 72 catch(Throwable throwable) 73 { 74 throw new Exception (throwable); 75 } 76 } 77 remotingClient.disconnect(); 78 } 79 } 80 81 public void testPullCallback() throws Throwable 82 { 83 numberOfCallbacks = calculateNumberOfCallbacks(); 84 System.out.println("Number of callbacks need to activate persitence: " + numberOfCallbacks); 85 pullCallbackHandler = new CallbackHandler(); 86 remotingClient.addListener(pullCallbackHandler); 88 89 makeInvocation("" + numberOfCallbacks); 91 92 makeInvocation("Do something"); 94 95 boolean didItWork = checkForCallback(); 96 97 System.out.println("Did it work = " + didItWork); 98 log.debug("Did it work = " + didItWork); 99 100 int totalCallbacks = 0; 101 if(didItWork) 102 { 103 int callbacksReceived = getAllCallbacks(pullCallbackHandler); 105 106 System.out.println("callbacks received = " + callbacksReceived); 107 log.debug("callbacks received = " + callbacksReceived); 108 totalCallbacks = totalCallbacks + callbacksReceived; 109 } 110 111 System.out.println("total callbacks received: " + totalCallbacks); 112 log.debug("total callbacks received: " + totalCallbacks); 113 System.out.println("total callbacks expected: " + numberOfCallbacks); 114 log.debug("total callbacks expected: " + numberOfCallbacks); 115 116 assertEquals(numberOfCallbacks, totalCallbacks); 117 } 118 119 124 private int calculateNumberOfCallbacks() 125 { 126 long max = Runtime.getRuntime().maxMemory(); 127 int targetMem = (int) (max * 0.3); 128 int num = targetMem / 102400; 129 return num; 130 } 131 132 private int getAllCallbacks(CallbackHandler pullCallbackHandler) throws Throwable 133 { 134 int counter = 0; 135 List callbacks = null; 136 137 callbacks = remotingClient.getCallbacks(pullCallbackHandler); 138 while(callbacks.size() > 0) 139 { 140 System.out.println("callbacks.size() = " + callbacks.size()); 141 counter = counter + callbacks.size(); 142 for(int i = 0; i < callbacks.size(); i++) 143 { 144 ((Callback) callbacks.get(i)).getCallbackObject(); 145 } 146 147 Thread.currentThread().sleep(2000); 149 callbacks = remotingClient.getCallbacks(pullCallbackHandler); 150 } 151 return counter; 152 } 153 154 private boolean checkForCallback() throws Throwable 155 { 156 boolean isComplete = false; 157 158 int waitPeriod = 1000; 159 int numOfWaits = 600000; 160 for(int x = 0; x < numOfWaits; x++) 161 { 162 isComplete = ((Boolean ) remotingClient.invoke("getdone")).booleanValue(); 164 if(!isComplete) 165 { 166 try 167 { 168 Thread.currentThread().sleep(waitPeriod); 169 } 170 catch(InterruptedException e) 171 { 172 e.printStackTrace(); 173 } 174 } 175 else 176 { 177 break; 178 } 179 } 180 return isComplete; 181 } 182 183 public static void main(String [] args) 184 { 185 CallbackTestClient client = new CallbackTestClient(); 186 try 187 { 188 client.setUp(); 189 client.testPullCallback(); 190 client.tearDown(); 191 } 192 catch(Throwable throwable) 193 { 194 throwable.printStackTrace(); 195 } 196 } 197 198 199 public class PushCallbackHandler extends CallbackHandler 200 { 201 202 } 203 204 public class CallbackHandler implements InvokerCallbackHandler 205 { 206 boolean isComplete = false; 207 208 216 public void handleCallback(Callback callback) throws HandleCallbackException 217 { 218 System.out.println("Received callback value of: " + callback.getCallbackObject()); 219 System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject()); 220 System.out.println("Received callback server invoker of: " + callback.getServerLocator()); 221 isComplete = true; 222 } 223 224 public boolean isComplete() 225 { 226 return isComplete; 227 } 228 } 229 230 } | Popular Tags |