IBM MQ 简单示例

本文参考:

http://blaiu.iteye.com/blog/829846

http://wenku.baidu.com/view/3fafa9cba1c7aa00b52acbd0.html

http://wenku.baidu.com/view/129678283169a4517723a3c9.html

http://keepmoving2012.iteye.com/blog/1395378

先添加IBM MQ的jar包,别忘了connector.jar,不然会报错。

TestMQ
 1 public class TestMQ {
 2     private MQQueueManager qmanager = null;
 3     private MQQueue queue = null;
 4 
 5     public TestMQ() {
 6         try {
 7             MQEnvironment.hostname = MQ_HOST;
 8             MQEnvironment.port = MQ_PORT;
 9             MQEnvironment.channel = MQ_CHANNEL;
10             qmanager = new MQQueueManager(MQ_QUEUEMANAGER_NAME);
11             int openOptions = MQConstants.MQOO_INPUT_AS_Q_DEF|MQConstants.MQOO_INQUIRE|MQConstants.MQOO_OUTPUT;
12             queue = qmanager.accessQueue(MQ_QUEUE_NAME, openOptions);
13         } catch (MQException e) {
14             e.printStackTrace();
15         }
16     }
getQueueDepth
 1 public Integer getQueueDepth() {
 2         Integer depth = 0;
 3            try {
 4                 if (queue != null && queue.isOpen()) {
 5                     depth = queue.getCurrentDepth();
 6                 }
 7             } catch (MQException e) {
 8                 // TODO Auto-generated catch block
 9                 e.printStackTrace();
10             }
11         
12         return depth;
13     }
close queue&qmanager
 1 public void close() {
 2         if (queue != null && queue.isOpen()) {
 3             try {
 4                 queue.close();
 5             } catch (MQException e) {
 6                 e.printStackTrace();
 7             }
 8         }
 9         if (qmanager != null && qmanager.isConnected()) {
10             try {
11                 qmanager.disconnect();
12             } catch (MQException e) {
13                 e.printStackTrace();
14             }
15         }
16     }
send message to the queue
 1 public void send(String msg) throws MQException {
 2         try {
 3             
 4             MQPutMessageOptions mqPutMessageOptions = new MQPutMessageOptions();      
 5             MQMessage mqMessage = new MQMessage(); 
 6             mqMessage.write(msg.getBytes("utf-8"));
 7             queue.put(mqMessage, mqPutMessageOptions);
 8             System.out.println("Sent a message:" + msg);
 9             
10         } catch (UnsupportedEncodingException e) {
11             // TODO Auto-generated catch block
12             e.printStackTrace();
13         } catch (IOException e) {
14             // TODO Auto-generated catch block
15             e.printStackTrace();
16         }      
17     }
receive a message from the queue
 1 public String receive() throws MQException{
 2         MQMessage mqMsg = new MQMessage();
 3         MQGetMessageOptions mqGetMessageOptions = new MQGetMessageOptions();
 4         try {
 5             
 6             queue.get(mqMsg, mqGetMessageOptions);
 7             int len = mqMsg.getDataLength();
 8             byte[] message = new byte[len];
 9             mqMsg.readFully(message, 0, len);
10             String str = new String(message);
11             System.out.println("Received a message:" + str);
12             return str;
13         } catch (MQException e) {
14             // TODO Auto-generated catch block
15             e.printStackTrace();
16             return null;
17         } catch (IOException e) {
18             // TODO Auto-generated catch block
19             e.printStackTrace();
20             return null;
21         }
22         
23     }
receive all the messages in the queue
 1 public void receiveAll() {
 2         try {
 3             int depth = queue.getCurrentDepth();
 4             System.out.println("The current depth of queue is " + depth);
 5             System.out.println("Now receive all messages....");
 6             while(depth-- > 0){
 7                 MQMessage mqMsg = new MQMessage();
 8                 MQGetMessageOptions mqGetMessageOptions = new MQGetMessageOptions();
 9                 queue.get(mqMsg, mqGetMessageOptions);
10                 int len = mqMsg.getDataLength();
11                 byte[] message = new byte[len];
12                 mqMsg.readFully(message, 0, len);
13                 System.out.println(new String(message));
14                 
15             }
16         } catch (MQException e) {
17             // TODO Auto-generated catch block
18             e.printStackTrace();
19         } catch (IOException e) {
20             // TODO Auto-generated catch block
21             e.printStackTrace();
22         } 
23         
24         
25     }
main
 1 public static void main(String[] args) {
 2         TestMQ mq = new TestMQ();
 3         
 4        try {
 5         mq.send("hello everyone");
 6         mq.send("WHAT");
 7 //      System.out.println(mq.receive());
 8     } catch (MQException e) {
 9         // TODO Auto-generated catch block
10         e.printStackTrace();
11     }
12         mq.receiveAll();
13         Integer depth = mq.getQueueDepth(); 
14         System.out.println("The current depth of queue is " + depth);
15         mq.close();
16     }

TestMQ还得加一些参数,如下:

parameters
1     public static final String MQ_QUEUEMANAGER_NAME = "";
2     public static final String MQ_QUEUE_NAME = "";
3     public static final String MQ_HOST = "";
4     public static final String MQ_CHANNEL = "";
5     public static final Integer MQ_PORT = ;

运行结果:


 

posted @ 2012-04-24 16:49  Matrix54  阅读(9855)  评论(2编辑  收藏  举报