Can you please let us know how to pass the Message Consumer variable to get function?
following is our code
and which jar do we need to use ? Is it javax.jms.jar?
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.constants.MQConstants;
public class ReadMQ {
public static void main(String[] args) throws MQException {
String host = "10.65.139.19";
String port = "port";
String username = "UN";
String password = "Pass";
String queueManager = "QM";
String channel = "channel";
MQQueueManager manager;
MQQueue queue = null;
boolean getMore = true;
Hashtable<String, Object> mqht = new Hashtable<>();
mqht.put(CMQC.CHANNEL_PROPERTY,channel);
mqht.put(CMQC.HOST_NAME_PROPERTY, host);
mqht.put(CMQC.PORT_PROPERTY, new Integer(port));
mqht.put(CMQC.USER_ID_PROPERTY, username);
mqht.put(CMQC.PASSWORD_PROPERTY, password);
manager=new MQQueueManager(queueManager, mqht);
int openOptions = MQConstants.MQOO_BROWSE | MQConstants.MQOO_INPUT_EXCLUSIVE | MQConstants.MQOO_INQUIRE;
queue = manager.accessQueue("QA.QUEUE.1",openOptions);
MQGetMessageOptions gmo = new MQGetMessageOptions();
// MessageSelector Selector =new MessageSelector(manager);
// Selector.setFilter("priority", Selector., 0);
// gmo.options = MQConstants.MQGMO_WAIT + MQConstants.MQGMO_FAIL_IF_QUIESCING
// + MQConstants.MQGMO_CONVERT
// + MQConstants.MQGMO_NO_SYNCPOINT;
gmo.options = MQConstants.MQGMO_WAIT + MQConstants.MQGMO_FAIL_IF_QUIESCING
+ MQConstants.MQGMO_CONVERT + MQConstants.MQGMO_BROWSE_NEXT ;
}
try {
while(getMore)
{
MQMessage message = new MQMessage();
queue.get(message, gmo);
byte[] b = new byte[message.getMessageLength()];
message.readFully(b);
System.out.println(new String(b));
if (IsDestructiveRead) {
//System.out.println(message.expiry);
OutputStream out = new FileOutputStream("C:\\Users\\adminqa\\Documents\\Newfolder\\filename.dat");
out.write(message.expiry);
out.close();
manager.commit();
}
}
} catch (MQException e) {
if (e.reasonCode == 2033)
{
System.out.println("No message available");
}
if ( (e.completionCode == CMQC.MQCC_FAILED) &&(e.reasonCode == CMQC.MQRC_NO_MSG_AVAILABLE) ) {
}
else
{
System.out.println("MQException: " + e.getLocalizedMessage());
System.out.println("CC=" + e.completionCode + " : RC=" + e.reasonCode);
getMore = false;
}
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//manager.commit();
// manager.disconnect();
// queue.close();
}
}
------------------------------
Ranjitha YM
------------------------------
Original Message:
Sent: Mon March 20, 2023 01:35 PM
From: Morag Hughson
Subject: Websphere MQ filter Support for more fields
Hi Ranjitha,
The fields you show us that can be matched on are not queue attributes, but message attributes.
Match Option | Message attribute |
MQMO_MATCH_MSG_SEQ_NUMBER | MQMD.MsgSeqNumber |
MQMO_MATCH_GROUP_ID | MQMD.GroupId |
MQMO_MATCH_CORREL_ID | MQMD.CorrelId |
MQMO_MATCH_MSG_ID | MQMD.MsgId |
MQMO_MATCH_MSG_TOKEN | MQGMO.MsgToken |
MQMO_MATCH_OFFSET | MQMD.Offset |
You can use an SQL selector string to match on any field in the MQMD. Some example selector strings are shown in this post. You can use the selector string using the JMS API thus:-
// Create consumer with selector String selector = "Root.MQMD.MsgType = 4"; MessageConsumer cons = session.createConsumer(queDest, selector);
Cheers,
Morag
------------------------------
Morag Hughson
MQ Technical Education Specialist
MQGem Software Limited
Website: https://www.mqgem.com
Original Message:
Sent: Sun March 19, 2023 06:28 PM
From: Ranjitha YM
Subject: Websphere MQ filter Support for more fields
Team,
with java code, we are reading Queue messages, and also reading with filter.
We are using the below function to read queue messages with a filter. this function supports only 6 fields but Queue has more than 25 fields
1) gmo.matchOptions =
MQConstants.MQMO_MATCH_MSG_SEQ_NUMBER;
2) gmo.matchOptions = MQConstants.MQMO_MATCH_GROUP_ID;
3) gmo.matchOptions =
MQConstants.MQMO_MATCH_CORREL_ID;
4) gmo.matchOptions = MQConstants.MQMO_MATCH_MSG_ID;
5) gmo.matchOptions =
MQConstants.MQMO_MATCH_MSG_TOKEN;
6) gmo.matchOptions = MQConstants.MQMO_MATCH_OFFSET;
We want to filter data with all the fields. Can you please support the filter for all the fields ?
we are using the below IBM MQ Jars
com.ibm.mq
com.ibm.mq.jmqi
com.ibm.mq.pcf
please let us know If any other method available which supports all the fields.
------------------------------
Ranjitha YM
------------------------------