Sun GlassFish Message Queue 4.4 Developer's Guide for JMX Clients

Connecting to the MBean Server

As defined in the JMX Specification, client applications obtain MBeans through an MBean server connection, accessed by means of a JMX connector. Message Queue brokers use the standard JMX infrastructure provided with the Java Development Kit (JDK) 1.5, which uses remote method invocation (RMI) for communicating between client and server. Once you obtain a JMX connector, you can use it to obtain an MBean server connection with which to access the attributes, operations, and notifications of individual MBeans. This infrastructure is describe in JMX Connection Infrastructure in Sun GlassFish Message Queue 4.4 Administration Guide.

For convenience, Message Queue provides an admin connection factory (class AdminConnectionFactory), similar in spirit to the familiar Message Queue connection factory, for creating JMX connectors with a minimum of effort. It is also possible to dispense with this convenience class and obtain a JMX connector using standard JMX classes instead. The following sections illustrate these two techniques. While Message Queue client applications are free to use either method, the first is simpler and is recommended.

Obtaining a JMX Connector from an Admin Connection Factory

The Message Queue convenience class AdminConnectionFactory (defined in package com.sun.messaging) encapsulates a predefined set of configuration properties and hides details, such as the JMX Service URL, involved in obtaining a JMX connector. Example 2–1 shows the most straightforward use, obtaining a JMX connector at the default broker Port Mapper port 7676 on host localhost, with the user name and password both set to the default value of admin. After obtaining the connector, its getMBeanServerConnection method is called to obtain an MBean server connection for interacting with Message Queue MBeans.


Example 2–1 Obtaining a JMX Connector from an Admin Connection Factory

import javax.management.remote.*;
import com.sun.messaging.AdminConnectionFactory;


//  Create admin connection factory for default host and port (localhost:7676)
    AdminConnectionFactory  acf = new AdminConnectionFactory();
    
//  Get JMX connector using default user name (admin) and password (admin)
    JMXConnector  jmxc = acf.createConnection();
    
//  Get MBean server connection
    MBeanServerConnection  mbsc = jmxc.getMBeanServerConnection();


Example 2–2 shows how to reconfigure an admin connection factory's properties to nondefault values. Instead of using the default broker address (localhost:7676), the code shown here uses the connection factory's setProperty method to reconfigure it to connect to a broker at port 9898 on host otherhost. (The names of the connection factory's configuration properties are defined as static constants in the Message Queue utility class AdminConnectionConfiguration, defined in package com.sun.messaging.) The arguments to the factory's createConnection method are then used to supply a user name and password other than the defaults.


Example 2–2 Configuring an Admin Connection Factory

import javax.management.remote.*;
import com.sun.messaging.AdminConnectionFactory;


//  Create admin connection factory
    AdminConnectionFactory  acf = new AdminConnectionFactory();
    
//  Configure for specific broker address
    acf.setProperty(AdminConnectionConfiguration.imqAddress, "otherhost:9898");
//  Get JMX connector, supplying user name and password
    JMXConnector  jmxc = acf.createConnection("AliBaba", "sesame");
//  Get MBean server connection
    MBeanServerConnection  mbsc = jmxc.getMBeanServerConnection();


Obtaining a JMX Connector Without Using an Admin Connection Factory

The generic (non–Message Queue) way of obtaining a JMX connector, as described in the JMX Specification, is by invoking the static connect method of the standard JMX class JMXConnectorFactory (see Example 2–3). Client applications may choose to use this method instead of an admin connection factory in order to avoid dependency on Message Queue–specific classes.


Example 2–3 Obtaining a JMX Connector Without Using an Admin Connection Factory

import java.util.HashMap;
import javax.management.remote.*;


//  Provide credentials required by server for user authentication
    HashMap   environment = new HashMap();
    String[]  credentials = new String[] {"AliBaba", "sesame"};
    environment.put (JMXConnector.CREDENTIALS, credentials);
//  Get JMXServiceURL of JMX Connector (must be known in advance)
    JMXServiceURL  url 
        = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server");
//  Get JMX connector
    JMXConnector  jmxc = JMXConnectorFactory.connect(url, environment);
//  Get MBean server connection
    MBeanServerConnection  mbsc = jmxc.getMBeanServerConnection();


The JMXConnectorFactory.connect method accepts two parameters: