1
package org.jboss.remoting.samples.detection.jndi;
3
import org.jboss.remoting.InvocationRequest;
4
import org.jboss.remoting.InvokerLocator;
5
import org.jboss.remoting.ServerInvocationHandler;
6
import org.jboss.remoting.ServerInvoker;
7
import org.jboss.remoting.callback.InvokerCallbackHandler;
8
import org.jboss.remoting.detection.jndi.JNDIDetector;
9
import org.jboss.remoting.transport.Connector;
11
import javax.management.MBeanServer;
12
import javax.management.MBeanServerFactory;
13
import javax.management.ObjectName;
14
import java.net.InetAddress;
15
import java.util.HashMap;
19
* A simple JBoss/Remoting remoting server. This uses an inner class SampleInvocationHandler as the invocation
20
* target handler class.
22
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
23
* @author <a href="mailto:mazz@jboss.com">John Mazzitelli</a>
25
public class SimpleDetectorServer
27
// Default locator values - command line args can override transport and port
28
protected static String transport = "socket";
29
protected static String host = "localhost";
30
protected static int port = 5400;
32
private int detectorPort = 1099;
33
private String contextFactory = "org.jnp.interfaces.NamingContextFactory";
34
private String urlPackage = "org.jboss.naming:org.jnp.interfaces";
38
* Sets up NetworkRegistry and JNDItDetector so will can register ourselves on the network. We could also
39
* use these to listen ourselves for any additions or removals of remoting servers on the network.
43
public void setupDetector()
46
// we need an MBeanServer to store our network registry and multicast detector services
47
MBeanServer server = MBeanServerFactory.createMBeanServer();
49
String detectorHost = InetAddress.getLocalHost().getHostName();
51
// multicast detector will detect new network registries that come online
52
JNDIDetector detector = new JNDIDetector(getConfiguration());
53
// set config info for detector and start it.
54
detector.setPort(detectorPort);
55
detector.setHost(detectorHost);
56
detector.setContextFactory(contextFactory);
57
detector.setURLPackage(urlPackage);
59
server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
61
println("JNDIDetector has been created and is listening for new NetworkRegistries to come online");
66
* Sets up our JBoss/Remoting server by creating our Connector on the given locator URI and installing our
67
* invocation handler that will handle incoming messages.
69
* @param locatorURI defines our server endpoing
72
public void setupServer(String locatorURI)
75
InvokerLocator locator = new InvokerLocator(locatorURI);
76
println("Starting remoting server with locator uri of: " + locatorURI);
77
Connector connector = new Connector(getConfiguration());
78
connector.setInvokerLocator(locator.getLocatorURI());
81
SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
83
// first parameter is sub-system name. can be any String value.
84
connector.addInvocationHandler("sample", invocationHandler);
86
println("Added our invocation handler; we are now ready to begin accepting messages from clients");
93
* Can pass transport and port to be used as parameters. Valid transports are 'rmi' and 'socket'.
95
* @param args transport and port number
97
public static void main(String[] args)
99
println("Starting JBoss/Remoting server... to stop this server, kill it manually via Control-C");
100
String locatorURI = getLocatorURI(args);
101
println("This server's endpoint will be: " + locatorURI);
103
SimpleDetectorServer server = new SimpleDetectorServer();
106
server.setupDetector();
107
server.setupServer(locatorURI);
109
// wait forever, let the user kill us at any point (at which point, the client will detect we went down)
120
println("Stopping JBoss/Remoting server");
124
* Outputs a message to stdout.
126
* @param msg the message to output
128
public static void println(String msg)
130
System.out.println(new java.util.Date() + ": [SERVER]: " + msg);
133
protected static String getLocatorURI(String[] args)
135
// get system property -Dargs that is in format "transport:port"
136
String prop = System.getProperty("args");
141
transport = prop.substring(0, prop.indexOf("-"));
142
port = Integer.parseInt(prop.substring(prop.indexOf("-") + 1));
144
catch(NumberFormatException nfe)
146
println("INVALID ARGUMENTS: Bad port from property args: " + prop);
151
println("INVALID ARGUMENTS: -Dargs property must be in the form '{socket|rmi}-{port#}': " + prop);
156
// command line args override defaults and system property
157
if((args != null) && (args.length != 0))
162
port = Integer.parseInt(args[1]);
166
println("INVALID ARGUMENTS: Usage: " + SimpleDetectorServer.class.getName()
167
+ " [rmi|socket <port>]");
172
return transport + "://" + host + ":" + port;
176
* @return configuration map for Connector and JNDIDetector
178
protected Map getConfiguration()
180
return new HashMap();
183
protected SimpleDetectorServer getDetectorServer()
185
return new SimpleDetectorServer();
189
* Simple invocation handler implementation. This is the handler that processes incoming messages from clients.
191
public static class SampleInvocationHandler
192
implements ServerInvocationHandler
195
* This is the method that is called when a new message comes in from a client.
197
* @param invocation the incoming client invocation, encapsulates the message object
198
* @return the response object we send back to the client.
201
public Object invoke(InvocationRequest invocation)
204
// Print out the invocation request
205
String msg = invocation.getParameter().toString();
207
println("RECEIVED A CLIENT MESSAGE: " + msg);
209
String response = "Server received your message that said [" + msg + "]";
211
if(msg.indexOf("Welcome") > -1)
213
response = "Received your welcome message. Thank you!";
216
println("Returning the following message back to the client: " + response);
222
* Adds a callback handler that will listen for callbacks from the server invoker handler.
224
* @param callbackHandler
226
public void addListener(InvokerCallbackHandler callbackHandler)
228
// NO OP as we do not handle callback listeners in this example
232
* Removes the callback handler that was listening for callbacks from the server invoker handler.
234
* @param callbackHandler
236
public void removeListener(InvokerCallbackHandler callbackHandler)
238
// NO OP as we do not handle callback listeners in this example
242
* set the mbean server that the handler can reference
246
public void setMBeanServer(MBeanServer server)
248
// NO OP as we do not need a reference to the MBeanServer for this handler
252
* set the invoker that owns this handler
256
public void setInvoker(ServerInvoker invoker)
258
// NO OP as we do not need a reference back to the server invoker