Writing java classes to perform queue and topic operations in IBM WebSphere MQ

IBM WebSphere MQ

A WebSphere MQ queue is a named object on which applications can put messages, and from which applications can get messages.Messages are stored on a queue, so that if the putting application is expecting a reply to its message, it is free to do other work while waiting for that reply.Before a message can be put on a queue, the queue must have already been created. A queue is owned by a queue manager, and that queue manager can own many queues. However, each queue must have a name that is unique within that queue manager.Following you get an example of opening a queue to put message.

MQQueueManager queueManager;

//initialize the queue manager here

MQQueue queue = queueManager.accessQueue("myqueue", CMQC.MQOO_OUTPUT);
MQMessage mqMessage = new MQMessage();

//prepare message here

queue.put(mqMessage);
queue.close();
Below you get an example of opening a queue to get messages.
MQQueue queue = queueManager.accessQueue("myqueue",
CMQC.MQRC_READ_AHEAD_MSGS);
MQMessage mqMessage = new MQMessage();
queue.get(message);
queue.close();
Assume you need to get messages of specific message ID without getting all the messages received at queue.You can filter messages using MQGetMessageOptions.
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.matchOptions = MQConstants.MQMO_MATCH_MSG_ID;
MQQueue queue = queueManager.accessQueue("myqueue",
CMQC.MQRC_READ_AHEAD_MSGS);
MQMessage mqMessage = new MQMessage();
message.messageId = "MESSAGE_ID".getBytes();
queue.get(message,gmo);
queue.close();
If you need to get messages that match both message ID and correlation ID you can change the above code as follows.
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.matchOptions = MQConstants.MQMO_MATCH_MSG_ID ;
MQQueue queue = queueManager.accessQueue("myqueue",
CMQC.MQRC_READ_AHEAD_MSGS);
MQMessage mqMessage = new MQMessage();
message.messageId = "MESSAGE_ID".getBytes();
queue.get(message,gmo);
queue.close();

A topic is the subject of the information that is published in a publish/subscribe message.Messages in point-to-point systems are sent to a specific destination address. Messages in subject-based publish/subscribe systems are sent to subscribers based on the subject that describes the contents of the message. In content-based systems, messages are sent to subscribers based on the contents of the message itself. The IBM MQ publish/subscribe system is a subject-based publish/subscribe system. A publisher creates a message, and publishes it with a topic string that best fits the subject of the publication. To receive publications, a subscriber creates a subscription with a pattern matching topic string to select publication topics. The queue manager delivers publications to subscribers that have subscriptions that match the publication topic, and are authorized to receive the publications.Following you get an example of opening a topic for a publication.
MQQueueManager queueManager;

//initialize the queue manager here

MQTopic publisher = queueManager.accessTopic("TOPIC_STRING", "TOPIC_NAME", CMQC.MQTOPIC_OPEN_AS_PUBLICATION, CMQC.MQOO_OUTPUT);
MQMessage mqMessage = new MQMessage();

//prepare message here

publisher.put(mqMessage);

TOPIC_STRING is a character string that identifies the topic of a publish/subscribe message. You can use any characters you like when you construct a topic string.For a subscription we can modify the above as follows.
MQTopic subcriber = queueManager.accessTopic("TOPIC_STRING","TOPIC_NAME",
                            CMQC.MQTOPIC_OPEN_AS_SUBSCRIPTION, CMQC.MQSO_CREATE);
For a durable subscription we can use the following.
int option = CMQC.MQSO_CREATE | CMQC.MQSO_FAIL_IF_QUIESCING | CMQC.MQSO_MANAGED | CMQC.MQSO_DURABLE;
MQTopic subscriber = queueManager.accessTopic("TOPIC_STRING","TOPIC_NAME",
                            option, null, config.getSubscriptionName());

Comments