1 4 package org.jboss.remoting.samples.detection; 5 6 import javax.management.MBeanServer ; 7 import javax.management.MBeanServerFactory ; 8 import javax.management.ObjectName ; 9 import org.jboss.remoting.InvocationRequest; 10 import org.jboss.remoting.InvokerLocator; 11 import org.jboss.remoting.ServerInvocationHandler; 12 import org.jboss.remoting.ServerInvoker; 13 import org.jboss.remoting.callback.InvokerCallbackHandler; 14 import org.jboss.remoting.detection.multicast.MulticastDetector; 15 import org.jboss.remoting.transport.Connector; 16 17 24 public class SimpleDetectorServer 25 { 26 private static String transport = "socket"; 28 private static String host = "localhost"; 29 private static int port = 5400; 30 31 37 public void setupDetector() 38 throws Exception 39 { 40 MBeanServer server = MBeanServerFactory.createMBeanServer(); 42 43 MulticastDetector detector = new MulticastDetector(); 45 server.registerMBean(detector, new ObjectName ("remoting:type=MulticastDetector")); 46 detector.start(); 47 println("MulticastDetector has been created and is listening for new NetworkRegistries to come online"); 48 49 return; 50 } 51 52 59 public void setupServer(String locatorURI) 60 throws Exception 61 { 62 InvokerLocator locator = new InvokerLocator(locatorURI); 63 println("Starting remoting server with locator uri of: " + locatorURI); 64 Connector connector = new Connector(); 65 connector.setInvokerLocator(locator.getLocatorURI()); 66 connector.create(); 67 68 SampleInvocationHandler invocationHandler = new SampleInvocationHandler(); 69 70 connector.addInvocationHandler("sample", invocationHandler); 72 73 println("Added our invocation handler; we are now ready to begin accepting messages from clients"); 74 75 connector.start(); 76 77 return; 78 } 79 80 85 public static void main(String [] args) 86 { 87 String prop = System.getProperty("args"); 89 if(prop != null) 90 { 91 try 92 { 93 transport = prop.substring(0, prop.indexOf("-")); 94 port = Integer.parseInt(prop.substring(prop.indexOf("-") + 1)); 95 } 96 catch(NumberFormatException nfe) 97 { 98 println("INVALID ARGUMENTS: Bad port from property args: " + prop); 99 System.exit(1); 100 } 101 catch(Exception e) 102 { 103 println("INVALID ARGUMENTS: -Dargs property must be in the form '{socket|rmi}-{port#}': " + prop); 104 System.exit(1); 105 } 106 } 107 108 if((args != null) && (args.length != 0)) 110 { 111 if(args.length == 2) 112 { 113 transport = args[0]; 114 port = Integer.parseInt(args[1]); 115 } 116 else 117 { 118 println("INVALID ARGUMENTS: Usage: " + SimpleDetectorServer.class.getName() 119 + " [rmi|socket <port>]"); 120 System.exit(1); 121 } 122 } 123 124 println("Starting JBoss/Remoting server... to stop this server, kill it manually via Control-C"); 125 126 String locatorURI = transport + "://" + host + ":" + port; 127 println("This server's endpoint will be: " + locatorURI); 128 129 SimpleDetectorServer server = new SimpleDetectorServer(); 130 try 131 { 132 server.setupDetector(); 133 server.setupServer(locatorURI); 134 135 while(true) 137 { 138 Thread.sleep(1000); 139 } 140 } 141 catch(Exception e) 142 { 143 e.printStackTrace(); 144 } 145 146 println("Stopping JBoss/Remoting server"); 147 } 148 149 154 public static void println(String msg) 155 { 156 System.out.println((++MSG_COUNT) + ". [SERVER]: " + msg); 157 } 158 159 private static int MSG_COUNT = 0; 161 162 165 public static class SampleInvocationHandler 166 implements ServerInvocationHandler 167 { 168 175 public Object invoke(InvocationRequest invocation) 176 throws Throwable 177 { 178 String msg = invocation.getParameter().toString(); 180 181 println("RECEIVED A CLIENT MESSAGE: " + msg); 182 183 String response = "Server received your message that said [" + msg + "]"; 184 185 if(msg.indexOf("Welcome") > -1) 186 { 187 response = "Received your welcome message. Thank you!"; 188 } 189 190 println("Returning the following message back to the client: " + response); 191 192 return response; 193 } 194 195 200 public void addListener(InvokerCallbackHandler callbackHandler) 201 { 202 } 204 205 210 public void removeListener(InvokerCallbackHandler callbackHandler) 211 { 212 } 214 215 220 public void setMBeanServer(MBeanServer server) 221 { 222 } 224 225 230 public void setInvoker(ServerInvoker invoker) 231 { 232 } 234 } 235 } | Popular Tags |