By connecting to MQSeries withing a .NET application, first it has to be done is to install MQ Series client at the machine which will host the application.
To do that, you can obtain for example the trial version of WebSphere MQ. During installation first the prerequisites are checked. Unfortunately if you try to install the package at Windows Vista, no wonder, it will just fail, because the operative system is currently not supported by the setup. The good thing is that this requirement just can be ignored. Clearly that means, just install all other prerequisites and proceed with installation.
If you installed the Web Sphere at the local machine or somewhere in the windows environment with active directory infrastructure, there is a group named "MQM". The windows user who is running your .NET application has to be member of this group in order to be able to connect to MQ. In that case following code can be used to establish the connection and to put one simple message in the queue:
[TestMethod] [Description("Changes the status of one single event."), Owner("ddobric")] public void MQ() { MQQueueManager queueManager = new MQQueueManager("QM_testmgr", "mychannel", "192.168.1.64"); MQQueue queue = queueManager.AccessQueue("default", MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING); MQMessage queueMessage = new MQMessage(); queueMessage.Format = MQC.MQFMT_STRING; queueMessage.MessageId = new byte[] { 0x01, 0x02, 0x03, 0x04}; queueMessage.WriteString("Hello World");
MQPutMessageOptions queuePutMessageOptions = new MQPutMessageOptions(); queue.Put(queueMessage, queuePutMessageOptions); } |
This code connects to the MQ manager "QM_testmgr" by using of channel "mychannel" hostet at the specified IP address. After executing you can see the message in the queue "default".
Assume you want now to connect to some remote machine. If you use this code the connection to the queueManager will fail with reason code 2035 = Not Authorized. To avoid this problem the MCA of the remote channel
has to be explicitly set. To do that, open the MQ Explorer go to channel properties and open the tab MCA. Then enter the name of the user who is authorized to connect. In the example bellow, I used the user "mqm".
Now the code in the .NET application has to be slightly changed as shown in the next example:
[TestMethod] [Description("Changes the status of one single event."), Owner("ddobric")] public void MQ() { Hashtable props = new Hashtable(); props.Add(MQC.HOST_NAME_PROPERTY, "sopmqseries"); props.Add(MQC.CHANNEL_PROPERTY, m_ChannelName); props.Add(MQC.USER_ID_PROPERTY, "mqm"); props.Add( MQC.PASSWORD_PROPERTY, "enter anything here." ); MQQueueManager queueManager = new MQQueueManager(m_QueueManager, props); MQQueue queue = queueManager.AccessQueue("default", MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING); MQMessage queueMessage = new MQMessage();
queueMessage.Format = MQC.MQFMT_STRING; queueMessage.MessageId = new byte[] { 0x01, 0x02, 0x03, 0x04}; queueMessage.WriteString("Hello World");
MQPutMessageOptions queuePutMessageOptions = new MQPutMessageOptions(); queue.Put(queueMessage, queuePutMessageOptions); } |
At this point is important, that specified username "mqm" has to match the name set in the MCA-tab. Additionally it is interesting, that the password does not have to match user's password, but it has to be specified as property.
If password property is not specified at all, the initialization will crash with a "null reference error".
Last but not least. If you do not want to specify the username in your code, means you would like to use the first code example, there also one a little confusing possibility. Create some other user and put in the MQM group. The name of this user should be the same as the name of user who will run the .NET application. In this case if the name of interactive user (who is running the application) matches the name of the user member of MQM group at the remote system (this does not have to be necessarily windows system) you will not need the provide credential properties (MQC.USER_ID_PROPERTY and MQC.PASSWORD_PROPERTY) and MCA name may be empty.
By troubleshooting following very useful link contains the list of all reason codes.
Posted
Apr 13 2007, 01:31 AM
by
Damir Dobric