我得到了一个很棒的ConsumerClass
WebsphereMQClasses
有一个问题。而不是给出消息它显示_Whole_thing (JMSMessage class: jms_text etc)
所以我想知道如何在没有标题的情况下获取消息。我发现了这个:
((MQDestination)destination).setMessageBodyStyle
(WMQConstants.WMQ_MESSAGE_BODY_MQ);
但它不起作用(根本) 这是我的班级:
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();
}
}
答案 0 :(得分:2)
变化:
System.out.println("Message:"+msg);
为:
System.out.println("Message:"+msg.getText());