IBM MQ 9.1 教程三:远程访问IBM MQ队列

IBM MQ 9.1 教程三:远程访问IBM MQ队列

2019年01月04日 14:43:48 可还记得你我的誓言 阅读数:34

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013231970/article/details/85774856

当应用程序不和IBM MQ的安装程序在一台主机上时,就需要通过IP远程访问IBM MQ队列了,此时可用TCP/IP协议

1. 在创建队列 管理器时,要勾选创建服务器连接通道

IBM MQ 9.1 教程三:远程访问IBM MQ队列

2. 创建一个本地队列Q1

3. 右键通道选项,新建服务器连接通道,名称SVRCONN.ONE

IBM MQ 9.1 教程三:远程访问IBM MQ队列

4. 代码访问示例,此示例访问windows上的IBM MQ,windows系统设置了用户名和密码

 
  1. import com.ibm.mq.*;

  2. import com.ibm.mq.constants.MQConstants;

  3.  
  4. public class MqDemo {

  5.  
  6. private MQQueueManager qMgr;

  7. private MQQueue sendQueue;

  8. private MQQueue receiveQueue;

  9. private static int CCSID = 1381;

  10. private static String hostname = "192.168.1.133";

  11. int port = 1466;

  12. static String queueManagerName = "QM_2";

  13. static String queueName = "Q1";

  14. static String channel = "SVRCONN.ONE";

  15. static String userId = "tiger";

  16. static String password = "tiger";

  17.  
  18. public MqDemo() throws MQException {

  19.  
  20. MQEnvironment.hostname = hostname;

  21. MQEnvironment.port = port;

  22. MQEnvironment.channel = channel;

  23. MQEnvironment.CCSID = CCSID;

  24. MQEnvironment.userID = userId; //MQ中拥有权限的用户名

  25. MQEnvironment.password = password; //用户名对应的密码

  26. qMgr = new MQQueueManager(queueManagerName);//创建队列管理器

  27.  
  28. }

  29.  
  30.  
  31. public void sendMsg(String msgStr) {

  32. int openOptions = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_OUTPUT | MQConstants.MQOO_INQUIRE;

  33. try {

  34. // 建立通道的连接

  35. sendQueue = qMgr.accessQueue(queueName, openOptions, null, null, null);

  36.  
  37. MQMessage msg = new MQMessage();// 要写入队列的消息

  38. msg.format = MQConstants.MQFMT_STRING;

  39. msg.characterSet = CCSID;

  40. msg.encoding = CCSID;

  41. msg.writeUTF(msgStr);

  42.  
  43. MQPutMessageOptions pmo = new MQPutMessageOptions();

  44. msg.expiry = -1; // 设置消息用不过期

  45.  
  46. sendQueue.put(msg, pmo);// 将消息放入队列

  47. qMgr.disconnect();

  48. } catch (Exception e) {

  49. e.printStackTrace();

  50. } finally {

  51. if (sendQueue != null) {

  52. try {

  53. sendQueue.close();

  54. } catch (MQException e) {

  55. e.printStackTrace();

  56. }

  57. }

  58. }

  59. }

  60.  
  61. public void receiveMsg() {

  62. int openOptions = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_OUTPUT | MQConstants.MQOO_INQUIRE;

  63. try {

  64. receiveQueue = qMgr.accessQueue(queueName, openOptions, null, null, null);

  65. int depth = receiveQueue.getCurrentDepth();

  66. System.out.println("该队列当前的深度为:" + depth);

  67.  
  68. // 将队列的里的消息读出来

  69. while (depth-- > 0) {

  70. MQMessage rcvMessage = new MQMessage();// 要读的队列的消息

  71. MQGetMessageOptions gmo = new MQGetMessageOptions();

  72. receiveQueue.get(rcvMessage, gmo);

  73. String msgText = rcvMessage.readUTF();

  74. System.out.println("消息的内容:" + msgText);

  75. }

  76. } catch (Exception e) {

  77. e.printStackTrace();

  78. } finally {

  79. if (receiveQueue != null) {

  80. try {

  81. receiveQueue.close();

  82. } catch (MQException e) {

  83. e.printStackTrace();

  84. }

  85. }

  86. try {

  87. qMgr.disconnect();

  88. } catch (MQException e) {

  89. e.printStackTrace();

  90. }

  91. }

  92. }

  93.  
  94.  
  95.  
  96. public static void main(String[] args) throws MQException {

  97. new MqDemo().sendMsg("消息1");

  98. new MqDemo().sendMsg("消息2");

  99. new MqDemo().sendMsg("消息3");

  100. new MqDemo().receiveMsg();

  101. }

  102. }