Connecting to IBM MQ (with SSL)
Sufyan S Jabr - 19 May 2020
Sufyan S Jabr - 19 May 2020
[SHOWTOGROUPS=4,20]
All items needed to successfully connect to IBM MQ
Connecting to IBM MQ is really an annoying task, whether for the configuration side or programming side.
In this post, I will go through all the items needed to successfully connect to IBM MQ.
Introduction
Supporting IBM MQ in your code is not an easy task. I can say the main reason for that is the lack of samples, very complicated configurations, lack of proper and easy documentation and last but not the least, a very bad user experience by IBM itself.
Background
To be able to understand this tip, you need the following:
Configuration - Normal Mode
Before we start our code, we need to start the proper configuration of our IBM MQ, the best and easiest way is through command. There are many ways to configure it, so if you prefer another way, that's Ok, as long as you know it’s working fine. Also, many of the below commands might not seem necessary.
*** Please note that the below configurations are not suitable for production servers. ***
Before we start with IBM MQ configuration, let's create a new windows user and call it "MQUser". This user must be a member of the group "mqm". This group will be available after installing IBM MQ.
Create a New Queue Manager with name QM:
Start Queue Manager:
Start MQSC to execute commands for our Queue Manager:
Create a new Local Queue with name (Queue1):
Disable CHLAUTH rules:
Create a new Channel with name CHANNEL1 and set the value of MCAUSER to our user MQUser:
Create a listener:
Start our listener:
Last command, close command:
Quick Connect Test
Using the below, you can easily connect to IBM MQ, noting the following:
[/SHOWTOGROUPS]
All items needed to successfully connect to IBM MQ
Connecting to IBM MQ is really an annoying task, whether for the configuration side or programming side.
In this post, I will go through all the items needed to successfully connect to IBM MQ.
Introduction
Supporting IBM MQ in your code is not an easy task. I can say the main reason for that is the lack of samples, very complicated configurations, lack of proper and easy documentation and last but not the least, a very bad user experience by IBM itself.
Background
To be able to understand this tip, you need the following:
- Basic programming knowledge
- IBM MQ installed (information in this post has been tested on versions 7.5, 8.0 and 9.0).
- This code was tested using .NET Framework 4.7.2. but I am sure the code will work fine with 4.5 and 3.5.
- This code was tested using IBM MQ DLL's version 8.0.0.5 amqmdnet.dll, amqmdxcs.dll.
Configuration - Normal Mode
Before we start our code, we need to start the proper configuration of our IBM MQ, the best and easiest way is through command. There are many ways to configure it, so if you prefer another way, that's Ok, as long as you know it’s working fine. Also, many of the below commands might not seem necessary.
*** Please note that the below configurations are not suitable for production servers. ***
Before we start with IBM MQ configuration, let's create a new windows user and call it "MQUser". This user must be a member of the group "mqm". This group will be available after installing IBM MQ.
Create a New Queue Manager with name QM:
Код:
crtmqm QM
Start Queue Manager:
Код:
strmqm QM
Start MQSC to execute commands for our Queue Manager:
Код:
runmqsc QM
Код:
// Expected Output:
// 5724-H72 (C) Copyright IBM Corp. 1994, 2011. ALL RIGHTS RESERVED.
// Starting MQSC for queue manager QM.
Create a new Local Queue with name (Queue1):
Код:
DEFINE QLOCAL (QUEUE1)
// Expected Output:
// 1 : DEFINE QLOCAL (QUEUE1)
// AMQ8006: WebSphere MQ queue created.
Disable CHLAUTH rules:
Код:
ALTER QMGR CHLAUTH (DISABLED)
// Expected Output:
// 2 : ALTER QMGR CHLAUTH (DISABLED)
// AMQ8005: WebSphere MQ queue manager changed.
Create a new Channel with name CHANNEL1 and set the value of MCAUSER to our user MQUser:
Код:
DEFINE CHANNEL (CHANNEL1) CHLTYPE (SVRCONN) TRPTYPE (TCP) MCAUSER('MQUser')
// Expected Output:
// 3 : DEFINE CHANNEL (CHANNEL1) CHLTYPE (SVRCONN) TRPTYPE (TCP)
// AMQ8014: WebSphere MQ channel created.
Create a listener:
Код:
DEFINE LISTENER (LISTENER1) TRPTYPE (TCP) CONTROL (QMGR) PORT (1414)
// Expected Output:
// 4 : DEFINE LISTENER (LISTENER1) TRPTYPE (TCP) CONTROL (QMGR) PORT (1414)
// AMQ8626: WebSphere MQ listener created.
Start our listener:
Код:
START LISTENER (LISTENER1)
// Expected Output:
// 5 : START LISTENER (LISTENER1)
// AMQ8021: Request to start WebSphere MQ listener accepted.
Last command, close command:
Код:
end
// Expected Output:
// 6 : end
// 6 MQSC commands read.
// No commands have a syntax error.
// All valid MQSC commands were processed.
Quick Connect Test
Using the below, you can easily connect to IBM MQ, noting the following:
- UTF-8 is not a must, you can use UTF-16.
- Many optional params not mentioned (below sample) are properties of object queueMessage if needed:
- CorrelationId
- MessageId
- ReplyToQueueName
- The code will type IBM MQ error code. Для просмотра ссылки Войди
или Зарегистрируйся - Username and Password in our configuration is not needed, uncomment the username / password assignment if needed.
Код:
using IBM.WMQ;
using System;
using System.Collections;
using System.Text;
namespace MQTest
{
class Program
{
static void Main(string[] args)
{
string strQueueManagerName = "QM";
string strChannelName = "CHANNEL1";
string strQueueName = "QUEUE1";
string strServerName = "127.0.0.1";
int intPort = 1414;
string strMsg = "Hello IBM, this is a message";
Hashtable queueProperties = new Hashtable
{
{ MQC.HOST_NAME_PROPERTY, strServerName },
{ MQC.CHANNEL_PROPERTY, strChannelName },
{ MQC.PORT_PROPERTY, intPort },
{ MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED }
};
//Set Username
//MQEnvironment.UserId = "User";
//Set Passowrd
//MQEnvironment.Password = "123";
//Define a Queue Manager
try
{
MQQueueManager myQM = new MQQueueManager(strQueueManagerName, queueProperties);
//Define a Message
MQMessage queueMessage = new MQMessage();
queueMessage.Format = MQC.MQFMT_STRING;
queueMessage.CharacterSet = Encoding.UTF8.CodePage;
queueMessage.Write(Encoding.UTF8.GetBytes(strMsg));
//Define a Queue
var queue = myQM.AccessQueue
(strQueueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);
MQPutMessageOptions queuePutMessageOptions = new MQPutMessageOptions();
queue.Put(queueMessage, queuePutMessageOptions);
queue.Close();
Console.WriteLine("Success");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
[/SHOWTOGROUPS]