任何人都可以建议我在C#.net中编写消息驱动bean的代码来监听MQ并处理它。
答案 0 :(得分:1)
XMS与JMS非常相似。这是一个"你好,世界"使用XMS在C#中使用消息侦听器的示例。请在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);
}
}
}
答案 1 :(得分:0)
MDB将使用Java。我不知道是否有一个应用服务器可以处理用C#编写的MDB。
如果您的想法是使用C#应用程序异步接收WebSphere MQ消息,那么您可以使用具有Message侦听器的XMS .NET。