IBM WebSphere MQ C#
使用VMware虚拟机搭建
A机:192.168.163.130 MQ8.0
B机:192.168.163.129 MQ7.0
第一次使用amqmdnet.dll,amqmdxcs.dll,imqb23vn.dll,mqic32.dll开发的WMQ通信小工具,必须要本地安装一套webSphere MQ客户端才能通信,如下图的哪两个玩意。为此使用非常不爽,而且连接8.0的时候要使用8.0的dll。
本次不需要安装插件也能正常通信的小工具,如下图:
上图是为了测试两台WMQ设备A到B通信过程,当然用一台WMQ(本机入队、出队)测试也是没有问题的,如下图:
废话少说直接上核心代码:
1 using com.ibm.mq; 2 using com.ibm.mq.headers.pcf; 3 using com.ibm.msg.client.jms; 4 using com.ibm.msg.client.wmq.common; 5 using javax.jms; 6 using System; 7 using System.Collections; 8 using System.Collections.Generic; 9 using System.Linq; 10 using System.Text; 11 using System.Threading.Tasks; 12 using System.Windows; 13 using Queue = javax.jms.Queue; 14 15 namespace WPFIbmMqClientTools 16 { 17 /// <summary> 18 /// https://blog.csdn.net/MePlusPlus/article/details/52847580 19 /// </summary> 20 public class WMQHelper 21 { 22 public static string HostName; 23 public static int Port; 24 /// <summary> 25 /// 队列管理器名称 26 /// </summary> 27 public static string QmName; 28 /// <summary> 29 /// 通道名称 30 /// </summary> 31 public static string ChannelName; 32 33 private static JMSContext mQQueueManagerJMS = null; 34 private static Queue mQQueue = null; 35 private static JMSProducer producer = null; 36 private static JMSConsumer consumer = null; 37 private static MQQueueManager mQQueueManager = null; 38 private static MQQueue mQQueueSend = null; 39 private static MQQueue mQQueueRecv = null; 40 private static MQQueue mQQueueDepth = null; 41 //private static MQQueue mQQueueDepth = null; 42 43 public static string CreateWQMConnect() 44 { 45 string result = "队列管理器连接失败"; 46 if (mQQueueManager == null) 47 { 48 try 49 { 50 //var ff = JmsFactoryFactory.getInstance(JmsConstants.__Fields.WMQ_PROVIDER); 51 //var cf = ff.createConnectionFactory(); 52 //cf.setIntProperty(CommonConstants.__Fields.WMQ_CONNECTION_MODE, CommonConstants.__Fields.WMQ_CM_CLIENT); 53 //cf.setStringProperty(CommonConstants.__Fields.WMQ_HOST_NAME, HostName); 54 //cf.setIntProperty(CommonConstants.__Fields.WMQ_PORT, Port); 55 //cf.setStringProperty(CommonConstants.__Fields.WMQ_CHANNEL, ChannelName); 56 //cf.setStringProperty(CommonConstants.__Fields.WMQ_QUEUE_MANAGER, QmName); 57 ////cf.setStringProperty(CommonConstants.__Fields.WMQ_APPLICATIONNAME, "JMS WMQ EXAMPLE"); 58 ////cf.setStringProperty(CommonConstants.USERID, "EXAMPLE_USER"); 59 ////mQQueueManagerJMS = cf.createContext(); 60 61 //配置MQ服务器连接参数 62 MQEnvironment.hostname = HostName; 63 MQEnvironment.port = Port; 64 MQEnvironment.channel = ChannelName; 65 //设置应用名称,方便服务器MQ 查看应用连接 66 //MQEnvironment.properties.put(CommonConstants.USERID, "MQ Test By Java"); 67 mQQueueManager = new MQQueueManager(QmName); 68 result = "队列管理器连接成功"; 69 } 70 catch (MQException ex) 71 { 72 MessageBox.Show(result + " CompCode:" + ex.getCompCode() + " Reason:" + ex.getReason() + " ERROR:" + ex.getErrorCode()); 73 } 74 catch (Exception ex) 75 { 76 MessageBox.Show(result + ex.Message); 77 } 78 } 79 else 80 { 81 result = "队列管理器连接成功"; 82 } 83 return result; 84 } 85 86 public static bool PutMessage(string queueName, string body) 87 { 88 bool result = false; 89 try 90 { 91 //if (mQQueue == null) 92 //{ 93 // //mQQueue = mQQueueManager.createQueue("queue:///" + queueName + ""); 94 // mQQueue = mQQueueManagerJMS.createQueue(queueName); 95 //} 96 //if (producer == null) 97 //{ 98 // producer = mQQueueManager.createProducer(); 99 //} 100 ////producer.send(mQQueue, body); 101 102 103 if (mQQueueSend == null) 104 { 105 //mQQueueSend = mQQueueManager.accessQueue(queueName, com.ibm.mq.constants.CMQC.MQOO_OUTPUT); 106 mQQueueSend = mQQueueManager.accessQueue(queueName, MQC.MQOO_OUTPUT); 107 } 108 MQMessage message = new MQMessage(); 109 message.writeUTF(body); 110 mQQueueSend.put(message); 111 result = true; 112 } 113 catch (Exception ex) 114 { 115 TraceHelper.Instance.Error("入队消息异常", ex); 116 } 117 return result; 118 } 119 120 /// <summary> 121 /// 122 /// </summary> 123 /// <param name="queueName">队列名称</param> 124 /// <returns>-1 失败 0未存在数据 1获取到数据</returns> 125 public static int GetMessage(string queueName) 126 { 127 int result = 0; 128 try 129 { 130 //if (mQQueue == null) 131 //{ 132 // //mQQueue = mQQueueManager.createQueue("queue:///" + queueName + ""); 133 // mQQueue = mQQueueManagerJMS.createQueue(queueName); 134 //} 135 //if (consumer == null) 136 //{ 137 // consumer = mQQueueManagerJMS.createConsumer(mQQueue); 138 //} 139 //var message = consumer.receive(); 140 //var body = message?.getBody(typeof(String)) as string; 141 142 if (mQQueueRecv == null) 143 { 144 //mQQueueRecv = mQQueueManager.accessQueue(queueName, com.ibm.mq.constants.CMQC.MQOO_INPUT_AS_Q_DEF 145 // | com.ibm.mq.constants.CMQC.MQOO_FAIL_IF_QUIESCING 146 // | com.ibm.mq.constants.CMQC.MQOO_INQUIRE); 147 mQQueueRecv = mQQueueManager.accessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_INQUIRE); 148 } 149 var currentDepth = mQQueueRecv.getCurrentDepth(); 150 if (currentDepth > 0) 151 { 152 MQMessage message = new MQMessage(); 153 mQQueueRecv.get(message); 154 result = 1; 155 //if (string.IsNullOrEmpty(message.ReadString(message.MessageLength)) == false) 156 //{ 157 // result = 1; 158 //} 159 } 160 } 161 catch (Exception ex) 162 { 163 result = -1; 164 TraceHelper.Instance.Error("出队消息异常", ex); 165 } 166 return result; 167 } 168 169 public static int GetCurrentDepth(string queueName) 170 { 171 int result = -1; 172 try 173 { 174 if (mQQueueDepth == null && mQQueueManager != null) 175 { 176 mQQueueDepth = mQQueueManager.accessQueue(queueName, MQC.MQOO_INQUIRE); 177 } 178 if (mQQueueDepth != null) 179 { 180 result = mQQueueDepth.getCurrentDepth(); 181 } 182 } 183 catch (Exception ex) 184 { 185 TraceHelper.Instance.Error("获取深度消息异常", ex); 186 } 187 return result; 188 } 189 190 } 191 }
主要使用IbmMqClient
本次上传的源码文件包含com.ibm.mq.allclient-9.0.4.0.dll和amqmdnet.dll两个版本。
在使用com.ibm.mq.allclient-9.0.4.0.dll(源https://github.com/mustaddon/IbmMqClient)开发过程中主要参考https://blog.csdn.net/MePlusPlus/article/details/52847580该博主代码。
因为cnblogs附件上传10M限制,所以源码上传到百度网盘进行下载
源码下载地址:链接:https://pan.baidu.com/s/1hTPVDF5sp7nfsui6x0ROOQ 提取码:mxjc
远程连接我遇到三个种错误分享一下
常见问题Reason:2035-MCA
配置MCA(MUSR_MQADMIN)开启远程访问权限,如下图
常见问题Reason:2035-用户过滤
常见问题权限过滤
- 首先右键点击队列管理器名称(QM_AAA)。
- 然后在扩展中设置安全策略为不受支持。
- 最后把连接认证信息清空,点击应用后在点击确认就可以了。