Oracle GlassFish Message Queue 4.4.2 Developer's Guide for Java Clients

Creating, Initializing and Starting an Embedded Broker

To create, initialize, and start an embedded broker, you:

  1. Create a broker instance in the client runtime.

  2. Create a broker event listener.

  3. Define properties to use when initializing the broker instance.

  4. Initialize the broker instance.

  5. Start the broker instance.

The following listing shows an example of creating, initializing, and starting an Embedded Broker. In this example, args represents the string of arguments to pass as properties when initializing the broker instance, and EmbeddedBrokerEventListener is an existing class that implements the BrokerEventListener interface.

import com.sun.messaging.jmq.jmsclient.runtime.BrokerInstance;
import com.sun.messaging.jmq.jmsclient.runtime.ClientRuntime;
import com.sun.messaging.jmq.jmsservice.BrokerEvent;
import com.sun.messaging.jmq.jmsservice.BrokerEventListener;

// Obtain the ClientRuntime singleton object
ClientRuntime clientRuntime = ClientRuntime.getRuntime();

// Create a broker instance
BrokerInstance brokerInstance = clientRuntime.createBrokerInstance();

// Create a broker event listener
BrokerEventListener listener = new EmbeddedBrokerEventListener();

// Convert the broker arguments into Properties. Note that parseArgs is
// a utility method that does not change the broker instance.
Properties props = brokerInstance.parseArgs(args);

// Initialize the broker instance using the specified properties and
// broker event listener
brokerInstance.init(props, listener);

// now start the embedded broker

Creating a Broker Event Listener

When initializing an embedded broker, you must provide a broker event listener. This listener is an instance of a class that implements the BrokerEventListener interface. This interface specifies two methods:

The following listing shows an example class that implements the BrokerEventListener interface.

class EmbeddedBrokerEventListener implements BrokerEventListener {

    public void brokerEvent(BrokerEvent brokerEvent) {
        System.out.println ("Received broker event:"+brokerEvent);

    public boolean exitRequested(BrokerEvent event, Throwable thr) {
        System.out.println ("Broker is requesting a shutdown because of:"+event+" with "+thr);
        // return true to allow the broker to shutdown
        return true;

Arguments to Specify When Initializing an Embedded Broker

When initializing an embedded broker, you can provide a list of arguments as properties.

Because a Java client runtime (not the imqbrokerd utility) is initializing the broker, you should specify these arguments:

-imqhome path

The home directory of the Message Queue installation (see Table P–5)

-libhome path

The directory in which Message Queue libraries are stored. This location depends on the distribution from which Message Queue was installed: 

  • From an IPS image: IMQ_HOME/lib

  • From Solaris SVR4 packages: /usr/share/lib/imq

  • From Linux RPMs: /opt/sun/mq/share/lib

-varhome path

The directory in which Message Queue temporary or dynamically created configuration and data files are stored installation (see Table P–5)

You can also specify imqbrokerd options as arguments. Two useful options to specify as arguments are:

-name instanceName

The instance name of the broker. 

-port portNumber

The port number for the broker's Port Mapper. This is port number on which the broker listens for client connections.