In this Article i have explained how we can connect to IBM MQ from C# and send and receive messages.I also introduced rfhUtil which can be used to view and place messages on IBM MQ remote queue.
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Ibm mq with c# sending and receiving messages
1. IBM MQ And C#-Sending And Receiving Messages
Introduction:
What is IBM MQ?
IBM MQ is messaging middleware that simplifies and accelerates the integration
of diverse applications and business data across multiple platforms. It uses
message queues to facilitate the exchange of information between applications,
systems, services and files and simplify the creation and maintenance of business
applications.
Benefits of IBM MQ
Rapid, seamless connectivity: Offers a single, robust and trusted messaging
backbone for dynamic heterogeneous environments.
Secure, reliable message delivery: Preserves message integrity and helps minimize
risk of information loss.
Lower cost of ownership: Reduces cost of integration and accelerates time to
deployment.
Connecting to IBM MQ from C#.
Note:This document targets only windows platform.
Prerequisites:
We need to install MQ client in order to connect with MQ server. I am using
MQv8 Client.
You can download the client software from this link.
If you follow normal installation path it will create folder in this path “C:Program
FilesIBMWebSphere MQ”.
Note: after installing WMQ client on windows platform, if the folder “SSL” does
not get created under {IBM MQ Installation directory}, create “SSL” folder
manually.
2. Also you need to have following connection properties.
AMQCLCHL.TAB This is also called Client channel
definition table. This binary file
contains client connection details and
it shall be used to connect to Client.
KEY.ARM This is the queue manager certificate
and it shall be used by WMQ client
while connecting to Server queue
manager.
Hostname The TCP/IP hostname of the machine
on which the WebSphere MQ server
resides.
Port Number The port to be used.
Channel The name of the channel to connect to
on the target queue manager.
MQ Queue Name Name of the Queue
Queue Manager Name The name of the queue manager to
which to connect.
ClientID (UserID) The ID used to identify the WebSphere
MQ client
Password The password used to verify the
identity of the WebSphere MQ Client.
Cipher Spec application can establish a connection
to a queue manager depends on the
CipherSpec specified at the server end
of the MQI channel and the
CipherSuite specified at the client end.
Cipher Suite The name of the Cipher Suite to be
used by SSL.
3. Setting up MQ client
To set up MQ client follow the below steps.
1. Copy AMQCLCHL.TAB and Key.arm file to the following path:
{Path of IBM MQ Installation directory}/SSL
2. Set the below values as “System Variables”
Variable name Value
MQCHLLIB {Path of IBM MQ Installation
directory}/SSL
MQCHLTAB AMQCLCHL.TAB
MQSSLKEYR {Path of IBM MQ Installation
directory}/SSL
3. Run the command prompt in administrator mode. Create a key database of
type CMS using command
runmqckm -keydb -create -db "C:Program FilesIBMWebSphere MQSSL key.kdb" -
pw password -type cms -expire 365 -stash
Here the password is of your choice and please remember this password as this is
needed in future.
Note: you can find more details about the options in the command in this link-
http://www.ibm.com/support/knowledgecenter/SSFKSJ_7.5.0/com.ibm.mq.ref.adm.doc/q083860_.htm
4. 4. Add the self signed certificate to key database using the below command.
runmqckm -cert -add -db " C:Program FilesIBMWebSphere MQSSLkey.kdb" -pw
password -label ibmwebspheremq<userID> -file " C:Program FilesIBMWebSphere
MQSSLkey.arm" -format ascii
where
password is the password you choose in the Step 3 when creating key
database of type CMS.
In ibmwebspheremq<userID> replace <userID> with client id. this is used as an
identifier.
Note: Specify the absolute path of the key.arm file.
5. 5. check if the certificate is added successfully using the below command.
runmqckm -cert -list -db " C:Program FilesIBMWebSphere MQSSLkey.kdb " -pw
password
The output of above command must list the certificate
ibmwebspheremq<userID>
Now as the client is configured Let’s do coding.
Note: add reference to the dll amqmdnet.dll. you can find this dll in the MQ installation
directory (C:….)
In this sample application I am storing all the queue properties in my
app.config file.
<configuration>
<appSettings>
<add key="QueueManagerName" value=""/>
<add key="QueueName" value=""/>
<add key="ChannelName" value=""/>
<add key="TransportType" value="TCP"/>
6. <add key="HostName" value=""/>
<add key="Port" value=""/>
<add key="UserID" value=""/>
<add key="Password" value="YourChoice"/>
<add key="SSLCipherSpec" value="TLS_RSA_WITH_DES_CBC_SHA"/>
<add key="SSLKeyRepository" value="C:Program FilesIBMWebSphere MQsslkey"/>
</appSettings>
</configuration>
And I am using an Info class to fetch these details
public class MQInfo
{
public string QueueManagerName { get; set; }
public string QueueName { get; set; }
public string ChannelName { get; set; }
public string TransportType { get; set; }
public string HostName { get; set; }
public string Port { get; set; }
public string UserID { get; set; }
public string Password { get; set; }
public string SSLCipherSpec { get; set; }
public string SSLKeyRepository { get; set; }
public MQInfo()
{
QueueManagerName = ConfigurationManager.AppSettings["QueueManagerName"];
QueueName = ConfigurationManager.AppSettings["QueueName"];
ChannelName = ConfigurationManager.AppSettings["ChannelName"];
TransportType = ConfigurationManager.AppSettings["TransportType"];
HostName = ConfigurationManager.AppSettings["HostName"];
Port = ConfigurationManager.AppSettings["Port"];
UserID = ConfigurationManager.AppSettings["UserID"];
Password = ConfigurationManager.AppSettings["Password"];
SSLCipherSpec = ConfigurationManager.AppSettings["SSLCipherSpec"];
SSLKeyRepository = ConfigurationManager.AppSettings["SSLKeyRepository"];
}
}
MQHelper class will take care of reading and writing messages.
using IBM.WMQ;
namespace SampleApplication
{
7. public class MQHelper
{
MQQueueManager oQueueManager;
MQInfo oMQInfo;
void Main()
{
Console.WriteLine("1.Read Sample Messagen2.Write Sample Message");
var option = Convert.ToInt32(Console.ReadLine());
switch (option)
{
case 1: Connect();
var message = ReadSingleMessage();
Console.WriteLine(message);
break;
case 2: Connect();
message = "";
WriteMessage(message);
break;
default: Console.WriteLine("Invalid Option");
break;
}
Console.ReadLine();
}
private void Connect()
{
try
{
//get connection information
oMQInfo = new MQInfo();
//set the connection properties
MQEnvironment.Hostname = oMQInfo.HostName;
MQEnvironment.Port = Convert.ToInt32(oMQInfo.Port);
MQEnvironment.Channel = oMQInfo.ChannelName;
MQEnvironment.SSLCipherSpec = oMQInfo.SSLCipherSpec;
MQEnvironment.SSLKeyRepository = oMQInfo.SSLKeyRepository;
MQEnvironment.UserId = oMQInfo.UserID;
MQEnvironment.Password = oMQInfo.Password;
MQEnvironment.properties.Add(MQC.TRANSPORT_PROPERTY,
MQC.TRANSPORT_MQSERIES_CLIENT);
oQueueManager = new MQQueueManager(oMQInfo.QueueManagerName);
}
catch (Exception ex)
{
//Log exception
}
10. rfhutilc.exe can be used to place or view messages on the MQ remote queue.
You can download the utility from this location-
ftp://ftp.software.ibm.com/software/integration/support/supportpacs/individual
/ih03.zip
Conclusion:
In this article I have explained how we can connect to IBM MQ from C# and read
and write messages. Also I introduced rfhutilc.exe which can be used to place or
view messages on the MQ remote queue.