Accessing queues, topics, and processes in IBM MQ classes for Java

To access queues, topics, and processes, use methods of the MQQueueManager class. The MQOD (object descriptor structure) is collapsed into the parameters of these methods.


Queues

To open a queue we can use the accessQueue method of the MQQueueManager class. For example, on a queue manager called queueManager, use the following code:
MQQueue queue = queueManager.accessQueue("qName",CMQC.MQOO_OUTPUT);

The accessQueue method returns a new object of class MQQueue.

When you have finished using the queue, use the close() method to close it, as in the following example:
queue.close();
We can also create a queue by using the MQQueue constructor. The parameters are exactly the same as for the accessQueue method, with the addition of a queue manager parameter. For example:
MQQueue queue = new MQQueue(queueManager,
                            "qName",
                            CMQC.MQOO_OUTPUT,
                            "qMgrName",
                            "dynamicQName",
                            "altUserID");
We can specify a number of options when you create queues. For details of these, see Class.com.ibm.mq.MQQueue. Constructing a queue object in this way enables you to write your own subclasses of MQQueue.


Topics

Similarly, we can open a topic using the accessTopic method of the MQQueueManager class. For example, on a queue manager called queueManager, use the following code to create a subscriber and publisher:
MQTopic subscriber =
      queueManager.accessTopic("TOPICSTRING","TOPICNAME",
      CMQC.MQTOPIC_OPEN_AS_SUBSCRIPTION, CMQC.MQSO_CREATE);
MQTopic publisher =
      queueManager.accessTopic("TOPICSTRING","TOPICNAME",
      CMQC.MQTOPIC_OPEN_AS_PUBLICATION, CMQC.MQOO_OUTPUT);

When you have finished using the topic, use the close() method to close it.

We can also create a topic by using the MQTopic constructor. The parameters are exactly the same as for the accessTopic method, with the addition of a queue manager parameter. For example:
MQTopic subscriber = new
      MQTopic(queueManager,"TOPICSTRING","TOPICNAME",
      CMQC.MQTOPIC_OPEN_AS_SUBSCRIPTION, CMQC.MQSO_CREATE);

We can specify a number of options when you create topics. For details of these, see Class com.ibm.mq.MQTopic. Constructing a topic object in this way enables you to write your own subclasses of MQTopic.

A topic must be opened either for publication or for subscription. The MQQueueManager class has eight accessTopic methods and the Topic class has eight constructors. In each case, four of these have a destination parameter and four have a subscriptionName parameter (including two that have both). These can only be used to open the topic for subscriptions. The two remaining methods have an openAs parameter, and the topic can be opened for either publication or subscription depending on the value of the openAs parameter.

To create a topic as a durable subscriber use either an accessTopic method of the MQQueueManager class or an MQTopic constructor that accepts a subscription name and, in either case, set the CMQC.MQSO_DURABLE option.


Processes

To access a process, use the accessProcess method of the MQQueueManager. For example, on a queue manager called queueManager, use the following code to create an MQProcess object:
MQProcess process =
queueManager.accessProcess("PROCESSNAME",
CMQC.MQOO_FAIL_IF_QUIESCING);

To access a process, use the accessProcess method of the MQQueueManager.

The accessProcess method returns a new object of class MQProcess.

When you have finished using the process object, use the close() method to close it, as in the following example:
process.close();
We can also create a process by using the MQProcess constructor. The parameters are exactly the same as for the accessProcess method, with the addition of a queue manager parameter. For example:
MQProcess process =
      new MQProcess(queueManager,"PROCESSNAME",
      CMQC.MQOO_FAIL_IF_QUIESCING);

Constructing a process object in this way enables you to write your own subclasses of MQProcess.

Parent topic: Writing IBM MQ classes for Java applications