Running the MQPubSubApiSample Java sample

How to run the MQPubSubApiSample using the Java Development Tools from the Eclipse platform.

Before you begin

Open the Eclipse workbench. Create a new workspace directory and select it. Close the welcome window.

Follow the steps in Preparing and running the sample programs prior to running as a client.

About this task

The Java publish/subscribe sample program is an IBM® MQ MQI client Java program. The sample runs without modification using a default queue manager listening on port 1414. The task describes this simple case, and indicates in general terms how to provide parameters and modify the sample to suit different IBM MQ configurations. The example is illustrated running on Windows. The file paths will differ on other platforms.

Procedure

  1. Import the Java sample programs
    1. In the workbench, click Window > Open perspective > Other > Java and click OK.
    2. Switch to the Package Explorer view.
    3. Right-click in the white-space in the Package Explorer view. Click New > Java project.
    4. In the Project name field type MQ Java Samples. Click Next.
    5. In the Java Settings panel, switch to the Libraries tab.
    6. Click Add External JARs.
    7. Browse to MQ_INSTALLATION_PATH \java\lib where MQ_INSTALLATION_PATH is the IBM MQ installation folder and select com.ibm.mq.jar and com.ibm.mq.jmqi.jar
    8. Click Open > Finish.
    9. Right-click src in the Package Explorer view.
    10. Select Import... > General > File System > Next > Browse... and browse to the path MQ_INSTALLATION_PATH \tools\wmqjava\samples where MQ_INSTALLATION_PATH is the IBM MQ installation directory.
    11. On the Import panel, Figure 1, click samples (do not select the check box).
    12. Select MQPubSubApiSample.java. The Into folder field should contain MQ Java Samples/src. Click Finish.
    Figure 1. File system import
    The file, MQPubSubApiSample.java, is selected in the right pane of the Import from File system panel that lists all the files in the ...\tools\wmqjava\samples directory.
  2. Run the publish/subscribe sample program.
    There are two ways to run the program, depending on whether you need to change the default parameters.
    • The first choice runs the program without making any changes:
      • In the workspace main menu, expand the src folder. Right-click MQPubSubApiSample.java Run-as > 1. Java Application
    • The second choice runs the program with parameters or with modified source code for your environment:
      • Open MQPubSubApiSample.java and study the MQPubSubApiSample constructor.
      • Modify the attributes of the program.

        These attributes are modifiable using the -D JVM switch, or by providing a default value for the System property by editing the source code.

        • topicObject
        • queueManagerName
        • subscriberCount

        These attributes are changeable only by editing the source code in the constructor.

        • hostname
        • port
        • channel

        To set System properties, code a default value in the accessor, for example:

        
        queueManagerName = System.getProperty("com.ibm.mq.pubSubSample.queueManagerName", "QM3");
        

        Or provide the parameter to the JVM using the -D option, as shown in the following steps:

      1. Copy the full name of the System.Property you want to set, for example: com.ibm.mq.pubSubSample.queueManagerName.
      2. In the workspace, right-click Run > Open Run Dialog. Double click Java Application in Create, Manage and Run applications and click the (x) = Arguments tab.
      3. In the VM arguments: pane, type -D and paste the System.property name, com.ibm.mq.pubSubSample.queueManagerName, followed by =QM3. Click Apply > Run.
      4. Add further arguments as a comma separated list, or as additional lines in the pane, without comma separators.

        For example: -Dcom.ibm.mq.pubSubSample.queueManagerName=QM3, -Dcom.ibm.mq.pubSubSample.subscriberCount=6.