1 7 package org.jboss.remoting.callback; 8 9 import org.jboss.remoting.AbstractInvokerTest; 10 import org.jboss.remoting.Callback; 11 import org.jboss.remoting.InvocationRequest; 12 import org.jboss.remoting.InvokerCallbackHandler; 13 import org.jboss.remoting.InvokerLocator; 14 import org.jboss.remoting.ServerInvocationHandler; 15 import org.jboss.remoting.ServerInvoker; 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 CallbackTestServer extends AbstractInvokerTest 30 { 31 private static String transport = "socket"; 33 private static String host = "localhost"; 34 private static int port = 5500; 35 36 private String locatorURI = null; 37 38 public static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation"; 40 public static final String CALLBACK_VALUE = "This is the payload of callback invocation."; 41 42 public CallbackTestServer(String name) 43 { 44 super(name); 45 } 46 47 public CallbackTestServer(String name, int numberOfInstances) 48 { 49 super(name, numberOfInstances); 50 } 51 52 public CallbackTestServer(String name, String transport, int port) 53 { 54 super(name, transport, port); 55 } 56 57 public CallbackTestServer(String name, String transport, int port, int numberOfInstances) 58 { 59 super(name, transport, port, numberOfInstances); 60 } 61 62 public CallbackTestServer(String name, String locatorURI) 63 { 64 super(name); 65 this.locatorURI = locatorURI; 66 } 67 68 public void setupServer() throws Exception 69 { 70 InvokerLocator locator = new InvokerLocator(locatorURI); 71 System.out.println("Starting remoting server with locator uri of: " + locatorURI); 72 Connector connector = new Connector(); 73 connector.setInvokerLocator(locator.getLocatorURI()); 74 connector.start(); 75 76 SampleInvocationHandler invocationHandler = new SampleInvocationHandler(); 77 connector.addInvocationHandler("sample", invocationHandler); 79 } 80 81 public void serverTest() throws Exception 82 { 83 try 84 { 85 setupServer(); 86 startup(getNumberOfInstances()); 87 Thread.currentThread().sleep(5000); 88 shutdown(); 89 } 90 catch(Exception e) 91 { 92 throw e; 93 } 94 } 95 96 102 public static void main(String [] args) 103 { 104 if(args != null && args.length == 2) 105 { 106 transport = args[0]; 107 port = Integer.parseInt(args[1]); 108 } 109 String locatorURI = transport + "://" + host + ":" + port; 110 CallbackTestServer server = new CallbackTestServer(CallbackTestServer.class.getName(), locatorURI); 111 try 112 { 113 server.serverTest(); 114 } 115 catch(Exception e) 116 { 117 e.printStackTrace(); 118 System.exit(1); 119 } 120 System.exit(0); 121 } 122 123 126 public static class SampleInvocationHandler implements ServerInvocationHandler 127 { 128 129 private List listeners = new ArrayList (); 130 131 132 139 public Object invoke(InvocationRequest invocation) throws Throwable 140 { 141 143 Callback callback = new Callback(CALLBACK_VALUE); 146 Iterator itr = listeners.iterator(); 147 while(itr.hasNext()) 148 { 149 InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next(); 150 callbackHandler.handleCallback(callback); 151 } 152 153 return RESPONSE_VALUE; 154 155 } 156 157 163 public void addListener(InvokerCallbackHandler callbackHandler) 164 { 165 listeners.add(callbackHandler); 166 } 167 168 174 public void removeListener(InvokerCallbackHandler callbackHandler) 175 { 176 listeners.remove(callbackHandler); 177 } 178 179 184 public void setMBeanServer(MBeanServer server) 185 { 186 } 188 189 194 public void setInvoker(ServerInvoker invoker) 195 { 196 } 198 199 } 200 } | Popular Tags |