2017-07-07 57 views
1

我正在將多實例隊列管理器的單個實例更改爲多實例隊列管理器,因此無法爲多實例隊列管理器定義多個主機名。 現有主機在web.config中定義從多實例隊列管理器獲取活動QM實例並連接

<QueueConfigurationSection> 
    <QueueConfiguration> 
     <add name="SomeQueueHandler" queueManager="QM1" host="99.99.99.01" port="12345" requestQueue="A.B.REQUEST" service="FLATFILE" responseQueue="B.A.RESPONSE" internalResponseQueue="B.A.INTERNAL" channel="A.SVC.SVRCONN" binding="SOAP11TcpBinding" endPoint="net.tcp://localhost:808/Bus/SomeServiceBus.svc/SOAP11" /> 
    </QueueConfiguration> 
    </QueueConfigurationSection> 

連接在這裏被定義

public List<QueueHandler> Queues 
{ 
    get 
    { 
     if (_queues == null) 
      _queues = new List<QueueHandler>(); 
     if (_queues.Count == 0 && _queueConfiguration != null) 
     { 
      //create queue handlers from configuration provided 
      foreach (QueueConfigurationElement element in _queueConfiguration) 
      { 
       // Using a different connection factory for each queue 
       XMSFactoryFactory factory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ); 
       IConnectionFactory connectionProperties = factory.CreateConnectionFactory(); 
       connectionProperties.SetStringProperty(XMSC.WMQ_HOST_NAME, element.Host); 
       connectionProperties.SetIntProperty(XMSC.WMQ_PORT, element.Port); 
       connectionProperties.SetStringProperty(XMSC.WMQ_CHANNEL, element.Channel); 
       connectionProperties.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT); 
       connectionProperties.SetIntProperty(XMSC.WMQ_BROKER_VERSION, XMSC.WMQ_BROKER_V1); 
       connectionProperties.SetBooleanProperty(XMSC.WMQ_USE_CONNECTION_POOLING, true); 

       var queue = new QueueHandler(element.Name, connectionProperties); 
       _queues.Add(queue); 
      } 
     } 
     return new List<QueueHandler>(_queues); 
    } 
} 

QueueHandler:

public QueueHandler(string handlerName, IConnectionFactory mqConnectionFactory) 
{ 
    _connectionProperties = mqConnectionFactory; 
    var queueConfigurationSection = ConfigurationManager.GetSection(QueueConfigurationSection.SectionName) as QueueConfigurationSection; 
    if (queueConfigurationSection != null) 
    { 
     if (queueConfigurationSection.QueueConfigurationCollection.Cast<QueueConfigurationElement>().Any(qc => qc.Name == handlerName)) 
     { 
      var element = queueConfigurationSection.QueueConfigurationCollection.Cast<QueueConfigurationElement>().First(qc => qc.Name == handlerName); 

      _name = element.Name; 
      _serviceType = element.DestinationService; 
      _queueManagerName = element.QueueManager; 
      _channel = element.Channel; 
      _requestQueueName = element.RequestQueue; 
      _responseQueueName = element.ResponseQueue; 
      _internalResponseQueueName = element.InternalResponseQueue; 
      _port = element.Port; 
      _host = element.Host; 

      //set up binding configuraion 
      EndpointType bindingEnum; 
      if (System.Enum.TryParse(element.Binding, out bindingEnum)) 
      { 
       _messageType = bindingEnum; 

       switch (bindingEnum) 
       { 
        case EndpointType.FlatFileTcpBinding: 
         //message received from the request queue is plain text - by configuration 
         _dvsBinding = EndpointHelper.CreateFlatFileTCPBinding(); 
         break; 
        // ... 
        default: 
         //unsupported endpoint configuration 
         throw new Exception("Unsupported binding configuration"); 
       } 
      } 

      //create endpoint address 
      _endPointAddress = new EndpointAddress(element.EndPoint); 
     } 
    } 
} 

和主機名和端口還定義在相同的類中SendNewMessage方法...

try 
     { 
      if (port != 0) 
       MQEnvironment.Port = port; 
      if (host != ".") 
       MQEnvironment.Hostname = host; 
      if (channel != ".") 
       MQEnvironment.Channel = channel; 
      hMgr = new MQQueueManager(manager); 
     } 

那麼如何在MQEnvironment.Hostname中設置備用主機?

回答

0

有多種方式可以爲MQ提供多個連接到的主機名和端口號。我在下面的建議指定類似於你已經指定主機和端口的設置。


爲了您QueueHandler這是使用XMS您將要替換的屬性XMSC.WMQ_HOST_NAMEXMSC.WMQ_PORT與下面的三個屬性。下面的例子假設你已經在你的web.config中定義主機1,端口1,主機2,端口2:

connectionProperties.SetIntProperty(XMSC.WMQ_CLIENT_RECONNECT_OPTIONS, XMSC.WMQ_CLIENT_RECONNECT_Q_MGR); 
connectionProperties.SetStringProperty(XMSC.WMQ_CONNECTION_NAME_LIST, String.Format("{0}({1}),{2}({3})", element.Host1, element.Port1, element.Host2, element.Port2)); 
connectionProperties.SetIntProperty(XMSC.WMQ_CLIENT_RECONNECT_TIMEOUT, XMSC.WMQ_CLIENT_RECONNECT_TIMEOUT_DEFAULT); 

鏈接到IBM MQ知識中心網頁這些屬性:

在噸他IBM安裝目錄中,您可以查看以下XMS示例程序:

tools\dotnet\samples\cs\xms\simple\wmq\SimpleClientAutoReconnect\SimpleClientAutoReconnect.cs 

對於那些使用IBM MQ C#庫,你將與性質和變化的Hashtable替換您MQEnvironment設置寫入你的SendNewMessage方法您撥打MQQueueManager的方式通過Hashtable。這具有線程安全的附加好處,其中MQEnvironment不是。下面的例子假設你已經在你的web.config中定義主機1,端口1,主機2,端口2:

properties = new Hashtable(); 
properties.Add(MQC.CONNECTION_NAME_PROPERTY, String.Format("{0}({1}),{2}({3})", host1, port1, host2, port2)); 
properties.Add(MQC.CHANNEL_PROPERTY, channel); 
properties.Add(MQC.CONNECT_OPTIONS_PROPERTY, MQC.MQCNO_RECONNECT_Q_MGR); 
hMgr = new MQQueueManager(manager, properties); 

的IBM MQ Knowldege中心頁面「MQQueueManager .NET class」對屬性的更多信息。

根據IBM安裝目錄中,您可以查看下面的C#示例程序:?當有人回答我的問題,我應該怎麼辦]

tools\dotnet\samples\cs\base\SimpleClientAutoReconnectPut\SimpleClientAutoReconnectPut.cs 
+0

請參閱(https://stackoverflow.com/help/someone -answers)。如果您喜歡它,請接受它並/或將它投票。很高興有幫助。 – JoshMc

+0

仍然無法正常工作...我使用連接屬性創建如下連接: connectionWMQ = cf.CreateConnection(); sessionWMQ = connectionWMQ.CreateSession(false,AcknowledgeMode.AutoAcknowledge); destination = sessionWMQ.CreateQueue(destinationName); producer = sessionWMQ.CreateProducer(destination);它引發異常: ...下一條評論 – Xstaci

+0

XMS試圖執行MQPUT或MQPUT1;但是WebSphere MQ報告了一個錯誤。使用鏈接的異常來確定此錯誤的原因。堆棧跟蹤:在IBM.XMS.Client.WMQ.ProducerShadow.CheckNmqiCallSuccess(字符串messageid,WmqDestination目的地,字符串probeid,Int32 cc,Int32 rc) – Xstaci