1 7 package org.jboss.samples.callback; 8 9 import org.jboss.remoting.Client; 10 import org.jboss.remoting.HandleCallbackException; 11 import org.jboss.remoting.InvocationRequest; 12 import org.jboss.remoting.InvokerCallbackHandler; 13 import org.jboss.remoting.InvokerLocator; 14 import org.jboss.remoting.Callback; 15 import org.jboss.remoting.transport.Connector; 16 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 23 public class CallbackClient 24 { 25 private static String transport = "socket"; 27 private static String host = "localhost"; 28 private static int port = 5400; 29 30 private Client remotingClient; 31 32 public void createRemotingClient(String locatorURI) throws Exception 33 { 34 InvokerLocator locator = new InvokerLocator(locatorURI); 35 System.out.println("Calling remoting server with locator uri of: " + locatorURI); 36 37 remotingClient = new Client(locator, null); 40 41 } 42 43 public void makeInvocation() throws Throwable 44 { 45 Object response = remotingClient.invoke("Do something", null); 46 System.out.println("Invocation response: " + response); 47 } 48 49 public void testPullCallback() throws Throwable 50 { 51 CallbackHandler callbackHandler = new CallbackHandler(); 52 remotingClient.addListener(callbackHandler); 54 makeInvocation(); 56 57 List callbacks = remotingClient.getCallbacks(); 58 Iterator itr = callbacks.iterator(); 59 while(itr.hasNext()) 60 { 61 Object obj = itr.next(); 62 if(obj instanceof Callback) 63 { 64 System.out.println("Callback value = " + ((Callback)obj).getCallbackObject()); 65 } 66 else 67 { 68 System.out.println("Callback value = " + obj); 69 } 70 } 71 72 remotingClient.removeListener(callbackHandler); 74 } 75 76 public void testPushCallback() throws Throwable 77 { 78 80 String callbackLocatorURI = transport + "://" + host + ":" + (port + 1); 82 InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI); 83 84 setupServer(callbackLocator); 87 88 CallbackHandler callbackHandler = new CallbackHandler(); 89 String callbackHandleObject = "myCallbackHandleObject"; 91 remotingClient.addListener(callbackHandler, callbackLocator, callbackHandleObject); 93 makeInvocation(); 95 96 Thread.sleep(2000); 98 99 remotingClient.removeListener(callbackHandler); 101 } 102 103 public void setupServer(InvokerLocator locator) throws Exception 104 { 105 System.out.println("Starting remoting server with locator uri of: " + locator); 106 Connector connector = new Connector(); 107 connector.setInvokerLocator(locator.getLocatorURI()); 108 connector.start(); 109 110 CallbackServer.SampleInvocationHandler invocationHandler = new CallbackServer.SampleInvocationHandler(); 111 connector.addInvocationHandler("sample", invocationHandler); 113 } 114 115 116 122 public static void main(String [] args) 123 { 124 if(args != null && args.length == 2) 125 { 126 transport = args[0]; 127 port = Integer.parseInt(args[1]); 128 } 129 String locatorURI = transport + "://" + host + ":" + port; 130 CallbackClient client = new CallbackClient(); 131 try 132 { 133 client.createRemotingClient(locatorURI); 134 135 client.testPullCallback(); 137 138 client.testPushCallback(); 140 } 141 catch(Throwable e) 142 { 143 e.printStackTrace(); 144 } 145 } 146 147 public class CallbackHandler implements InvokerCallbackHandler 148 { 149 157 public void handleCallback(InvocationRequest invocation) throws HandleCallbackException 158 { 159 if(invocation instanceof Callback) 160 { 161 Callback callback = (Callback)invocation; 162 System.out.println("Received callback value of: " + callback.getCallbackObject()); 163 System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject()); 164 System.out.println("Received callback server invoker of: " + callback.getServerLocator()); 165 } 166 else 167 { 168 System.out.println("Received callback value of: " + invocation.getParameter()); 169 } 170 } 171 } 172 173 } | Popular Tags |