package com.elab.mq.rocket.configuration;

import com.elab.mq.rocket.listener.DefaultRocketMQListener;
import com.elab.mq.rocket.listener.TagInvokerListener;
import com.elab.mq.rocket.listener.TagRocketMQListener;
import com.elab.mq.rocket.msg.ConsumerDataStore;
import com.elab.mq.rocket.msg.DefaultConsumeMessageHook;
import com.elab.mq.rocket.msg.DefaultSendMessageHook;
import com.elab.mq.rocket.msg.MysqlDataStore;
import com.elab.mq.rocket.template.DefaultMQProducerExt;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置文件
 *
 * @author ： liukx
 * @time ： 2019/7/18 - 13:49
 */
@Configuration
public class RocketMQConfiguration {

    @Bean
    public DefaultSendMessageHook defaultSendMessageHook(@Autowired DefaultMQProducer defaultMQProducer) {
        DefaultSendMessageHook defaultSendMessageHook = new DefaultSendMessageHook();
        defaultMQProducer.getDefaultMQProducerImpl().registerSendMessageHook(defaultSendMessageHook);
        return defaultSendMessageHook;
    }

    @Bean
    @ConditionalOnBean(DefaultMQPushConsumer.class)
    public DefaultConsumeMessageHook defaultConsumeMessageHook(@Autowired DefaultMQPushConsumer defaultMQPushConsumer) {
        DefaultConsumeMessageHook defaultConsumeMessageHook = new DefaultConsumeMessageHook();
        defaultMQPushConsumer.getDefaultMQPushConsumerImpl().registerConsumeMessageHook(defaultConsumeMessageHook);
        return defaultConsumeMessageHook;
    }

    @Bean
    @ConditionalOnMissingBean(value = {ConsumerDataStore.class})
    public ConsumerDataStore consumerDataStore() {
        MysqlDataStore mysqlDataStore = new MysqlDataStore();
        return mysqlDataStore;
    }

    @Bean
    public DefaultMQProducerExt rocketMQTemplateExt(@Autowired DefaultMQProducer defaultMQProducer) {
        return new DefaultMQProducerExt(defaultMQProducer);
    }

    @Bean
    @ConditionalOnBean(value = {TagRocketMQListener.class})
    public TagInvokerListener tagInvokerListener() {
        return new TagInvokerListener();
    }

    @Bean
    @ConditionalOnBean(value = {TagRocketMQListener.class})
    public DefaultRocketMQListener defaultRocketMQListener() {
        return new DefaultRocketMQListener();
    }

}
