package com.fdkankan.modeling.config;

import com.fdkankan.modeling.receiver.BuildSceneReceiver;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class MQReceiverConfig {
    private static Logger logger = LoggerFactory.getLogger("programLog");

    @Value("${rocketmq.name-srv-addr}")
    private String nameSrvAddr;

    @Value("${rocketmq.consumer.testGrop}")
    private String consumerGroupName;

    @Value("${rocketmq.topic.testGrop}")
    private String topicName;

    @Value("${rocketmq.tag.testGrop}")
    private String tagName;

    @Autowired
    private BuildSceneReceiver buildSceneReceiver;

    @PostConstruct
    public void CalUserTradeAmountConsumer() throws MQClientException {
        // Instantiate with specified consumer group name.
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(consumerGroupName);

        // Specify name server addresses.
        consumer.setNamesrvAddr(nameSrvAddr);
        consumer.setInstanceName(consumerGroupName);
        //广播
        consumer.setMessageModel(MessageModel.BROADCASTING);
        //一次最大消费多少数量消息，与List<MessageExt>数组数量有关，默认为1
        //  consumer.setConsumeThreadMax(batchMaxSize);
        //设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费
        //如果非第一次启动，那么按照上次消费的位置继续消费
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

        // Subscribe one or more topics to consume.
        consumer.subscribe(topicName, tagName);

        // Register callback to execute on arrival of messages fetched from brokers.
        consumer.registerMessageListener(buildSceneReceiver);

        //Launch the consumer instance.
        consumer.start();
    }

}
