Contenues dansTrouver plus de documentationRessources d'assistance comprises | Télécharger cet ouvrage au format PDF (1891 Ko)
10.3 Jini Lookup ServiceThe purpose of this example is to demonstrate how a connector client can find and connect to a connector server that has registered with the Jini lookup service. This example performs the following operations:
The Jini lookup service example is contained in the directory examplesDir/current/Lookup/jini. Note – These examples assume that you are already familiar with the Jini network technology. The documentation for the Jini network technology is available at http://wwws.sun.com/software/jini/specs/index.html. You can download the Jini network technology from the Sun Microsystems Community Source Licensing page, at http://wwws.sun.com/software/communitysource/jini/download.html. This example has been implemented using the Jini Technology Starter Kit Version 1.2.1_001. 10.3.1 Registering the Connector Server with the Jini Lookup ServiceThe following code extract is taken from the Server class in the Jini Lookup Service examples directory. Example 10–6 Creating Connector Servers for Registration in the Jini Lookup Servicepublic class Server {
private final MBeanServer mbs;
private static boolean debug = false;
public Server() {
mbs = MBeanServerFactory.createMBeanServer();
}
public JMXConnectorServer rmi(String url)
throws IOException, JMException, ClassNotFoundException {
JMXServiceURL jurl = new JMXServiceURL(url);
final HashMap env = new HashMap();
// Environment map attributes
[...]
JMXConnectorServer rmis =
JMXConnectorServerFactory.newJMXConnectorServer(jurl, env, mbs);
final String agentName = System.getProperty("agent.name",
"DefaultAgent");
start(rmis,env,agentName);
return rmis;
}
[...]
Example 10–6 shows the creation of an MBean server mbs. As was the case for the SLP examples, the JMX service URL and the agent name are passed to Server when it is launched at the command line. We then see the creation of an RMI connector server named rmis, using the system properties defined by the environment map env and the address jurl. The RMI connector server rmis is started. The RMI connector server address will be registered in the Jini lookup service under the name agentName. Subsequent code not shown here creates a corresponding JMXMP connector server named jmxmp, that will also be registered with the Jini lookup service, as shown in the following code extract. Example 10–7 Registering the Connector Server with the Jini Lookup Servicepublic void start(JMXConnectorServer server, Map env, String agentName)
throws IOException, ClassNotFoundException {
server.start();
final ServiceRegistrar registrar=getRegistrar();
final JMXConnector proxy = server.toJMXConnector(env);
register(registrar,proxy,agentName);
}
public static ServiceRegistrar getRegistrar()
throws IOException, ClassNotFoundException,
MalformedURLException {
final String jurl =
System.getProperty("jini.lookup.url","jini://localhost");
final LookupLocator lookup = new LookupLocator(jurl);
final ServiceRegistrar registrar = lookup.getRegistrar();
if (registrar instanceof Administrable)
debug("Registry is administrable.");
return registrar;
}
public static ServiceRegistration register(ServiceRegistrar registrar,
JMXConnector proxy,
String name)
throws IOException {
Entry[] serviceAttrs = new Entry[] {
new net.jini.lookup.entry.Name(name)
};
System.out.println("Registering proxy: AgentName=" + name );
debug("" + proxy);
ServiceItem srvcItem = new ServiceItem(null, proxy, serviceAttrs);
ServiceRegistration srvcRegistration =
registrar.register(srvcItem, Lease.ANY);
debug("Registered ServiceID: " +
srvcRegistration.getServiceID().toString());
return srvcRegistration;
}
Example 10–7 shows the creation of a connector server named server with the environment map env and the service URL jurl. The connector server instance server then gets a pointer to the Jini lookup service by calling the Jini lookup service method LookupLocator.getRegistrar(). The connector server is registered in the Jini lookup service in the form of a proxy, using the Jini lookup service locator registrar and the agent name under which the connector server will be registered. The proxy is in fact a client stub for the connector server, obtained by a call to the toJMXConnector() method of JMXConnectorServer. The registration itself is performed by a call to the register() method of the Jini lookup service class ServiceRegistrar, with an array of service items. 10.3.2 Looking up the Connector Server with the Jini Lookup ServiceThe following code extract is taken from the Client class in the Jini lookup service examples directory. Example 10–8 Looking up the Connector Server with the Jini Lookup Servicepublic class Client {
private static boolean debug = false;
public static ServiceRegistrar getRegistrar()
throws IOException, ClassNotFoundException, MalformedURLException {
final String jurl =
System.getProperty("jini.lookup.url","jini://localhost");
final LookupLocator lookup = new LookupLocator(jurl);
final ServiceRegistrar registrar = lookup.getRegistrar();
if (registrar instanceof Administrable)
debug("Registry is administrable.");
return registrar;
}
public static List lookup(ServiceRegistrar registrar,
String name) throws IOException {
final ArrayList list = new ArrayList();
final Class[] classes = new Class[] {JMXConnector.class};
final Entry[] serviceAttrs = new Entry[] {
new net.jini.lookup.entry.Name(name)
};
ServiceTemplate template =
new ServiceTemplate(null,classes,serviceAttrs);
ServiceMatches matches =
registrar.lookup(template, Integer.MAX_VALUE);
for(int i = 0; i < matches.totalMatches; i++) {
debug("Found Service: " + matches.items[i].serviceID);
if (debug) {
if (matches.items[i].attributeSets != null) {
final Entry[] attrs = matches.items[i].attributeSets;
for (int j = 0; j < attrs.length ; j++) {
debug("Attribute["+j+"]=" + attrs[j]);
}
}
}
if(matches.items[i].service != null) {
JMXConnector c = (JMXConnector)(matches.items[i].service);
debug("Found a JMXConnector: " + c);
list.add(c);
}
}
return list;
}
[...]
Example 10–8 shows how the connector client obtains a pointer to the Jini lookup service with a call to lookup.getRegistrar(). The client then obtains the list of the connectors registered as entries in the Jini lookup service with the agent name name. Unlike in the SLP example, the agent name you pass to Client when it is launched must be either an exact match of an existing agent name, or null, in which case the Jini lookup service will look up all the agents. Once the list of connectors has been obtained, in code that is not shown here, the client connects to the MBean server started by Server, and retrieves the list of all the MBeans registered in it. 10.3.3 Running the Jini Lookup Service ExampleIn addition to the actions you performed in 10.1 Initial Configuration, before you can run the lookup service examples that use the Jini lookup service, you must perform some further initial actions that are specific to this example. You can then start looking up connectors using the Jini network technology, in conjunction with the two connectors supported by Java DMK. When you run the examples, to help you keep track of which agent has been created with which connector and transport, the agent names include a letter suffix. For example, the agent from the example of an RMI connector over JRMP, without an external directory is called test-server-a. To Set up the Jini Lookup Service ExampleThe following steps are required by all of the different connector/transport combinations you can run in this example.
To Run the Jini Lookup Service Example With an RMI ConnectorThis example demonstrates the use of the Jini lookup service to look up RMI connector servers that use RMI's default transport, JRMP, as well as the IIOP transport. In addition, as described in 10.1 Initial Configuration, different external directories are used to register the RMI connector stubs. The combinations of transports and external directories demonstrated here are:
To Run the Jini Lookup Service Example With a JMXMP ConnectorThis example demonstrates the use of the Jini lookup service to look up JMXMP connector servers.
|
||||||||||||||||