package com.bonus.common.houqin.mq.rocket.mqtt;

import com.bonus.common.houqin.mq.MQListener;
import org.apache.rocketmq.common.message.Message;
import org.eclipse.paho.mqttv5.client.IMqttToken;
import org.eclipse.paho.mqttv5.client.MqttAsyncClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MqttMessageListener implements MQListener<Message> {
   private static final Logger log = LoggerFactory.getLogger(MqttMessageListener.class);
   private final MqttAsyncClient mqttAsyncClient;

   public MqttMessageListener(MqttAsyncClient mqttAsyncClient) {
      this.mqttAsyncClient = mqttAsyncClient;
   }

   public void onMessage(Message message) {
      try {
         IMqttToken mqttToken = this.mqttAsyncClient.publish(message.getProperty("__SUB_TOPIC"), message.getBody(), 2, false);
         mqttToken.waitForCompletion();
         if (mqttToken.getException() != null) {
            throw mqttToken.getException();
         }
      } catch (Throwable var3) {
         log.error("MQTT message to [{}] publish failed:{}", message.getProperty("__SUB_TOPIC"), var3.getMessage());
         throw new RuntimeException(var3);
      }
   }
}
