4
votes

I need to write a Windows based service in C# to listen to the request queue configured in WebSphere MQ. I successfully put the request in MQ and get a correlation ID, but in response, I'm not getting the actual result.

I want to develop a system that whenever a new message arrives in the queue, the service should pick up the message and process the request. I can map that response with my correlation ID.

2
here is a tutorial for basic functionality c-sharpcorner.com/UploadFile/pk_khuman/… - Ozgur Dogus
Can you post some sample code that isn't working for you? What have you tried already? - Mike Atlas
Mike thanks, but I m not getting any error the things are running fine actually I am calling asynchronous call to MQ but some time I am getting the response and some time not as its take time to processing, I am looking for a code by which i can get the response whenever the queue process the request with the correlation Id. please let me know if you need any code. - Rashmi Kant Shrivastwa
Anything here help? C:\Program Files (x86)\IBM\WebSphere MQ\tools\dotnet\samples\cs - T.Rob

2 Answers

6
votes

The following code has solved my problem.

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);
    }

}
1
votes

I'm connecting to MQ on Z/OS Mainframe. The code example from Rashmi did the trick for me, but I needed to change the "OnMessage" method as follows to extract the message:

    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);
    }