KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > samples > detection > SimpleDetectorServer


1 /*
2  * JBoss, Home of Professional Open Source
3  */

4 package org.jboss.remoting.samples.detection;
5
6 import javax.management.MBeanServer JavaDoc;
7 import javax.management.MBeanServerFactory JavaDoc;
8 import javax.management.ObjectName JavaDoc;
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 /**
18  * A simple JBoss/Remoting remoting server. This uses an inner class SampleInvocationHandler as the invocation
19  * target handler class.
20  *
21  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
22  * @author <a HREF="mailto:mazz@jboss.com">John Mazzitelli</a>
23  */

24 public class SimpleDetectorServer
25 {
26    // Default locator values - command line args can override transport and port
27
private static String JavaDoc transport = "socket";
28    private static String JavaDoc host = "localhost";
29    private static int port = 5400;
30
31    /**
32     * Sets up NetworkRegistry and MulticastDetector so will can register ourselves on the network. We could also
33     * use these to listen ourselves for any additions or removals of remoting servers on the network.
34     *
35     * @throws Exception
36     */

37    public void setupDetector()
38          throws Exception JavaDoc
39    {
40       // we need an MBeanServer to store our network registry and multicast detector services
41
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
42
43       // multicast detector will detect new network registries that come online
44
MulticastDetector detector = new MulticastDetector();
45       server.registerMBean(detector, new ObjectName JavaDoc("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    /**
53     * Sets up our JBoss/Remoting server by creating our Connector on the given locator URI and installing our
54     * invocation handler that will handle incoming messages.
55     *
56     * @param locatorURI defines our server endpoing
57     * @throws Exception
58     */

59    public void setupServer(String JavaDoc locatorURI)
60          throws Exception JavaDoc
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       // first parameter is sub-system name. can be any String value.
71
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    /**
81     * Can pass transport and port to be used as parameters. Valid transports are 'rmi' and 'socket'.
82     *
83     * @param args transport and port number
84     */

85    public static void main(String JavaDoc[] args)
86    {
87       // get system property -Dargs that is in format "transport:port"
88
String JavaDoc 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 JavaDoc nfe)
97          {
98             println("INVALID ARGUMENTS: Bad port from property args: " + prop);
99             System.exit(1);
100          }
101          catch(Exception JavaDoc 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       // command line args override defaults and system property
109
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 JavaDoc 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          // wait forever, let the user kill us at any point (at which point, the client will detect we went down)
136
while(true)
137          {
138             Thread.sleep(1000);
139          }
140       }
141       catch(Exception JavaDoc e)
142       {
143          e.printStackTrace();
144       }
145
146       println("Stopping JBoss/Remoting server");
147    }
148
149    /**
150     * Outputs a message to stdout.
151     *
152     * @param msg the message to output
153     */

154    public static void println(String JavaDoc msg)
155    {
156       System.out.println((++MSG_COUNT) + ". [SERVER]: " + msg);
157    }
158
159    // just to show the ordering of the messages that come out
160
private static int MSG_COUNT = 0;
161
162    /**
163     * Simple invocation handler implementation. This is the handler that processes incoming messages from clients.
164     */

165    public static class SampleInvocationHandler
166          implements ServerInvocationHandler
167    {
168       /**
169        * This is the method that is called when a new message comes in from a client.
170        *
171        * @param invocation the incoming client invocation, encapsulates the message object
172        * @return the response object we send back to the client.
173        * @throws Throwable
174        */

175       public Object JavaDoc invoke(InvocationRequest invocation)
176             throws Throwable JavaDoc
177       {
178          // Print out the invocation request
179
String JavaDoc msg = invocation.getParameter().toString();
180
181          println("RECEIVED A CLIENT MESSAGE: " + msg);
182
183          String JavaDoc 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       /**
196        * Adds a callback handler that will listen for callbacks from the server invoker handler.
197        *
198        * @param callbackHandler
199        */

200       public void addListener(InvokerCallbackHandler callbackHandler)
201       {
202          // NO OP as we do not handle callback listeners in this example
203
}
204
205       /**
206        * Removes the callback handler that was listening for callbacks from the server invoker handler.
207        *
208        * @param callbackHandler
209        */

210       public void removeListener(InvokerCallbackHandler callbackHandler)
211       {
212          // NO OP as we do not handle callback listeners in this example
213
}
214
215       /**
216        * set the mbean server that the handler can reference
217        *
218        * @param server
219        */

220       public void setMBeanServer(MBeanServer JavaDoc server)
221       {
222          // NO OP as we do not need a reference to the MBeanServer for this handler
223
}
224
225       /**
226        * set the invoker that owns this handler
227        *
228        * @param invoker
229        */

230       public void setInvoker(ServerInvoker invoker)
231       {
232          // NO OP as we do not need a reference back to the server invoker
233
}
234    }
235 }
Popular Tags