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