|
|
@@ -19,6 +19,12 @@ public class MqttMessageHandler implements MqttCallback {
|
|
|
@Autowired
|
|
|
private ApplicationContext applicationContext;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private MqttSubscribeProcessor subscribeProcessor;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MessageConverter messageConverter;
|
|
|
+
|
|
|
@Override
|
|
|
public void connectionLost(Throwable cause) {
|
|
|
log.error("Connection lost", cause);
|
|
|
@@ -26,56 +32,56 @@ public class MqttMessageHandler implements MqttCallback {
|
|
|
|
|
|
@Override
|
|
|
public void messageArrived(String topic, MqttMessage message) {
|
|
|
- log.info("Message arrived on topic: {}", topic);
|
|
|
- log.info("Message content: {}", new String(message.getPayload()));
|
|
|
+// log.info("Message arrived on topic: {}", topic);
|
|
|
+// log.debug("Message content: {}", new String(message.getPayload()));
|
|
|
|
|
|
- // 从 ApplicationContext 中获取 MqttSubscribeProcessor
|
|
|
- MqttSubscribeProcessor subscribeProcessor = applicationContext.getBean(MqttSubscribeProcessor.class);
|
|
|
Method method = subscribeProcessor.getMethodForTopic(topic);
|
|
|
-
|
|
|
- if (method != null) {
|
|
|
- try {
|
|
|
- Object bean = applicationContext.getBean(method.getDeclaringClass());
|
|
|
- log.info("Found handler bean: {} for topic: {}", bean.getClass().getName(), topic);
|
|
|
-
|
|
|
- // 获取方法参数类型
|
|
|
- Class<?>[] parameterTypes = method.getParameterTypes();
|
|
|
- Object[] args = new Object[parameterTypes.length];
|
|
|
-
|
|
|
- // 根据参数类型转换消息内容
|
|
|
- if (parameterTypes.length == 1) {
|
|
|
- if (parameterTypes[0] == String.class) {
|
|
|
- args[0] = new String(message.getPayload(), StandardCharsets.UTF_8);
|
|
|
- log.info("Converting message to String: {}", args[0]);
|
|
|
- } else {
|
|
|
- // 尝试使用消息转换器转换
|
|
|
- MessageConverter converter = applicationContext.getBean(MessageConverter.class);
|
|
|
- args[0] = converter.fromMessage(message, parameterTypes[0]);
|
|
|
- log.info("Converting message to {}: {}", parameterTypes[0].getName(), args[0]);
|
|
|
- }
|
|
|
- } else {
|
|
|
- log.error("Handler method must have exactly one parameter, but found {} parameters", parameterTypes.length);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- log.info("Invoking method: {} on bean: {} with args: {}", method.getName(), bean.getClass().getName(), args[0]);
|
|
|
- Object result = method.invoke(bean, args);
|
|
|
- log.info("Method invocation result: {}", result);
|
|
|
- log.info("Successfully processed message for topic: {}", topic);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("Error invoking method for topic: {} - Error: {}", topic, e.getMessage(), e);
|
|
|
- log.error("Stack trace:", e);
|
|
|
- }
|
|
|
- } else {
|
|
|
+ if (method == null) {
|
|
|
log.warn("No handler method found for topic: {}", topic);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ Object bean = applicationContext.getBean(method.getDeclaringClass());
|
|
|
+// log.debug("Found handler bean: {} for topic: {}", bean.getClass().getName(), topic);
|
|
|
+
|
|
|
+ // 获取方法参数类型
|
|
|
+ Class<?>[] parameterTypes = method.getParameterTypes();
|
|
|
+ if (parameterTypes.length != 1) {
|
|
|
+ log.error("Handler method must have exactly one parameter, but found {} parameters", parameterTypes.length);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Object[] args = new Object[1];
|
|
|
+ Class<?> paramType = parameterTypes[0];
|
|
|
+
|
|
|
+ // 根据参数类型转换消息内容
|
|
|
+ if (paramType == String.class) {
|
|
|
+ args[0] = new String(message.getPayload(), StandardCharsets.UTF_8);
|
|
|
+// log.debug("Converting message to String: {}", args[0]);
|
|
|
+ } else if (paramType == byte[].class) {
|
|
|
+ args[0] = message.getPayload();
|
|
|
+// log.debug("Using raw byte array as message payload");
|
|
|
+ } else {
|
|
|
+ // 使用消息转换器转换
|
|
|
+ args[0] = messageConverter.fromMessage(message, paramType);
|
|
|
+// log.debug("Converting message to {}: {}", paramType.getName(), args[0]);
|
|
|
+ }
|
|
|
+
|
|
|
+// log.debug("Invoking method: {} on bean: {} with args: {}", method.getName(), bean.getClass().getName(), args[0]);
|
|
|
+ Object result = method.invoke(bean, args);
|
|
|
+// log.debug("Method invocation result: {}", result);
|
|
|
+// log.info("Successfully processed message for topic: {}", topic);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Error invoking method for topic: {} - Error: {}", topic, e.getMessage(), e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void deliveryComplete(IMqttDeliveryToken token) {
|
|
|
- log.info("Message delivery complete for token: {}", token);
|
|
|
try {
|
|
|
- log.info("Message delivered to topics: {}", token.getTopics());
|
|
|
+// log.debug("Message delivery complete for token: {}", token);
|
|
|
+// log.debug("Message delivered to topics: {}", (Object) token.getTopics());
|
|
|
} catch (Exception e) {
|
|
|
log.error("Error getting delivery topics", e);
|
|
|
}
|