![]() |
|||
![]() |
![]() ![]() |
![]() |
![]() ![]() |
![]() |
![]() ![]() |
![]() |
| |||||
|
$ java -classpath .:$classp -Ddebug=true \ -Dagent.name=test-server-g \ -Durl="service:jmx:jmxmp://" \ -Djava.security.policy=java.policy \ jini.Server & |
In this command:
The name of the agent created is test-server-g
The service URL specifies the chosen connector as the JMXMP connector, running on the first available port.
When the Server is launched, you will see confirmation of the creation of the JMXMP connector, and the registration of its automatically generated URL in the Jini lookup service. JMXMP connector servers can be used alongside RMI connectors, and will be detected by the Client in exactly the same way as RMI connector servers.
Start the Client.
After starting the Server, start the Client:
$ java -classpath .:$classp -Ddebug=true \ -Djava.security.policy=java.policy \ jini.Client |
You will see output confirming the detection of the agents created by the Server and registered in the lookup service. You will also see the identification and confirmation of the connection made to the agents.
To look up a specific agent, type the following command:
$ java -classpath .:$classp -Ddebug=true \ -Djava.security.policy=java.policy \ -Dagent.name="agentName" \ jini.Client |
In the command shown above, agentName is the name of the agent you want to look up. You can also specify a partial agent name by using *; for example, x* for all agent names beginning with the letter x.
You can register RMI connectors or JMXMP connectors with a JNDI lookup service using an LDAP registry as a backend. This example performs the following operations:
The agent:
Creates an MBean server
Creates a connector server
Registers the connector address with the LDAP server
The client:
Gets a pointer to the JNDI/LDAP lookup service
Looks for any connector servers registered in the JNDI/LDAP lookup service
Creates a connector
Retrieves information about the MBeans in the MBean server
The JNDI/LDAP lookup example is contained in the directory examplesDir/current/Lookup/ldap.
Note - Some code provided in this example is specific to Sun's implementation of the Java 2 Platform, Standard Edition (J2SE) SDK 1.4. If you are not using Sun's implementation, you might need to adapt the code to your implementation.
The following code extracts are taken from the Server class in the Jini Lookup Service examples directory.
Example 10-9 Creating Connector Servers for Registration in the JNDI/LDAP Lookup Service
public class Server { public final static int JMX_DEFAULT_LEASE = 60; private static boolean debug = false; private final MBeanServer mbs; public Server() { mbs = MBeanServerFactory.createMBeanServer(); } public static DirContext getRootContext() throws NamingException { final Hashtable env = new Hashtable(); final String factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); final String ldapServerUrl = System.getProperty(Context.PROVIDER_URL); final String ldapUser = System.getProperty(Context.SECURITY_PRINCIPAL, "cn=Directory Manager"); final String ldapPasswd = System.getProperty(Context.SECURITY_CREDENTIALS); debug(Context.PROVIDER_URL + "=" + ldapServerUrl); debug(Context.SECURITY_PRINCIPAL + "=" + ldapUser); if (debug) { System.out.print(Context.SECURITY_CREDENTIALS + "="); final int len = (ldapPasswd==null)?0:ldapPasswd.length(); for (int i=0;i for (int i=0;i<len;i++) System.out.print("*"); System.out.println(); } env.put(Context.INITIAL_CONTEXT_FACTORY,factory); env.put(Context.SECURITY_PRINCIPAL, ldapUser); if (ldapServerUrl != null) env.put(Context.PROVIDER_URL, ldapServerUrl); if (ldapPasswd != null) env.put(Context.SECURITY_CREDENTIALS, ldapPasswd); InitialContext root = new InitialLdapContext(env,null); return (DirContext)(root.lookup("")); } [...] |
Example 10-9 shows the initial creation of an MBean server mbs, and the obtainment of a pointer to the root context of the LDAP directory tree in which the connector server address is to be registered. All the relevant LDAP access variables, such as the provider URL, the LDAP user name and the security credentials, are given here and passed into the environment map env. The environment map env is then passed as a parameter into a call to InitialLdapContext, from which the initial LDAP context is obtained.
Code that is not shown retrieves the agent name under which the connector will be registered in the LDAP server.
Example 10-10 Registering the Connector Server Address in the LDAP Registry
[...] public static void register(DirContext root, JMXServiceURL jmxUrl, String name) throws NamingException, IOException { final String mydn = System.getProperty("dn","cn="+name); debug("dn: " + mydn ); Object o = null; try { o = root.lookup(mydn); } catch (NameNotFoundException n) { Attributes attrs = new BasicAttributes(); Attribute objclass = new BasicAttribute("objectClass"); objclass.add("top"); objclass.add("javaContainer"); objclass.add("jmxConnector"); attrs.put(objclass); attrs.put("jmxAgentName", name); o = root.createSubcontext(mydn,attrs); } if (o == null) throw new NameNotFoundException(); final Attributes attrs = root.getAttributes(mydn); final Attribute oc = attrs.get("objectClass"); if (!oc.contains("jmxConnector")) { final String msg = "The supplied node [" + mydn + "] does not contain the jmxConnector objectclass"; throw new NamingException(msg); } final Attributes newattrs = new BasicAttributes(); newattrs.put("jmxAgentName",name); newattrs.put("jmxServiceURL",jmxUrl.toString()); newattrs.put("jmxAgentHost",InetAddress.getLocalHost().getHostName()); newattrs.put("jmxProtocolType",jmxUrl.getProtocol()); newattrs.put("jmxExpirationDate", getExpirationDate(JMX_DEFAULT_LEASE)); root.modifyAttributes(mydn,DirContext.REPLACE_ATTRIBUTE,newattrs); } [...] |
![]() ![]() |