gpt4 book ai didi

java - IBM MQ :Message is published as Binary but sending as String

转载 作者:行者123 更新时间:2023-12-02 16:47:18 29 4
gpt4 key购买 nike

我正在尝试使用 java 向 ibmmq 发布一条简单消息。消息发送成功。但是当我在 ibm 控制台上检查队列时。消息显示为 enter image description here

但我期待的是简单的字符串。

enter image description here

这是我的代码。当我尝试转换时,我收到以下消息jms_bytes 类型的消息不能将其主体分配给 java.lang.String

import com.ibm.mq.*;
import com.ibm.mq.constants.MQConstants;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;

import javax.jms.*;
import java.io.IOException;
import java.util.Hashtable;

public class PublisherTest
{
static private String CHANNEL = "anychannel";
static private int PORT = 1414;
static private String HOST = localhost;
static private String QMANAGER = "QM1";
static private String QUEUE = "queue.test";
static private String USER = USER;
static private Hashtable<String, Object> props =
new Hashtable<String, Object>();
static MQQueueManager qMgr = null;

static private void putMsgOnQueue(String message) {
// Disabling IBM cipher suite mapping due to
// using Oracle Java and not IBM Java
System.setProperty("com.ibm.mq.cfg.useIBMCipherMappings", "false");
// Enabling SSL debug to view the communication
System.setProperty("javax.net.debug", "ssl:handshake");

props.put(MQConstants.CHANNEL_PROPERTY, CHANNEL);
props.put(MQConstants.PORT_PROPERTY, PORT);
props.put(MQConstants.HOST_NAME_PROPERTY, HOST);
props.put(MQConstants.USER_ID_PROPERTY, USER);
props.put(MQConstants.PASSWORD_PROPERTY, PASSWORD);
props.put(MQConstants.SSL_CIPHER_SUITE_PROPERTY, "TLS_RSA_WITH_AES_256_CBC_SHA256");


try {

qMgr = new MQQueueManager(QMANAGER, props);


// MQOO_OUTPUT = Open the queue to put messages
// MQOO_INPUT_AS_Q_DEF = Using queue-defined defaults
int openOptions = MQConstants.MQOO_OUTPUT;


// creating destination
MQQueue queue = qMgr.accessQueue(QUEUE, openOptions);

// specify the message options...
MQPutMessageOptions pmo = new MQPutMessageOptions(); // Default


// MQPMO_ASYNC_RESPONSE = MQPUT operation is completed without the
// application waiting for the queue manager to complete the call
// Using this option can improve messaging performance,
// particularly for applications using client bindings.
pmo.options = MQConstants.MQPMO_ASYNC_RESPONSE;

// create message
MQMessage mqMessage = new MQMessage();


System.out.println("Writing message to queue: " + QUEUE);
mqMessage.writeString(message.toString());

// Put message on queue
queue.put(mqMessage, pmo);

// Close queue
queue.close();

// Get status
MQAsyncStatus asyncStatus = qMgr.getAsyncStatus();

// Print status code (0 = successful)
System.out.println(asyncStatus.reasonCode);

} catch (MQException e) {
System.out.println("The connection to MQ could not be established." + e.getMessage());

} catch (IOException e) {
System.out.println("Error while writing message." +
e.getMessage());
} finally {
try {
qMgr.disconnect();
} catch (MQException e) {
System.out.println("The connection could not be closed." +
e.getMessage());
}
}
}


public static void main(String[] args) {

putMsgOnQueue("WELCOME");
}
}

如有任何帮助,我们将不胜感激。

最佳答案

默认的消息格式是 MQFMT_NONE。这意味着消息体由字节组成。您的代码未设置消息格式。所以我的想法是 MQ 控制台将此类消息指示为二进制。

建议您将消息格式设置为字符串并运行。这会将消息格式设置为字符串。

        MQMessage mqMessage = new MQMessage();
mqMessage.format = MQConstants.MQFMT_STRING;

关于java - IBM MQ :Message is published as Binary but sending as String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59977002/

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