Rashmi Kant Shrivastwa
Rashmi Kant Shrivastwa

Reputation: 1157

WebSphere MQ listener in C#

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.

Upvotes: 5

Views: 9375

Answers (2)

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

Upvotes: 1

Rashmi Kant Shrivastwa
Rashmi Kant Shrivastwa

Reputation: 1157

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

}

Upvotes: 7

Related Questions