PutMessage sends 10 messages to the queue
MQReader reads (and consumes) 1 message at a time (getAllMessages() still fails on the getQueueDepth()... investigating...)
package com.acme.mq;
import com.ibm.mq.*;
import java.io.*;
import static com.acme.mq.MQConstants.*;
public class PutMessage {
private String qManager = MQ_queueManager;
private String qName = MQ_queue;
private String qmessageFile = "c:/pierre/mqfile.txt";
public static void main(String args[]) throws Exception {
System.out.println("java.library.path=" + System.getProperty("java.library.path"));
PutMessage pM = new PutMessage();
pM.runNow();
}
public void runNow() throws Exception {
System.out.println("Connecting to queue manager: " + qManager);
MQQueueManager qMgr = MQReader.setup();
int openOptions = 17;
System.out.println("Accessing queue: " + qName);
MQQueue queue = qMgr.accessQueue(qName, openOptions);
File file = new File(qmessageFile);
for (int i = 0; i < 10; i++) {
String qmessage = getContents(file) + " " + i;
MQMessage msg = new MQMessage();
msg.writeString(qmessage);
MQPutMessageOptions pmo = new MQPutMessageOptions();
System.out.println("Sending message: " + qmessage);
queue.put(msg, pmo);
}
System.out.println("Closing the queue");
queue.close();
System.out.println("Disconnecting from the Queue Manager");
qMgr.disconnect();
System.out.println("Done!");
}
private String getContents(File aFile) {
StringBuffer contents;
contents = new StringBuffer();
BufferedReader input = null;
try {
input = new BufferedReader(new FileReader(aFile));
for (String line = null; (line = input.readLine()) != null;) {
contents.append(line);
//contents.append(System.getProperty("line.separator"));
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (input != null)
input.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return contents.toString();
}
}
package com.acme.mq;
import com.ibm.mq.*;
import java.text.*;
import java.io.*;
import java.util.Hashtable;
import static com.acme.mq.MQConstants.*;
public class MQReader {
public static void main(String[] args) throws Exception {
MQReader mqReader = new MQReader();
mqReader.getMessage();
}
public void getAllMessages() throws MQException, IOException {
MQQueueManager qm = setup();
int options = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_INQUIRE;
MQQueue q = qm.accessQueue(MQ_queue, options, null, null, null);
int depth = q.getCurrentDepth();
DecimalFormat indexFormat = new DecimalFormat(Integer.toString(depth).replaceAll(".", "0"));
System.out.println("found messages " + depth);
for (int index = 0; index < depth; index++) {
MQMessage msg = new MQMessage();
q.get(msg, new MQGetMessageOptions());
int msgLength = msg.getMessageLength();
String text = msg.readStringOfByteLength(msgLength);
System.out.println("message#" + index + " text=" + text);
}
}
public void getMessage() {
try
{
MQQueueManager qm = setup();
MQQueue q = qm.accessQueue(MQ_queue, MQC.MQOO_INPUT_AS_Q_DEF);
MQMessage msg = new MQMessage();
q.get(msg);
System.out.println("Message: " + msg.readLine());
q.close();
qm.disconnect();
}
catch(MQException e)
{
System.out.println("MQ Error: cc=" + e.completionCode + ", reason=" + e.reasonCode);
}
catch(java.io.IOException e)
{
System.out.println("IO Error: " + e);
}
}
public static MQQueueManager setup() throws MQException {
MQEnvironment.hostname = MQ_hostname;
MQEnvironment.channel = MQ_channel;
MQEnvironment.port = MQ_port;
Hashtable<String, String> props = new Hashtable<String, String>();
props.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
MQEnvironment.properties = props;
MQQueueManager qm = new MQQueueManager(MQ_queueManager);
return qm;
}
}
package com.acme.mq;
public class MQConstants {
public static String MQ_hostname = "bla.acme.com";
public static String MQ_channel = "A123.TO.QMIA00D";
public static int MQ_port = 1435;
public static String MQ_queueManager = "QMIA00D";
public static String MQ_queue = "AQ.A123.BLA.NOTIFICATION.EVENT";
}
I have added to the classpath ALL the MQ jars I could find in my MQ installation...
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="lib/CL3Export.jar"/>
<classpathentry kind="lib" path="lib/CL3Nonexport.jar"/>
<classpathentry kind="lib" path="lib/com.ibm.mq.axis2.jar"/>
<classpathentry kind="lib" path="lib/com.ibm.mq.commonservices.jar"/>
<classpathentry kind="lib" path="lib/com.ibm.mq.defaultconfig.jar"/>
<classpathentry kind="lib" path="lib/com.ibm.mq.headers.jar"/>
<classpathentry kind="lib" path="lib/com.ibm.mq.jar"/>
<classpathentry kind="lib" path="lib/com.ibm.mq.jmqi.jar"/>
<classpathentry kind="lib" path="lib/com.ibm.mq.jms.Nojndi.jar"/>
<classpathentry kind="lib" path="lib/com.ibm.mq.pcf.jar"/>
<classpathentry kind="lib" path="lib/com.ibm.mq.postcard.jar"/>
<classpathentry kind="lib" path="lib/com.ibm.mq.soap.jar"/>
<classpathentry kind="lib" path="lib/com.ibm.mq.tools.ras.jar"/>
<classpathentry kind="lib" path="lib/com.ibm.mqjms.jar"/>
<classpathentry kind="lib" path="lib/connector.jar"/>
<classpathentry kind="lib" path="lib/dhbcore.jar"/>
<classpathentry kind="lib" path="lib/fscontext.jar"/>
<classpathentry kind="lib" path="lib/jms.jar"/>
<classpathentry kind="lib" path="lib/jndi.jar"/>
<classpathentry kind="lib" path="lib/jta.jar"/>
<classpathentry kind="lib" path="lib/ldap.jar"/>
<classpathentry kind="lib" path="lib/providerutil.jar"/>
<classpathentry kind="lib" path="lib/rmm.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Friday, June 17, 2011
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment