Reputation: 11
When I execute my below code, it will read data from MQ on the console and then delete all the data from the queue. I don't want my data to be deleted from the queue while reading from MQ. I want to make it parameterize and also wants to write the data to excel. Can anybody please help me out from this. Below is my code that I am using.
public class MQReadJava
{
private MQQueueManager _queueManager = null;
public int port = 1416;
public String hostname = "xyz";
public String channel = "SYSTEM.ABC.123";
public String qManager = "ABC.BAW";
public String inputQName = "MYQUEUE";
public MQReadJava()
{
super();
}
private void init(String[] args) throws IllegalArgumentException
{
// Set up MQ environment
MQEnvironment.hostname = hostname;
MQEnvironment.channel = channel;
MQEnvironment.port = port;
}
public static void main(String[] args)throws IllegalArgumentException
{
MQReadJava readQ = new MQReadJava();
try
{
readQ.init(args);
readQ.selectQMgr();
readQ.read();
}
catch (IllegalArgumentException e)
{
System.exit(1);
}
catch (MQException e)
{
System.out.println(e);
System.exit(1);
}
}
private void selectQMgr() throws MQException
{
_queueManager = new MQQueueManager(qManager);
}
private void read() throws MQException
{
int openOptions = MQC.MQOO_INQUIRE + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INPUT_SHARED;
//int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING;
MQQueue queue = _queueManager.accessQueue( inputQName,
openOptions,
null, // default q manager
null, // no dynamic q name
null ); // no alternate user id
System.out.println("MQRead is now connected.\n");
int depth = queue.getCurrentDepth();
System.out.println("Current depth: " + depth + "\n");
if (depth == 0)
{
return;
}
MQGetMessageOptions getOptions = new MQGetMessageOptions();
getOptions.options = MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING +
MQC.MQGMO_CONVERT;
while(true)
{
MQMessage message = new MQMessage();
try
{
queue.get(message, getOptions);
byte[] b = new byte[message.getMessageLength()];
message.readFully(b);
System.out.println(new String(b));
message.clearMessage();
}
catch (IOException e)
{
System.out.println("IOException during GET: " + e.getMessage());
break;
}
catch (MQException e)
{
if (e.completionCode == 2 && e.reasonCode == MQException.MQRC_NO_MSG_AVAILABLE) {
if (depth > 0)
{
System.out.println("All messages read.");
}
}
else
{
System.out.println("GET Exception: " + e);
}
break;
}
}
queue.close();
_queueManager.disconnect();
}
}
Upvotes: 1
Views: 20434
Reputation: 7456
Below is my code that I am using.
:) You downloaded my MQRead program. If you don't want it to do a destructive MQGET then you need to update the code to do a browse (see JoshMc's comments). Why didn't you download my MQBrowse program? Finally, please start reading the MQ Knowledge Center as it contains lots & lots of information for beginners to IBM MQ.
Update: You should not use MQEnvironment class as it is not thread safe. Put the connection information in a Hashtable. See here for an example: Java program to connect WMQ with User Id instead of channel
Upvotes: 5
Reputation: 10642
In order to not delete messages from the queue you need to browse the queue, this would be accomplished by updating your openOptions and getOptions as follows:
int openOptions = MQC.MQOO_INQUIRE + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_BROWSE;
and
getOptions.options = MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING + MQC.MQGMO_CONVERT + MQC.MQGMO_BROWSE_NEXT;
Checkout this great list of sample IBM MQ Classes for Java applications on Capitalware's website: Sample IBM MQ Java Code
Upvotes: 2