How a message with french symbols can be decoded? Example of message in MQ:
"name":" ĄĆĘŁŃÓŚŹŻąćęłńóśźżƵƶÁáÂâÃãÀàÇçÉéÊêÍíÓóÔôÕõÚúÀÉÈÍÏÓÒÚÜÇ",
"id":"2463"
When I receive a message from MQ there is:
"name":" ",
"id":"2463"
The code I use:
public static String getMessage() throws JMSException {
String message = null;
try {
MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options = MQGMO_ALL_MSGS_AVAILABLE | MQGMO_WAIT | MQGMO_PROPERTIES_AS_Q_DEF | MQGMO_FAIL_IF_QUIESCING | MQOO_INPUT_AS_Q_DEF | MQGMO_SYNCPOINT
| MQC.MQGMO_CONVERT;;
cf.setHostName(HOST);
cf.setChannel(CHANNEL);
cf.setPort(PORT);
cf.setQueueManager(QMN);
cf.setTransportType(WMQConstants.WMQ_CM_CLIENT);
MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();
MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
MQQueue queue = (MQQueue) session.createQueue(QUEUE_NAME);
MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue);
connection.start();
TextMessage receivedMessage = (TextMessage) receiver.receive();
byte[] bytes = receivedMessage.getText().getBytes("UTF-8");
String s = new String(bytes, "UTF-8");
If the message is an instance of a TextMessage
then its going to contain a UTF-16 conformant string and not a byte array.
You should, however, add in some guard logic to make sure:
Message receivedMessage = receiver.receive();
if (receivedMessage instanceof TextMessage) {
TextMessage textMessage = (TextMessage) receivedMessage;
try {
String text = textMessage.getText();
...
} catch (JMSException jmsex) {
...
}
} else {
// Not a TextMessage, maybe its a JMSBytesMessage, let's check
...
}
This is of course assuming that your producer app is generating text messages. So if it is a JMS app it should look something like:
Destination destination = context.createQueue("queue:///" + QUEUE_NAME);
JMSProducer producer = context.createProducer();
logger.info("producer created");
TextMessage message = context.createTextMessage("
L'hôpital est près de l'hôtel");
producer.send(destination, message);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.