我是WMQ的新手。我正在尝试使用jms api从eclipse IDE向IBM MQ编写消息,但即使我将WMQ_TARGET_CLIENT设置为WMQConstants.WMQ_TARGET_DEST_DEFAULT',我仍然会收到以下错误。我现在坚持这个。任何帮助将不胜感激。
的ErrorMessage:
Wrong MQ message attribute 'format': ' ' or 'MQHRF2 ' expected but was 'MQSTR '
package com.java.queuebrowser;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import com.ibm.mq.constants.MQConstants;
import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;
public class JMS_Producer {
private static String host = hostname;
private static int port = 1414;
private static String channel = "SYSTEM.ADMIN.SVRCONN";
private static String user = null;
private static String password = null;
private static String queueManagerName = QMName;
private static String destinationName = QName;
private static boolean isTopic = false;
private static boolean clientTransport = false;
private static String filePath = null;
// System exit status value (assume unset value to be 1)
private static int status = 1;
public static void main(String[] args) throws JMSException {
// TODO Auto-generated method stub
Connection connection = null;
Session session = null;
Destination destination = null;
MessageProducer producer = null;
try {
// Create a connection factory
JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
JmsConnectionFactory cf = ff.createConnectionFactory();
// Set the properties
cf.setBooleanProperty(WMQConstants.WMQ_MQMD_WRITE_ENABLED, true);
cf.setStringProperty(WMQConstants.WMQ_MQMD_MESSAGE_CONTEXT,"1");
cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, host);
cf.setIntProperty(WMQConstants.WMQ_PORT, port);
cf.setStringProperty(WMQConstants.WMQ_CHANNEL, channel);
if (clientTransport) {
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_BINDINGS);
}
else {
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
}
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, queueManagerName);
if (user != null) {
cf.setStringProperty(WMQConstants.USERID, user);
cf.setStringProperty(WMQConstants.PASSWORD, password);
cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
}
cf.setIntProperty(WMQConstants.WMQ_TARGET_CLIENT, WMQConstants.WMQ_TARGET_DEST_DEFAULT);
// Create JMS objects
connection = cf.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
if (isTopic) {
destination = session.createTopic(destinationName);
}
else {
destination = session.createQueue("queue://" + queueManagerName + "/" + destinationName + "?persistence=2&CCSID=1208&targetClient=0");
}
producer = session.createProducer(destination);
// Start the connection
connection.start();
String line;
filePath = "C:\\Users\\rraghuna\\Desktop\\test.xml";
FileInputStream fi = new FileInputStream(filePath);
BufferedReader in = new BufferedReader(new InputStreamReader(fi, "UTF-8"));
StringBuilder s = new StringBuilder();
do {
line = in.readLine();
if (line!=null){
if(line.trim().length()==0){
break;
}
s = s.append(line);
}
} while (line != null);
System.out.println("Sent message:\n" + s);
TextMessage message = session.createTextMessage("test");
cf.setStringProperty(WMQConstants.WMQ_MQMD_MESSAGE_CONTEXT,"1");
message.setIntProperty(WMQConstants.WMQ_MESSAGE_BODY, WMQConstants.WMQ_MESSAGE_BODY_JMS);
message.setIntProperty(WMQConstants.WMQ_TARGET_CLIENT, WMQConstants.WMQ_TARGET_DEST_DEFAULT);
message.setBooleanProperty(WMQConstants.WMQ_MQMD_WRITE_ENABLED, true);
message.setIntProperty(WMQConstants.WMQ_MQMD_MESSAGE_CONTEXT,WMQConstants.WMQ_MDCTX_SET_IDENTITY_CONTEXT);
message.setStringProperty("WMQ_MESSAGE_BODY", "WMQ_MESSAGE_BODY_MQ");
// And, send the message
producer.send(message);
fi.close();
recordSuccess();
}
catch (JMSException | IOException jmsex) {
recordFailure(jmsex);
}
finally {
if (producer != null) {
try {
producer.close();
}
catch (JMSException jmsex) {
System.out.println("Producer could not be closed.");
recordFailure(jmsex);
}
}
if (session != null) {
try {
session.close();
}
catch (JMSException jmsex) {
System.out.println("Session could not be closed.");
recordFailure(jmsex);
}
}
if (connection != null) {
try {
connection.close();
}
catch (JMSException jmsex) {
System.out.println("Connection could not be closed.");
recordFailure(jmsex);
}
}
}
System.exit(status);
return;
} // end main()
/**
* Process a JMSException and any associated inner exceptions.
*
* @param jmsex
*/
private static void processJMSException(JMSException jmsex) {
System.out.println(jmsex);
Throwable innerException = jmsex.getLinkedException();
if (innerException != null) {
System.out.println("Inner exception(s):");
}
while (innerException != null) {
System.out.println(innerException);
innerException = innerException.getCause();
}
return;
}
/**
* Record this run as successful.
*/
private static void recordSuccess() {
System.out.println("SUCCESS");
status = 0;
return;
}
/**
* Record this run as failure.
*
* @param ex
*/
private static void recordFailure(Exception ex) {
if (ex != null) {
if (ex instanceof JMSException) {
processJMSException((JMSException) ex);
}
else {
System.out.println(ex);
}
}
System.out.println("FAILURE");
status = -1;
return;
}
/**
* Display usage help.
*/
private static void printUsage() {
System.out.println("\nUsage:");
System.out
.println("JmsProducer -m queueManagerName -d destinationName [-h host -p port -l channel] [-u userid -w passWord]");
return;
}
} // end class
答案 0 :(得分:0)
我猜这个错误不是来自你的JMS应用程序,而是来自消耗消息的应用程序,这似乎是一个本机MQ应用程序。
如果是这种情况,则需要在目标地址中使用targetClient = 1。
据我所知,WMQ_TARGET_CLIENT属性仅用于目的地,因此您不应在连接工厂或消息上设置它。