本人也是新手 刚刚接触IBM mq在这发表点心得!!!!求大神指点指点
public class MessageByMQ {
private static String qmName;
static String queueString = "LOCALQUEUE"; //队列名称
private static MQQueueManager qMgr;
private static final int CCSID = 1381;
;
static {
MQEnvironment.hostname = "127.0.0.1"; //ip地址
MQEnvironment.channel = "SERVERCONN";// 通道
MQEnvironment.CCSID = CCSID;// 编码格式
MQEnvironment.port = 5000; //端口号
qmName = "MyTest"; //队列管理器名称
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,
MQC.TRANSPORT_MQSERIES_BINDINGS);
try {
qMgr = new MQQueueManager(qmName);
} catch (MQException e) {
// TODO Auto-generated catch block
System.out.println("初始化失败");
e.printStackTrace();
}
}
/**
*
*
* @param message
* @return
*/
public static int sendMessage(String message) {
int result = 0;
try {
int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
if (qMgr == null || !qMgr.isConnected()) {
qMgr = new MQQueueManager(qmName);
}
MQQueue queue = qMgr.accessQueue(queueString, openOptions);
MQMessage putMessage = new MQMessage();
putMessage.writeUTF(message);
MQPutMessageOptions pmo = new MQPutMessageOptions();
queue.put(putMessage, pmo);
queue.close();
} catch (MQException ex) {
System.out.println("A WebSphere MQ error occurred : Completion code "
+ ex.completionCode + " Reason code " + ex.reasonCode);
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("An error occurred whilst writing to the message buffer: " + ex);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
qMgr.disconnect();
} catch (MQException e) {
e.printStackTrace();
}
}
return result;
}
public static void getMessage() {
MQQueue queue = null;
try {
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE;
MQGetMessageOptions gmo = new MQGetMessageOptions();
queue = qMgr.accessQueue(queueString, openOptions, null, null, null);
System.out.println("该队列当前的深度为:" + queue.getCurrentDepth());
System.out.println("===========================");
int depth = queue.getCurrentDepth();
while (depth-- > 0) {
MQMessage msg = new MQMessage();
queue.get(msg, gmo);
System.out.println("消息的大小为:" + msg.getDataLength());
System.out.println("消息的内容:\n" + msg.readLine());
System.out.println("---------------------------");
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (queue != null) {
try {
qMgr.disconnect();
queue.close();
} catch (MQException e) {
e.printStackTrace();
}
}
}
// return message;
}
public static void main(String args[]) {
//sendMessage("测试333");
getMessage();
}
以上是代码
需要的jar包