1 7 package org.jboss.remoting.samples.callback; 8 9 import java.util.Iterator ; 10 import java.util.List ; 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 import org.jboss.remoting.transport.Connector; 17 18 25 public class CallbackClient 26 { 27 private static String transport = "socket"; 29 private static String host = "localhost"; 30 private static int port = 5400; 31 32 private Client remotingClient; 33 34 private Connector callbackServerConnector; 36 37 43 public void createRemotingClient(String locatorURI) throws Exception 44 { 45 InvokerLocator locator = new InvokerLocator(locatorURI); 46 System.out.println("Calling remoting server with locator uri of: " + locatorURI); 47 48 remotingClient = new Client(locator); 49 } 50 51 56 public void makeInvocation() throws Throwable 57 { 58 Object response = remotingClient.invoke("Do something", null); 59 System.out.println("Invocation response: " + response); 60 } 61 62 68 public void testPullCallback() throws Throwable 69 { 70 CallbackHandler callbackHandler = new CallbackHandler(); 73 remotingClient.addListener(callbackHandler); 75 76 Thread.currentThread().sleep(2000); 79 80 makeInvocation(); 82 83 List callbacks = remotingClient.getCallbacks(callbackHandler); 85 Iterator itr = callbacks.iterator(); 86 while(itr.hasNext()) 87 { 88 Callback callbackObject = (Callback) itr.next(); 89 System.out.println("Pull Callback value = " + callbackObject.getCallbackObject()); 90 } 91 92 remotingClient.removeListener(callbackHandler); 94 } 95 96 102 public void testPushCallback() throws Throwable 103 { 104 String callbackLocatorURI = transport + "://" + host + ":" + (port + 1); 107 InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI); 108 109 setupServer(callbackLocator); 112 113 CallbackHandler callbackHandler = new CallbackHandler(); 114 String callbackHandleObject = "myCallbackHandleObject"; 117 remotingClient.addListener(callbackHandler, callbackLocator, callbackHandleObject); 121 122 Thread.sleep(2000); 124 125 remotingClient.removeListener(callbackHandler); 127 128 callbackServerConnector.stop(); 130 callbackServerConnector.destroy(); 131 } 132 133 140 public void setupServer(InvokerLocator locator) throws Exception 141 { 142 System.out.println("Starting remoting server with locator uri of: " + locator); 143 callbackServerConnector = new Connector(); 144 callbackServerConnector.setInvokerLocator(locator.getLocatorURI()); 145 callbackServerConnector.start(); 146 147 CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler(); 149 callbackServerConnector.addInvocationHandler("sample", invocationHandler); 151 } 152 153 154 160 public static void main(String [] args) 161 { 162 if(args != null && args.length == 2) 163 { 164 transport = args[0]; 165 port = Integer.parseInt(args[1]); 166 } 167 String locatorURI = transport + "://" + host + ":" + port; 168 CallbackClient client = new CallbackClient(); 169 try 170 { 171 client.createRemotingClient(locatorURI); 172 173 client.testPullCallback(); 175 176 client.testPushCallback(); 178 } 179 catch(Throwable e) 180 { 181 e.printStackTrace(); 182 } 183 } 184 185 189 public class CallbackHandler implements InvokerCallbackHandler 190 { 191 198 public void handleCallback(Callback callback) throws HandleCallbackException 199 { 200 System.out.println("Received push callback."); 201 System.out.println("Received callback value of: " + callback.getCallbackObject()); 202 System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject()); 203 System.out.println("Received callback server invoker of: " + callback.getServerLocator()); 204 } 205 } 206 207 } | Popular Tags |