1
zhang
1 天以前 23182f2c951df5fa55e70e30ff70ddaf91199a2e
zy-acs-hex/src/main/java/com/zy/acs/hex/consumer/UpMessageListener.java
@@ -1,18 +1,19 @@
package com.zy.acs.hex.consumer;
import com.rabbitmq.client.Channel;
import com.zy.acs.hex.constant.InfluxDBConstant;
import com.zy.acs.hex.constant.RabbitConstant;
import com.zy.acs.hex.consumer.listener.AbstractListener;
import com.zy.acs.hex.domain.Device;
import com.zy.acs.hex.utils.ReflectionUtils;
import com.zy.acs.hex.utils.StrUtils;
import com.zy.component.influxdb.service.InfluxDBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
import java.io.IOException;
/**
 * 消费者
@@ -27,30 +28,15 @@
public class UpMessageListener implements AbstractListener {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private InfluxDBService influxDBService;
    @RabbitHandler
    public void handle(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) {
        log.info("receive up message: {}" ,  msg);
        try {
            channel.basicAck(tag,true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 进入消息消费业务逻辑。
        //String data = new String(message.getBody());
        //log.info("收到消息:{}" ,data );
        //influxDBService.writeData();
    public void handle(Device msg, @Header(AmqpHeaders.RECEIVED_ROUTING_KEY) String routingKey, Channel channel) {
        log.info("routingKey:{},receive up message:{}", routingKey, msg);
        influxDBService.writeData(InfluxDBConstant.DEVICE_MEASUREMENT, StrUtils.getTagsByRoutingKey(routingKey), ReflectionUtils.convertBean2Map(msg));
    }
}