gpt4 book ai didi

java - 不带消息头的 MQMessage

转载 作者:行者123 更新时间:2023-12-02 04:19:18 30 4
gpt4 key购买 nike

我得到了一个很棒的 ConsumerClass ,但 WebsphereMQClasses 却遇到了一个问题。它没有提供消息,而是显示 _Whole_thing (JMSMessage 类:jms_text 等) 所以我想知道如何在没有标题的情况下获取消息。我发现了这个:

((MQDestination)destination).setMessageBodyStyle
(WMQConstants.WMQ_MESSAGE_BODY_MQ);

但它根本不起作用这是我的类(class):

package mq.pack;

import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.ibm.jms.JMSMessage;
import com.ibm.jms.JMSTextMessage;
import com.ibm.mq.MQC;
import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQConnection;
import com.ibm.mq.jms.MQConnectionFactory;
import com.ibm.mq.jms.MQMessageConsumer;
import com.ibm.mq.jms.MQSession;
import com.ibm.msg.client.wmq.WMQConstants;

public class Consumer {
private MQConnection cnct;
private MQSession session;
private MQMessageConsumer cons;

@SuppressWarnings("deprecation")
public void run() throws JMSException, NamingException, Throwable {
try {

MQConnectionFactory factory = new MQConnectionFactory();
factory.setHostName("localhost");
factory.setPort(1414);
factory.setQueueManager("QM01");
factory.setChannel("SYSTEM.DEF.SVRCONN");
factory.setTransportType(WMQConstants.WMQ_CLIENT_NONJMS_MQ);
cnct = (MQConnection) factory.createConnection();
session = (MQSession) cnct.createSession(false, Session.AUTO_ACKNOWLEDGE);
MQDestination queue = (MQDestination) session.createQueue("QUEUE1");
cnct.start();
cons = (MQMessageConsumer) session.createConsumer(queue);
JMSTextMessage msg = (JMSTextMessage) cons.receive(1000);


if (msg instanceof TextMessage){
System.out.println("Message:"+msg);
}
else {
//String mes= msg.toString();
System.out.println("No Textmessage");
}
} catch (Throwable e) {
System.out.println(e);
}
}
public static void main(String[]args) throws JMSException, NamingException, Throwable{

Consumer send = new Consumer();
send.run();

}
}

最佳答案

更改:

System.out.println("Message:"+msg);

至:

System.out.println("Message:"+msg.getText());

关于java - 不带消息头的 MQMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32969432/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com