2012-07-12 76 views

回答

1

XMS與JMS非常相似。這是C#中使用XMS的消息監聽器的「hello,world」示例。請從您的websphere mq安裝中包含參考IBM.XMS.dll。

在我的Windows安裝,32位,這是

c:\Program Files\IBM\WebSphere MQ\bin\IBM.XMS.dll 

此示例假設幾個硬編碼的設置,並沒有錯誤處理,但我覺得你的想法。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using IBM.XMS; 

namespace XMSTest 
{ 
    class MyXmsApp 
    { 
     static void Main(string[] args) 
     { 
      MyXmsApp app = new MyXmsApp(); 
      app.Setup(); 
      Console.ReadLine(); 
     } 

     public void Setup() 
     { 
      XMSFactoryFactory xff = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ); 
      IConnectionFactory cf = xff.CreateConnectionFactory(); 
      cf.SetStringProperty(XMSC.WMQ_HOST_NAME, "localhost"); 
      cf.SetIntProperty(XMSC.WMQ_PORT, 1414); 
      cf.SetStringProperty(XMSC.WMQ_CHANNEL, "CLIENT"); 
      cf.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT); 
      cf.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, "QM_LOCAL"); 
      cf.SetIntProperty(XMSC.WMQ_BROKER_VERSION, XMSC.WMQ_BROKER_V1); 

      IConnection conn = cf.CreateConnection(); 
      Console.WriteLine("connection created"); 
      ISession sess = conn.CreateSession(false, AcknowledgeMode.AutoAcknowledge); 
      IDestination dest = sess.CreateQueue("queue://q"); 
      IMessageConsumer consumer = sess.CreateConsumer(dest); 
      MessageListener ml = new MessageListener(OnMessage); 
      consumer.MessageListener = ml; 
      conn.Start(); 
      Console.WriteLine("Consumer started"); 
     } 

     private void OnMessage(IMessage msg) 
     { 
      ITextMessage textMsg = (ITextMessage)msg; 
      Console.Write("Got a message: "); 
      Console.WriteLine(textMsg.Text); 
     } 
    } 
} 
+0

謝謝佩特,它真的幫助,但我有點困惑,它會讀取那些已經放入隊列並等待響應的消息。 – 2012-07-17 11:54:43

+0

我並不真正關注你。它會讀取所有消息。如果有消息的回覆隊列已設置,並且您知道需要以某種方式進行回覆,則必須在「OnMessage」中手動處理,因此您必須創建消息,將消息ID複製到相關ID並將其發送出去。它應該是相當直接的 – 2012-07-18 06:25:43

0

MDB將在Java中。我不知道是否有可以處理用C#編寫的MDB的應用程序服務器。

如果您的想法是使用C#應用程序異步接收WebSphere MQ消息,那麼可以使用具有Message偵聽器的XMS .NET