> 文章列表 > ActiveMQ使用(四):在JavaScript中发送的MQTT消息在SpringBoot中变为字节数组

ActiveMQ使用(四):在JavaScript中发送的MQTT消息在SpringBoot中变为字节数组

ActiveMQ使用(四):在JavaScript中发送的MQTT消息在SpringBoot中变为字节数组

ActiveMQ使用(四):在JavaScript中发送的MQTT消息在SpringBoot中变为字节数组

1. 问题描述

    @JmsListener(destination = "test_producer", containerFactory = "topicListenerContainer")public void receiveTestProducer(String message) throws JMSException {System.out.println("收到测试生产者的消息: " + message);}
  1. 使用浏览器客户端发送消息:
    ActiveMQ使用(四):在JavaScript中发送的MQTT消息在SpringBoot中变为字节数组
    ActiveMQ使用(四):在JavaScript中发送的MQTT消息在SpringBoot中变为字节数组
  2. JavaScript中发送消息
    ActiveMQ使用(四):在JavaScript中发送的MQTT消息在SpringBoot中变为字节数组
    ActiveMQ使用(四):在JavaScript中发送的MQTT消息在SpringBoot中变为字节数组
    从上面的图片可以看到,JavaScript发送的消息变成了字节数据

2. 解决

  1. 思路描述:
    可以发现JavaScript发送的是BytesMessage,只要修改发送的数据为TextMessage类型就可以了,但是我没找到这种思路的解决方法
  2. 字符串解码
    通过判断数据类型,如果是TestMessage就直接取出text就是utf-8形式的字符串,如果是BytesMessage就取出字节数组,将字节数组转成utf-8字符串就可以了
	import org.apache.activemq.command.Message;@JmsListener(destination = "test_producer", containerFactory = "topicListenerContainer")public void receiveTestProducer(Message message) throws JMSException {String msg = StringUtils.activeMQMessageParse(message);System.out.println("收到测试生产者的消息: " + message);}package com.icebear.icedt.utils;import org.apache.activemq.command.ActiveMQBytesMessage;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.apache.activemq.command.Message;import javax.jms.JMSException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;/ @author icebear* @date 2023/4/13* @info 字符串工具类*/
public class StringUtils {/* 将字符串进行分割并获得字节数组* @param str 待处理字符串* @param split 分割字符串* @return 字节数组*/public static byte[] stringToBytes(String str, String split) {String[] strArr = str.split(split);byte[] byteArr = new byte[strArr.length];for (int i = 0; i < strArr.length; i++) {byteArr[i] = (byte) Integer.parseInt(strArr[i]);}return byteArr;}/* 根据字节数组获取指定字符集的字符串* @param byteArr 字节数组* @param charset 编码字符集* @return 处理后的字符串*/public static String bytesToString(byte[] byteArr, Charset charset) {return new String(byteArr, charset);}/* 将字符串根据分隔符转成字节数组,然后转成指定字符集的字符串* @param str 待处理字符串* @param split 分割字符串* @param charset 指定字符集* @return 处理后的字符串*/public static String stringToString(String str, String split, Charset charset) {return bytesToString(stringToBytes(str, split), charset);}/* 将ActiveMQ接收到的消息转换为UTF-8字符串* @param message* @return*/public static String activeMQMessageParse(Message message) {String str = null;if (message instanceof ActiveMQTextMessage) {ActiveMQTextMessage textMessage = (ActiveMQTextMessage) message;try {str = textMessage.getText().toString();} catch (JMSException e) {e.printStackTrace();}
//            System.out.println("text : " + textMessage.getText());} else if (message instanceof ActiveMQBytesMessage) {ActiveMQBytesMessage bytesMessage = (ActiveMQBytesMessage) message;byte[] byteArr = new byte[0];try {byteArr = new byte[(int) bytesMessage.getBodyLength()];int flag = bytesMessage.readBytes(byteArr);str = bytesToString(byteArr, StandardCharsets.UTF_8);
//                System.out.println("bytes : " + flag + " : " + str);} catch (JMSException e) {e.printStackTrace();}}return str;}
}

3. 测试

3.1 浏览器

ActiveMQ使用(四):在JavaScript中发送的MQTT消息在SpringBoot中变为字节数组

ActiveMQ使用(四):在JavaScript中发送的MQTT消息在SpringBoot中变为字节数组

3.2 JavaScript

ActiveMQ使用(四):在JavaScript中发送的MQTT消息在SpringBoot中变为字节数组

ActiveMQ使用(四):在JavaScript中发送的MQTT消息在SpringBoot中变为字节数组

4. 参考

如何从ActiveMqMessage获取消息文本

activemq - 接收到的 ActiveMQBytesMessage 内容为空

https://activemq.apache.org/maven/apidocs/org/apache/activemq/command/ActiveMQBytesMessage.html

byte数组与字符串之间相互转换