1 7 package org.jboss.remoting.samples.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.remoting.InvocationRequest; 14 import org.jboss.remoting.InvokerLocator; 15 import org.jboss.remoting.ServerInvocationHandler; 16 import org.jboss.remoting.ServerInvoker; 17 import org.jboss.remoting.callback.Callback; 18 import org.jboss.remoting.callback.HandleCallbackException; 19 import org.jboss.remoting.callback.InvokerCallbackHandler; 20 import org.jboss.remoting.transport.Connector; 21 22 29 public class CallbackServer 30 { 31 private static String transport = "socket"; 33 private static String host = "localhost"; 34 private static int port = 5400; 35 36 private static int callbackCounter = 1; 38 39 private Connector connector = null; 41 42 private static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation"; 44 45 51 public void setupServer(String locatorURI) throws Exception 52 { 53 InvokerLocator locator = new InvokerLocator(locatorURI); 54 System.out.println("Starting remoting server with locator uri of: " + locatorURI); 55 connector = new Connector(); 56 connector.setInvokerLocator(locator.getLocatorURI()); 57 connector.create(); 58 59 SampleInvocationHandler invocationHandler = new SampleInvocationHandler(); 60 connector.addInvocationHandler("sample", invocationHandler); 62 63 connector.start(); 64 } 65 66 69 public void shutdownServer() 70 { 71 connector.stop(); 72 connector.destroy(); 73 } 74 75 81 public static void main(String [] args) 82 { 83 if(args != null && args.length == 2) 84 { 85 transport = args[0]; 86 port = Integer.parseInt(args[1]); 87 } 88 String locatorURI = transport + "://" + host + ":" + port; 89 CallbackServer server = new CallbackServer(); 90 try 91 { 92 server.setupServer(locatorURI); 93 94 while(true) 96 { 97 Thread.sleep(1000); 98 } 99 100 } 101 catch(Exception e) 102 { 103 e.printStackTrace(); 104 } 105 } 106 107 111 public static class SampleInvocationHandler implements ServerInvocationHandler, Runnable 112 { 113 private List listeners = new ArrayList (); 115 116 private boolean shouldGenerateCallbacks = false; 118 119 public SampleInvocationHandler() 120 { 121 Thread callbackThread = new Thread (this); 123 callbackThread.setDaemon(true); 124 callbackThread.start(); 125 } 126 127 134 public Object invoke(InvocationRequest invocation) throws Throwable 135 { 136 System.out.println("Invocation request is: " + invocation.getParameter()); 138 139 return RESPONSE_VALUE; 141 } 142 143 149 public void addListener(InvokerCallbackHandler callbackHandler) 150 { 151 System.out.println("Adding callback listener."); 152 listeners.add(callbackHandler); 153 shouldGenerateCallbacks = true; 154 } 155 156 162 public void removeListener(InvokerCallbackHandler callbackHandler) 163 { 164 System.out.println("Removing callback listener."); 165 listeners.remove(callbackHandler); 166 if(listeners.size() == 0) 167 { 168 shouldGenerateCallbacks = false; 169 } 170 } 171 172 176 public void run() 177 { 178 while(true) 180 { 181 while(shouldGenerateCallbacks) 182 { 183 Callback callback = new Callback("Callback " + callbackCounter++ + ": This is the payload of callback invocation."); 185 Iterator itr = listeners.iterator(); 186 while(itr.hasNext()) 187 { 188 InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next(); 189 try 190 { 191 callbackHandler.handleCallback(callback); 192 } 193 catch(HandleCallbackException e) 194 { 195 e.printStackTrace(); 196 } 197 } 198 try 200 { 201 Thread.currentThread().sleep(1000); 202 } 203 catch(InterruptedException e) 204 { 205 } 206 207 } 208 try 210 { 211 Thread.currentThread().sleep(1000); 212 } 213 catch(InterruptedException e) 214 { 215 } 216 } 217 } 218 219 224 public void setMBeanServer(MBeanServer server) 225 { 226 } 228 229 234 public void setInvoker(ServerInvoker invoker) 235 { 236 } 238 239 } 240 } | Popular Tags |