我需要用 C# 编写一个基于 Windows 的服务来监听 WebSphere MQ 中配置的请求队列。我成功地将请求放入 MQ 并获得了相关 ID,但作为响应,我没有得到实际结果。
我想开发一个系统,每当有新消息到达队列时,服务应该接收消息并处理请求。我可以使用我的相关 ID 映射该响应。
下面的代码解决了我的问题。
class Program
{
static void Main(string[] args)
{
Program app = new Program();
app.Setup();
Console.ReadLine();
}
public void Setup()
{
XMSFactoryFactory xff = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ);
IConnectionFactory cf = xff.CreateConnectionFactory();
cf.SetStringProperty(XMSC.WMQ_HOST_NAME, "10.87.188.156(7111)");
cf.SetIntProperty(XMSC.WMQ_PORT, 7111);
cf.SetStringProperty(XMSC.WMQ_CHANNEL, "QMEIGS1.CRM.SVRCONN");
cf.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT);
cf.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, "QMEIGS1");
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("DOX.APYMT.ESB.SSK.RPO.02");
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);
}
}
我正在连接到 Z/OS 大型机上的 MQ。Rashmi 的代码示例对我有用,但我需要更改“OnMessage”方法,如下所示来提取消息:
private void OnMessage(IMessage msg)
{
IBytesMessage bytesMessage = (IBytesMessage)msg;
var buffer = new byte[bytesMessage.BodyLength];
bytesMessage.ReadBytes(buffer, (int)bytesMessage.BodyLength);
var messageAsText = Encoding.Unicode.GetString(buffer);
Console.Write("Got a message: ");
Console.WriteLine(messageAsText);
}