Connecting to a Configuration Manager using the Configuration Manager Proxy

Before you start

Before starting this step, you must have completed Configuring an environment for developing and running Configuration Manager Proxy applications.

Consider the following program ConnectToConfigManager.java; it attempts to connect to a Configuration Manager running on the default queue manager of the local machine.
import com.ibm.broker.config.proxy.*;

public class ConfigManagerRunStateChecker {

    public static void main(String[] args) {
        displayConfigManagerRunState("localhost", 1414, "");
    }

    public static void displayConfigManagerRunState(String hostname,
                                                    int port,
                                                    String qmgr) {
        ConfigManagerProxy cmp = null;
        try {
            ConfigManagerConnectionParameters cmcp =
                new MQConfigManagerConnectionParameters(hostname, port, qmgr);
            cmp = ConfigManagerProxy.getInstance(cmcp);
            String configManagerName = cmp.getName();
            
            System.out.println("Configuration Manager ‘"+configManagerName+
                "’ is available!");
            cmp.disconnect();
        } catch (ConfigManagerProxyException ex) {
            System.out.println("Configuration Manager is NOT available"+
                " because "+ex);
        }
    }
}

The first line of the program requests Java to import the CMP classes. All CMP classes are in the com.ibm.broker.config.proxy package.

The first line inside the try block of the displayConfigManagerRunState() method instantiates a ConfigManagerConnectionParameters object. This is an interface which states that implementing classes are able to provide the parameters to connect to a Configuration Manager.

The only class that implements this interface is MQConfigManagerConnectionParameters, which defines a set of WebSphere MQ-based connection parameters. The constructor used here takes three parameters:
  1. The host name of the Configuration Manager machine
  2. The port on which the Configuration Manager’s WebSphere MQ listener service is listening
  3. the name of the Configuration Manager’s WebSphere MQ queue manager
Once you have defined this object, an attempt can be made to connect to the Configuration Manager’s queue manager with those characteristics. This is achieved by the static getInstance() factory method just inside the try block. Once a valid handle to the Configuration Manager is obtained, the application attempts to discover the name of the Configuration Manager (cmp.getName()) and display it.
Note: getName() - and other methods that request information from the Configuration Manager - block until the information is supplied, or a timeout occurs.

This means that if the Configuration Manager is not running, the application hangs for a period. It is possible to control the timeout period by using the ConfigManagerProxy.setRetryCharacteristics() method. Generally, however, blocking only occurs when a given resource is accessed for the first time within an application.

Finally, the disconnect() method is called. This method frees up resources associated with the connection in both the CMP and Configuration Manager.
Note: When a ConfigManagerProxy handle is first returned from the getInstance() method, the Configuration Manager service is not necessarily running. It is only when the application attempts to make use of the handle (by calling getName() in this example) that the application can be assured that a two-way connection with the Configuration Manager is active
Related tasks
Configuring an environment for developing and running Configuration Manager Proxy applications