2012-07-12 77 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