Send and Receive XML formatted text to IBM WebSphe

2019-09-17 05:21发布

问题:

We have IBM WebSphere MQ 7.5. We have setup Queue Manager and Queues. We want to read and write XML formatted text from the queues using XMS for .net client application. I tried below code but I don't know how to read that data.

Code:

try
         {
                var factoryfactory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ);
                var connectionfactory = factoryfactory.CreateConnectionFactory();
                connectionfactory.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, queueManager);
                connectionfactory.SetStringProperty(XMSC.WMQ_HOST_NAME, hostIP);
                connectionfactory.SetIntProperty(XMSC.WMQ_PORT, port);
                connectionfactory.SetStringProperty(XMSC.WMQ_CHANNEL, channel);
                connectionfactory.SetIntProperty(XMSC.WMQ_BROKER_VERSION, XMSC.WMQ_BROKER_V2);
                connectionfactory.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT_UNMANAGED);
                using (IConnection conn = connectionfactory.CreateConnection())
                {
                    using (ISession session = conn.CreateSession(false, AcknowledgeMode.AutoAcknowledge))
                    {
                        using (IDestination dest = session.CreateQueue(string.Format("queue://{0}", queueName))) // Create a queue
                        {
                            using (IMessageConsumer consumer = session.CreateConsumer(dest))
                            {
                                conn.Start();
                                IMessage recvMsg = null;
                                recvMsg = consumer.Receive();
                                if (recvMsg != null)
                                    return recvMsg;

                            }
                        }
                    }
                }
            }
            catch(XMSException ex)
            {

            }

recvMsg is returning data in JMS. Any working code to read data for string/XML?

The response is coming like this:

JMSMessage class: jms_bytes
  JMSType:          
  JMSDeliveryMode:  NonPersistent
  JMSExpiration:    0
  JMSPriority:      0
  JMSMessageID:     ID:414d512043524d44514d312020202020118cff552000dd10
  JMSTimestamp:     1442899405610
  JMSCorrelationID: 
  JMSDestination:   
  JMSReplyTo:       
  JMSRedelivered:   False
    JMS_IBM_Character_Set: 850
    JMS_IBM_Encoding: 546
    JMS_IBM_Format:         
    JMS_IBM_MsgType: 8
    JMS_IBM_PutApplType: 11
    JMS_IBM_PutDate: 20150922
    JMS_IBM_PutTime: 05232561
    JMSXAppID: staller\MQ\Tools\rfhutil.exe
    JMSXDeliveryCount: 1
    JMSXUserID:       
3c3f786d6c2076657273696f6e3d22312e302220656e636f64696e673d225554462d38223f3e0d0a
3c212d2d53616d706c6520584d4c2066696c652067656e65726174656420627920584d4c53707920
76323031322028687474703a2f2f7777772e616c746f76612e636f6d292d2d3e0d0a3c6d74613a4d
5441546f45534254726176656c486973746f7279526571756573744d657373616765207873693a73
6368656d614c6f636174696f6e3d22687474703a2f2f6d74612e626f726465722e676f762e61752f
6d74612f76312e30204d5441546f4553424d657373616765732e7873642220786d6c6e733a636f6d
6d6f6e3d22687474703a2f2f6573622e626f726465722e676f762e61752f636f6d6d6f6e2f76312e
302220786d6c6e733a74683d22687474703a2f2f6d74612e626f726465722e676f762e61752f6d74
612f74726176656c686973746f72792f76312e302220786d6c6e733a6d74613d22687474703a2f2f
6d74612e626f726465722e676f762e61752f6d74612f76312e302220786d6c6e733a7873693d2268
...

回答1:

Your application is getting the message as jms_bytes type message. As XML is basically text you can use ITextMessage message type for sending and receiving XML payload. Change your producer application to send messages of type ITextMessage and change your application to receive the same message type. Sample snippet is here.

Producer:

   IMessageProducer producer;
   ITextMessage textMessage;

   // create session, producer
   ....

   textMessage = sessionWMQ.CreateTextMessage();
   textMessage.Text = XmlPayload;
   producer.Send(textMessage);

Consumer:

IMessageConsumer consumer;
ITextMessage textMessage;

// create session and consumer

....
// Receive text message
textMessage = (ITextMessage)consumer.Receive(3000); // Wait for 3 seconds to receive message
String XmlPayload = textMessage.Text;