1 7 package org.jboss.samples.callback; 8 9 import org.jboss.remoting.InvocationRequest; 10 import org.jboss.remoting.InvokerCallbackHandler; 11 import org.jboss.remoting.InvokerLocator; 12 import org.jboss.remoting.ServerInvocationHandler; 13 import org.jboss.remoting.ServerInvoker; 14 import org.jboss.remoting.Callback; 15 import org.jboss.remoting.HandleCallbackException; 16 import org.jboss.remoting.transport.Connector; 17 18 import javax.management.MBeanServer ; 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 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 final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation"; 38 39 40 public void setupServer(String locatorURI) throws Exception 41 { 42 InvokerLocator locator = new InvokerLocator(locatorURI); 43 System.out.println("Starting remoting server with locator uri of: " + locatorURI); 44 Connector connector = new Connector(); 45 connector.setInvokerLocator(locator.getLocatorURI()); 46 connector.start(); 47 48 SampleInvocationHandler invocationHandler = new SampleInvocationHandler(); 49 connector.addInvocationHandler("sample", invocationHandler); 51 } 52 53 59 public static void main(String [] args) 60 { 61 if(args != null && args.length == 2) 62 { 63 transport = args[0]; 64 port = Integer.parseInt(args[1]); 65 } 66 String locatorURI = transport + "://" + host + ":" + port; 67 CallbackServer server = new CallbackServer(); 68 try 69 { 70 server.setupServer(locatorURI); 71 72 Thread.sleep(10000); 74 75 } 76 catch(Exception e) 77 { 78 e.printStackTrace(); 79 } 80 } 81 82 85 public static class SampleInvocationHandler implements ServerInvocationHandler 86 { 87 88 private List listeners = new ArrayList (); 89 90 91 98 public Object invoke(InvocationRequest invocation) throws Throwable 99 { 100 102 Callback callback = new Callback("This is the payload of callback invocation."); 105 Iterator itr = listeners.iterator(); 106 while(itr.hasNext()) 107 { 108 InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next(); 109 try 110 { 111 callbackHandler.handleCallback(callback); 112 } 113 catch(HandleCallbackException e) 114 { 115 e.printStackTrace(); 116 } 117 } 118 119 return RESPONSE_VALUE; 120 121 } 122 123 129 public void addListener(InvokerCallbackHandler callbackHandler) 130 { 131 listeners.add(callbackHandler); 132 } 133 134 140 public void removeListener(InvokerCallbackHandler callbackHandler) 141 { 142 listeners.remove(callbackHandler); 143 } 144 145 150 public void setMBeanServer(MBeanServer server) 151 { 152 } 154 155 160 public void setInvoker(ServerInvoker invoker) 161 { 162 } 164 165 } 166 } | Popular Tags |