监听MQTT消息在SpringBoot中变为字节数组

解决思路:使用 Message 类进行接收,再转想要的类型。

@Component
@Slf4j
public class DataSyncTestEventMsg {

	@Autowired
	public DemoService demoService;

	@JmsListener(destination= "0.system.inoutmanage.datasync.managedperson", containerFactory="topicListenerFactory")
	public void topicReceive(Message message) throws JMSException {
		//从前端的MQTT发送过来的是byte[]类型,要转换
		if (message instanceof BytesMessage){
			//强制类型转换,Message是接口或者父类,所以可以用其子类BytesMessage强制转换,ActiveMQ带有的ActiveMQBytesMessage也可以使用
			BytesMessage bytesMessage = (BytesMessage) message;
			try {
				byte[] bytes = new byte[(int) bytesMessage.getBodyLength()]; //得到byte[]数据流
				bytesMessage.readBytes(bytes); //读取byte
				String getStr = new String(bytes); //根据byte创建字符串

				System.out.println("接收到请求" + getStr);
				log.info("接收到请求" + getStr);

				JSONObject msgJsonObj = JSONObject.parseObject(getStr);
				// 处理业务
				demoService.test(msgJsonObj);
			} catch (JMSException e) {
				log.error("收到事件,解析失败", e);
			}
		}

	}

}
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot异步监听MQTT消息,可以使用Spring Integration提供的MQTT适配器和异步消息通道。下面是一个简单的示例,演示如何使用Spring Boot异步监听MQTT消息: 首先,在pom.xml添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-mqtt</artifactId> </dependency> ``` 然后,在application.properties文件配置MQTT连接参数: ``` spring.mqtt.host=your-mqtt-host spring.mqtt.username=your-mqtt-username spring.mqtt.password=your-mqtt-password ``` 接下来,定义一个异步消息通道: ```java @Configuration @EnableIntegration public class IntegrationConfig { @Bean public MessageChannel mqttInputChannel() { return new DirectChannel(); } } ``` 然后,定义一个MQTT适配器,将MQTT消息发送到异步消息通道: ```java @Configuration @EnableIntegration public class IntegrationConfig { @Bean public MessageChannel mqttInputChannel() { return new DirectChannel(); } @Bean public MqttPahoMessageDrivenChannelAdapter mqttInbound() { String clientId = MqttClient.generateClientId(); MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", clientId, "topic"); adapter.setCompletionTimeout(5000); adapter.setConverter(new DefaultPahoMessageConverter()); adapter.setQos(1); adapter.setOutputChannel(mqttInputChannel()); return adapter; } } ``` 在这个例子,我们使用了MqttPahoMessageDrivenChannelAdapter类来监听MQTT消息,并将它们发送到名为"mqttInputChannel"的异步消息通道。你需要根据你的实际需求来调整MQTT主题、客户端ID、服务器地址和端口等参数。 最后,在Spring Boot应用程序,定义一个异步消息处理器来处理MQTT消息: ```java @Component public class MqttMessageHandler { @ServiceActivator(inputChannel = "mqttInputChannel") public void handleMessage(Message<?> message) { System.out.println("Received message: " + message.getPayload()); } } ``` 这个类被标记为一个@Component,同时使用@ServiceActivator注解标记了一个方法,将它作为消息处理器的入口点。当MQTT消息到达时,Spring Integration将调用这个方法,并将消息作为参数传递给它。 总之,使用Spring Integration的MQTT适配器和异步消息通道,可以方便地实现在Spring Boot异步监听MQTT消息

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值