如何在 C# 中使用 JNDI 连接到 IBM MQ?

问题描述 投票:0回答:2

我目前可以通过直接在 C# 代码中指定连接属性,使用 IBMXMSDotnetClient 连接到 IBM MQ,如下所示。

XMSFactoryFactory factory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ);
IConnectionFactory connFactory = factory.CreateConnectionFactory();
connFactory.SetStringProperty(XMSC.WMQ_HOST_NAME, "xxx");
connFactory.SetIntProperty(XMSC.WMQ_PORT, 1414);
connFactory.SetStringProperty(XMSC.WMQ_CHANNEL, "xxx");
connFactory.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT);
connFactory.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, "xxx");

但是在Java中,似乎可以通过JNDI绑定文件来完成。

从我看来,JNDI 看起来就像 TNS 文件(它指定了连接详细信息,如主机、端口、SID 等),客户端使用它来连接到 Oracle 中的服务器。 我的理解正确吗?

如果是这种情况,是否可以使用IBMXMSDotnetClient通过JNDI绑定文件连接到IBM MQ?我能找到的所有示例都是直接设置连接属性(connFactory.SetXXXProperty)。

c# ibm-mq jndi xms
2个回答
1
投票

我有这个片段来创建初始上下文并使用它来创建连接工厂和会话。希望它能帮助您入门。

        InitialContext ic = null;
        IConnectionFactory confac = null;
        IConnection conn = null;
        ISession sess = null;
        IMessageConsumer cons = null;
        IDestination dest = null;

        try
        {
            System.Collections.Hashtable env = new System.Collections.Hashtable();
            // Set the URL or PATH where the bindings file is located
            env[XMSC.IC_URL] = "file://c:/mqbindings/.bindings";
            // Initialize the context
            ic = new InitialContext(env);
            // Lookup for the connection factory name
            confac = (IConnectionFactory)ic.Lookup("myconfactoryname");
            // Create connection using the details from connection factory
            conn = (IConnection)confac.CreateConnection();
            // Create an auto ack session
            sess = conn.CreateSession(false, AcknowledgeMode.AutoAcknowledge);
            // Lookup for the destination
            dest = (IDestination)ic.Lookup("myqueue");
            // ... rest of the code - create consumer or producer
        } 
        catch (XMSException xmsE)
        {
                // Handle exception
        }

0
投票

@Shashi / KZFid - 你知道如何使用安全集成吗:-

我正在使用这段代码,但它抛出错误 -

#region SSL Integration Code Section
string ssl_identity = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "certificate", "cacerts.p12");
string ssl_target_hostname = serverUrl;
string ssl_password = KeyConstants.config_cert_hash;

EMSSSLFileStoreInfo StoreInfo = new EMSSSLFileStoreInfo();
StoreInfo.SetSSLClientIdentity(ssl_identity);
StoreInfo.SetSSLPassword(ssl_password.ToCharArray());

hashEnvironment.Add(EMSSSL.TRACE, true);
hashEnvironment.Add(EMSSSL.CLIENT_TRACER, true);

//StoreInfo.SetSSLTrustedCertificate(ssl_identity);

//hashEnvironment.Add(LookupContext.SSL_HOST_NAME_VERIFIER, false);

hashEnvironment.Add(LookupContext.PROVIDER_URL, serverUrl);
if (userName != null)
{
    hashEnvironment.Add(LookupContext.SECURITY_PRINCIPAL, userName);
    if (password != null)
        hashEnvironment.Add(LookupContext.SECURITY_CREDENTIALS, password);
}
hashEnvironment.Add(LookupContext.SECURITY_PROTOCOL, "ssl");

hashEnvironment.Add(LookupContext.SSL_TARGET_HOST_NAME, serverUrl);
hashEnvironment.Add(LookupContext.SSL_STORE_TYPE, EMSSSLStoreType.EMSSSL_STORE_TYPE_FILE);
hashEnvironment.Add(LookupContext.SSL_STORE_INFO, StoreInfo);
#endregion




ILookupContext lcxt = new LookupContext(hashEnvironment);

QueueConnectionFactory queueFactory = null;
//if (contextLookup == "")
//    queueFactory = new TIBCO.EMS.QueueConnectionFactory(serverUrl);
//else
//    queueFactory = (QueueConnectionFactory)lcxt.Lookup(contextLookup);

queueFactory = contextLookup == string.Empty ? new TIBCO.EMS.QueueConnectionFactory(serverUrl) : (QueueConnectionFactory)lcxt.Lookup(contextLookup);

QueueConnection connection = queueFactory.CreateQueueConnection(userName, password);

这行Codce = queueFactory = contextLookup == string.Empty ?新的 TIBCO.EMS.QueueConnectionFactory(serverUrl) : (QueueConnectionFactory)lcxt.Lookup(contextLookup);

抛出错误并提示 -

TIBCO.EMS.ServiceUnavailableException:'无法查询命名服务器:无法通过 SSL 连接到 [ssl://emsas1t:27221]:无法通过 SSL 连接到 [ssl://emsas1t:27221]:系统找不到指定的路径。'

因此尝试了解这里缺少什么,或者如何使用 .Net 代码 C# 连接到 Tibco EMS 服务器。谢谢提前...!!

© www.soinside.com 2019 - 2024. All rights reserved.