how to send msg to Websphere MQ with specific reply to MQ MGR using JMS

核能气质少年 提交于 2019-12-08 04:00:40

问题


I need to build a java standalone application with ability to send messages to MQSeries specifying reply-to queue and reply-to MQMgr. I was able to use the code in SimpleP2P example to send a message, but unable to specify the reply-to queue and reply-to MQMgr

public static void main(String[] args) {
    try {
        MQQueueConnectionFactory cf = new MQQueueConnectionFactory();


        cf.setHostName("xyz.com");
        cf.setPort(141600);
        cf.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
        cf.setQueueManager("QMG");
        cf.setChannel("CHANNEL");


        MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        MQQueue queue = (MQQueue) session.createQueue("queue:///A.QUEUE");
        MQQueueSender sender = (MQQueueSender) session.createSender(queue);

        long uniqueNumber = System.currentTimeMillis() % 1000;
        String fileText = convertXMLFileToString("test.xml");
        System.out.println("fileText " + fileText);

        JMSTextMessage message = (JMSTextMessage) session.createTextMessage(fileText);


        connection.start();

        System.out.println("before Sent message:\\n" + message);

        sender.send(message);
        System.out.println("Sent message:\\n" + message);

        sender.close();

        session.close();
        connection.close();

        System.out.println("\\nSUCCESS\\n");
    } catch (JMSException jmsex) {
        System.out.println(jmsex);
        System.out.println("\\nFAILURE\\n");
    } catch (Exception ex) {
        System.out.println(ex);
        System.out.println("\\nFAILURE\\n");
    }
}

回答1:


Try this:-

MQQueue replyToQ = new MQQueue(QMgrName, ReplyQueue);
Destination replyTo = (Destination) replyToQ;
message.setJMSReplyTo(replyTo);



回答2:


public void postMessageToDatastage(String msg,String queueName,String batchID){

    int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT; 
    try {
        defaultLocalQueue = qManager.accessQueue(queueName, openOptions);            
        MQMessage putMessage = new MQMessage();
        putMessage.writeUTF(msg);            
        //specify the message options...
        MQPutMessageOptions pmo = new MQPutMessageOptions(); 
        // accept 
        // put the message on the queue
        defaultLocalQueue.put(putMessage, pmo);            
        System.out.println("Message is put on MQ.");
        //System.out.println("Queue is closing...");
         defaultLocalQueue.close();
         // for asynchornus communtication...
        //MQAsyncStatus asyncStatus = qManager.getAsyncStatus();
          qManager.disconnect();

    } catch (MQException e) {
        e.printStackTrace();
    }
}



回答3:


public void init(){

    //Set MQ connection credentials to MQ Envorinment.
     MQEnvironment.hostname = hostName;
     MQEnvironment.channel = channel;
     MQEnvironment.port = port;
     MQEnvironment.userID = user;
     MQEnvironment.password = password;
     //set transport properties.
     MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);

     try {
         //initialize MQ manager.
        qManager = new MQQueueManager(qMngrStr);
    } catch (MQException e) {
        e.printStackTrace();
    }
}


来源:https://stackoverflow.com/questions/27889503/how-to-send-msg-to-websphere-mq-with-specific-reply-to-mq-mgr-using-jms

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!