1 7 package org.jboss.test.remoting.callback; 8 9 import java.util.ArrayList ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import javax.management.MBeanServer ; 13 import org.jboss.logging.Logger; 14 import org.jboss.remoting.Client; 15 import org.jboss.remoting.InvocationRequest; 16 import org.jboss.remoting.InvokerLocator; 17 import org.jboss.remoting.ServerInvocationHandler; 18 import org.jboss.remoting.ServerInvoker; 19 import org.jboss.remoting.callback.Callback; 20 import org.jboss.remoting.callback.HandleCallbackException; 21 import org.jboss.remoting.callback.InvokerCallbackHandler; 22 import org.jboss.remoting.transport.Connector; 23 24 import junit.framework.TestCase; 25 26 29 public class CallbackTestClient extends TestCase 30 { 31 private static String transport = "socket"; 33 private static String host = "localhost"; 34 private static int port = 5500; 35 36 private String locatorURI = transport + "://" + host + ":" + port; 37 38 private Client remotingClient; 39 private Connector connector; 40 41 private static final Logger log = Logger.getLogger(CallbackTestClient.class); 42 43 public static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation"; 44 45 46 private void init() throws Exception 47 { 48 createRemotingClient(); 49 } 50 51 public void createRemotingClient() throws Exception 52 { 53 InvokerLocator locator = new InvokerLocator(locatorURI); 54 System.out.println("Calling remoting server with locator uri of: " + locatorURI); 55 56 remotingClient = new Client(locator, null); 59 60 } 61 62 public void makeInvocation() throws Throwable 63 { 64 Object response = remotingClient.invoke("Do something", null); 65 System.out.println("Invocation response: " + response); 66 } 67 68 public void setUp() throws Exception 69 { 70 init(); 71 } 72 73 public void tearDown() throws Exception 74 { 75 if(remotingClient != null) 76 { 77 remotingClient.disconnect(); 78 } 79 if(connector != null) 80 { 81 connector.stop(); 82 connector.destroy(); 83 } 84 } 85 86 public void testPullCallback() throws Throwable 87 { 88 CallbackHandler callbackHandler = new CallbackHandler(); 89 try 90 { 91 remotingClient.addListener(callbackHandler); 93 makeInvocation(); 95 96 List callbacks = remotingClient.getCallbacks(callbackHandler); 97 Iterator itr = callbacks.iterator(); 98 while(itr.hasNext()) 99 { 100 Object obj = itr.next(); 101 log.info("testPullCallback - Callback object should have been " + 102 CallbackTestServer.CALLBACK_VALUE + " and was " + 103 (obj == null ? null : ((Callback) obj).getCallbackObject())); 104 if(obj instanceof Callback) 105 { 106 assertEquals("Callback object is NOT same.", CallbackTestServer.CALLBACK_VALUE, ((Callback) obj).getCallbackObject()); 107 } 108 else 109 { 110 assertTrue("Callback object is NOT of type Callback.", false); 111 } 112 } 113 } 114 finally 115 { 116 if(remotingClient != null) 117 { 118 remotingClient.removeListener(callbackHandler); 120 } 121 } 122 } 123 124 public void testPushCallback() throws Throwable 125 { 126 127 CallbackHandler callbackHandler = new CallbackHandler(); 128 129 try 130 { 131 String callbackLocatorURI = transport + "://" + host + ":" + (port + 5); 133 InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI); 134 135 log.info("testPushCallback - Setting up server."); 136 setupServer(callbackLocator); 139 140 String callbackHandleObject = "myCallbackHandleObject"; 142 log.info("testPushCallback - adding listener."); 143 remotingClient.addListener(callbackHandler, callbackLocator, callbackHandleObject); 145 log.info("testPushCallback - make invocation"); 146 makeInvocation(); 148 149 Thread.sleep(5000); 151 152 Callback callback = callbackHandler.getCallback(); 153 log.info("testPushCallback - Callback returned was " + callback); 154 assertNotNull("Callback returned was null.", callback); 155 156 Object callbackObj = callback.getCallbackObject(); 157 log.info("testPushCallback - Callback value should have been " + CallbackTestServer.CALLBACK_VALUE + ", and was " + callbackObj); 158 assertEquals("Callback value should have been " + CallbackTestServer.CALLBACK_VALUE + ", but was " + callbackObj, 159 CallbackTestServer.CALLBACK_VALUE, callbackObj); 160 Object callbackHandleObj = callback.getCallbackHandleObject(); 161 log.info("testPushCallback - Callback handle object should have been " + callbackHandleObject + ", and was " + callbackHandleObj); 162 assertEquals("Callback handle object should have been " + callbackHandleObject + ", but was " + callbackHandleObj, 163 callbackHandleObject, callbackHandleObj); 164 InvokerLocator serverLoc = callback.getServerLocator(); 165 log.info("testPushCallback - Callback server locator should have been " + remotingClient.getInvoker().getLocator() + 166 ", and was " + serverLoc); 167 assertEquals("Callback server locator should have been " + remotingClient.getInvoker().getLocator() + 168 ", but was " + serverLoc, 169 remotingClient.getInvoker().getLocator(), serverLoc); 170 171 } 172 finally 173 { 174 if(remotingClient != null) 175 { 176 remotingClient.removeListener(callbackHandler); 178 } 179 } 180 181 182 } 183 184 public void setupServer(InvokerLocator locator) throws Exception 185 { 186 log.info("Starting remoting server with locator uri of: " + locator); 187 try 188 { 189 connector = new Connector(); 190 connector.setInvokerLocator(locator.getLocatorURI()); 191 connector.start(); 192 193 SampleInvocationHandler invocationHandler = new SampleInvocationHandler(); 194 connector.addInvocationHandler("sample", invocationHandler); 196 } 197 catch(Exception e) 198 { 199 log.error("Error starting callback server", e); 200 throw e; 201 } 202 } 203 204 207 public static class SampleInvocationHandler implements ServerInvocationHandler 208 { 209 210 private List listeners = new ArrayList (); 211 212 213 220 public Object invoke(InvocationRequest invocation) throws Throwable 221 { 222 224 Callback callback = new Callback("This is the payload of callback invocation."); 227 Iterator itr = listeners.iterator(); 228 while(itr.hasNext()) 229 { 230 InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next(); 231 try 232 { 233 callbackHandler.handleCallback(callback); 234 } 235 catch(HandleCallbackException e) 236 { 237 e.printStackTrace(); 238 } 239 } 240 241 return RESPONSE_VALUE; 242 243 } 244 245 251 public void addListener(InvokerCallbackHandler callbackHandler) 252 { 253 listeners.add(callbackHandler); 254 } 255 256 262 public void removeListener(InvokerCallbackHandler callbackHandler) 263 { 264 listeners.remove(callbackHandler); 265 } 266 267 272 public void setMBeanServer(MBeanServer server) 273 { 274 } 276 277 282 public void setInvoker(ServerInvoker invoker) 283 { 284 } 286 287 } 288 289 290 public class CallbackHandler implements InvokerCallbackHandler 291 { 292 private Callback callback; 293 294 302 public void handleCallback(Callback callback) throws HandleCallbackException 303 { 304 this.callback = callback; 305 System.out.println("Received callback value of: " + callback.getCallbackObject()); 306 System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject()); 307 System.out.println("Received callback server invoker of: " + callback.getServerLocator()); 308 } 309 310 public Callback getCallback() 311 { 312 return callback; 313 } 314 315 } 316 317 } | Popular Tags |