WebSphere Message Broker, Version 8.0.0.7 Operating Systems: AIX, HP-Itanium, Linux, Solaris, Windows, z/OS

See information about the latest product version

Connecting to a broker from a CMP application

Connect an application that uses the CMP API to a broker, to send requests about its status and its resources.

Before you start

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

Consider the following program BrokerRunStateChecker.java. It connects to a broker that is running on the default queue manager of the local computer.

import com.ibm.broker.config.proxy.*;

public class BrokerRunStateChecker {

    public static void main(String[] args) {

	    	// The ip address of where the broker is running
  		  	// and the port number of the queue manager listener.
        displayBrokerRunState("localhost", 2414, "");
    }

    public static void displayBrokerRunState(String hostname,
                                             int port,
                                             String qmgr) {
        BrokerProxy b = null;
        try {
            BrokerConnectionParameters bcp =
                new MQBrokerConnectionParameters(hostname, port, qmgr);
            b = BrokerProxy.getInstance(bcp);
            String brokerName = b.getName();
            
            System.out.println("Broker '"+brokerName+
                "' is available!");
            b.disconnect();
        } catch (ConfigManagerProxyException ex) {
            System.out.println("Broker is NOT available"+
                " because "+ex);
        }
    }
}

The first line of the program requests Java™ to import the CMP API classes, which are supplied in the package com.ibm.broker.config.proxy.

The first line of code inside the try block of the displayBrokerRunState() method instantiates a BrokerConnectionParameters object. This method is an interface which states that implementing classes are able to provide the parameters to connect to a broker.

The class MQBrokerConnectionParameters implements this interface; it defines a set of WebSphere® MQ connection parameters. The constructor used here takes three parameters:
  1. The host name of the computer that the broker is running on
  2. The port on which the WebSphere MQ listener service for the broker is listening
  3. The name of the queue manager that is associated with the broker

When you have defined this object, you can connect to the queue manager that is defined by those characteristics. The connection is achieved by the static getInstance() factory method just inside the try block. When a valid handle to the queue manager is returned, the application requests the name of the broker by using b.getName(), and displays it.

getName(), and other methods that request information from the broker, block until the information is supplied, or a timeout occurs. Therefore, if the broker is not running, the application hangs for a period. You can control the timeout period by using the BrokerProxy.setRetryCharacteristics() method. Typically, blocking only occurs when a given resource is accessed for the first time within an application.

Finally, the program calls the disconnect() method. This method frees up resources associated with the connection in both the CMP and the broker.

When a BrokerProxy handle is first returned from the getInstance() method, the broker service does not have to be running. It is only when the application uses the handle (by calling getName() in this example) that the application can be assured that a two-way connection with the broker is active.

Notices | Trademarks | Downloads | Library | Support | Feedback

Copyright IBM Corporation 1999, 2016Copyright IBM Corporation 1999, 2016.

        
        Last updated:
        
        Last updated: 2016-05-23 14:48:06


Task topicTask topic | Version 8.0.0.7 | be43130_