从 C# 连接到 MQ 多实例 (MQMI) 队列管理器
Connecting to an MQ multi-instance (MQMI) queue manager from C#
使用 IBM 的示例代码,我可以连接到单实例队列管理器,也可以连接到多实例队列管理器的单个实例,但无法连接到多实例队列经理。
我使用的是 ibm-mq-client 9.2 版。
当我使用单个服务器作为主机名时一切正常,但如果我将其更改为列表 ("iib-mq1.it.wd.com,iib-mq2.it.wd.com") 它会在该行失败:
MQQueueManager qMgr = new MQQueueManager(qManager, connectionProperties);
错误:
2298 MQRC_FUNCTION_NOT_SUPPORTED
我的代码:
using IBM.WMQ;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace QueueInterface
{
class MQSample
{
// The type of connection to use, this can be:-
// MQC.TRANSPORT_MQSERIES_BINDINGS for a server connection.
// MQC.TRANSPORT_MQSERIES_CLIENT for a non-XA client connection
// MQC.TRANSPORT_MQSERIES_XACLIENT for an XA client connection
// MQC.TRANSPORT_MQSERIES_MANAGED for a managed client connection
const String connectionType = MQC.TRANSPORT_MQSERIES_MANAGED;
// Define the name of the queue manager to use (applies to all connections)
const String qManager = "TST_MQMI_QM";
// Define the name of your host connection (applies to client connections only)
//const String hostName = "iib-mq1.it.wd.com";
const String hostName = "iib-mq1.it.wd.com,iib-mq2.it.wd.com";
// Define the name of the channel to use (applies to client connections only)
const String channel = "FHS.IIB.TS";
const String userName = "iib-tst-usr";
const String password = "randomPassword";
const String port = "1212";
const String queue = "TEST.ADD.FULL";
/// <summary>
/// Initialise the connection properties for the connection type requested
/// </summary>
/// <param name="connectionType">One of the MQC.TRANSPORT_MQSERIES_ values</param>
static Hashtable init(String connectionType)
{
Hashtable connectionProperties = new Hashtable();
// Add the connection type
connectionProperties.Add(MQC.TRANSPORT_PROPERTY, connectionType);
// Set up the rest of the connection properties, based on the
// connection type requested
switch (connectionType)
{
case MQC.TRANSPORT_MQSERIES_BINDINGS:
break;
case MQC.TRANSPORT_MQSERIES_CLIENT:
case MQC.TRANSPORT_MQSERIES_XACLIENT:
case MQC.TRANSPORT_MQSERIES_MANAGED:
connectionProperties.Add(MQC.HOST_NAME_PROPERTY, hostName);
connectionProperties.Add(MQC.PORT_PROPERTY, port);
connectionProperties.Add(MQC.CHANNEL_PROPERTY, channel);
connectionProperties.Add(MQC.USER_ID_PROPERTY, userName);
connectionProperties.Add(MQC.PASSWORD_PROPERTY, password);
connectionProperties.Add(MQC.USE_MQCSP_AUTHENTICATION_PROPERTY, true);
break;
}
return connectionProperties;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args)
{
try
{
Hashtable connectionProperties = init(connectionType);
// Create a connection to the queue manager using the connection
// properties just defined
MQQueueManager qMgr = new MQQueueManager(qManager, connectionProperties);
// Set up the options on the queue we want to open
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
// Now specify the queue that we want to open,and the open options
MQQueue system_default_local_queue = qMgr.AccessQueue(queue, openOptions);
// Define an IBM MQ message, writing some text in UTF format
MQMessage hello_world = new MQMessage();
hello_world.WriteUTF("Sample Message");
// Specify the message options
MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults,
// same as MQPMO_DEFAULT
// Put the message on the queue
system_default_local_queue.Put(hello_world, pmo);
// Close the queue
system_default_local_queue.Close();
// Disconnect from the queue manager
qMgr.Disconnect();
}
//If an error has occurred,try to identify what went wrong.
//Was it an IBM MQ error?
catch (MQException ex)
{
Console.WriteLine("An IBM MQ error occurred: {0}", ex.ToString());
}
catch (System.Exception ex)
{
Console.WriteLine("A System error occurred: {0}", ex.ToString());
}
return 0;
}//end of start
}//end of sample
}
您无需指定主机和端口,而是需要指定连接名称列表。在下面的示例中,我删除了变量以保持简单。
变化:
connectionProperties.Add(MQC.HOST_NAME_PROPERTY, "iib-mq1.it.wd.com,iib-mq2.it.wd.com");
connectionProperties.Add(MQC.PORT_PROPERTY, 1212);
收件人:
connectionProperties.Add(MQC.CONNECTION_NAME_PROPERTY,"iib-mq1.it.wd.com(1212),iib-mq2.it.wd.com(1212)")
使用 IBM 的示例代码,我可以连接到单实例队列管理器,也可以连接到多实例队列管理器的单个实例,但无法连接到多实例队列经理。
我使用的是 ibm-mq-client 9.2 版。
当我使用单个服务器作为主机名时一切正常,但如果我将其更改为列表 ("iib-mq1.it.wd.com,iib-mq2.it.wd.com") 它会在该行失败:
MQQueueManager qMgr = new MQQueueManager(qManager, connectionProperties);
错误:
2298 MQRC_FUNCTION_NOT_SUPPORTED
我的代码:
using IBM.WMQ;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace QueueInterface
{
class MQSample
{
// The type of connection to use, this can be:-
// MQC.TRANSPORT_MQSERIES_BINDINGS for a server connection.
// MQC.TRANSPORT_MQSERIES_CLIENT for a non-XA client connection
// MQC.TRANSPORT_MQSERIES_XACLIENT for an XA client connection
// MQC.TRANSPORT_MQSERIES_MANAGED for a managed client connection
const String connectionType = MQC.TRANSPORT_MQSERIES_MANAGED;
// Define the name of the queue manager to use (applies to all connections)
const String qManager = "TST_MQMI_QM";
// Define the name of your host connection (applies to client connections only)
//const String hostName = "iib-mq1.it.wd.com";
const String hostName = "iib-mq1.it.wd.com,iib-mq2.it.wd.com";
// Define the name of the channel to use (applies to client connections only)
const String channel = "FHS.IIB.TS";
const String userName = "iib-tst-usr";
const String password = "randomPassword";
const String port = "1212";
const String queue = "TEST.ADD.FULL";
/// <summary>
/// Initialise the connection properties for the connection type requested
/// </summary>
/// <param name="connectionType">One of the MQC.TRANSPORT_MQSERIES_ values</param>
static Hashtable init(String connectionType)
{
Hashtable connectionProperties = new Hashtable();
// Add the connection type
connectionProperties.Add(MQC.TRANSPORT_PROPERTY, connectionType);
// Set up the rest of the connection properties, based on the
// connection type requested
switch (connectionType)
{
case MQC.TRANSPORT_MQSERIES_BINDINGS:
break;
case MQC.TRANSPORT_MQSERIES_CLIENT:
case MQC.TRANSPORT_MQSERIES_XACLIENT:
case MQC.TRANSPORT_MQSERIES_MANAGED:
connectionProperties.Add(MQC.HOST_NAME_PROPERTY, hostName);
connectionProperties.Add(MQC.PORT_PROPERTY, port);
connectionProperties.Add(MQC.CHANNEL_PROPERTY, channel);
connectionProperties.Add(MQC.USER_ID_PROPERTY, userName);
connectionProperties.Add(MQC.PASSWORD_PROPERTY, password);
connectionProperties.Add(MQC.USE_MQCSP_AUTHENTICATION_PROPERTY, true);
break;
}
return connectionProperties;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args)
{
try
{
Hashtable connectionProperties = init(connectionType);
// Create a connection to the queue manager using the connection
// properties just defined
MQQueueManager qMgr = new MQQueueManager(qManager, connectionProperties);
// Set up the options on the queue we want to open
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
// Now specify the queue that we want to open,and the open options
MQQueue system_default_local_queue = qMgr.AccessQueue(queue, openOptions);
// Define an IBM MQ message, writing some text in UTF format
MQMessage hello_world = new MQMessage();
hello_world.WriteUTF("Sample Message");
// Specify the message options
MQPutMessageOptions pmo = new MQPutMessageOptions(); // accept the defaults,
// same as MQPMO_DEFAULT
// Put the message on the queue
system_default_local_queue.Put(hello_world, pmo);
// Close the queue
system_default_local_queue.Close();
// Disconnect from the queue manager
qMgr.Disconnect();
}
//If an error has occurred,try to identify what went wrong.
//Was it an IBM MQ error?
catch (MQException ex)
{
Console.WriteLine("An IBM MQ error occurred: {0}", ex.ToString());
}
catch (System.Exception ex)
{
Console.WriteLine("A System error occurred: {0}", ex.ToString());
}
return 0;
}//end of start
}//end of sample
}
您无需指定主机和端口,而是需要指定连接名称列表。在下面的示例中,我删除了变量以保持简单。
变化:
connectionProperties.Add(MQC.HOST_NAME_PROPERTY, "iib-mq1.it.wd.com,iib-mq2.it.wd.com");
connectionProperties.Add(MQC.PORT_PROPERTY, 1212);
收件人:
connectionProperties.Add(MQC.CONNECTION_NAME_PROPERTY,"iib-mq1.it.wd.com(1212),iib-mq2.it.wd.com(1212)")