package com.qmth.teachcloud.task.config;

import com.qmth.teachcloud.common.enums.MqTagEnum;
import com.qmth.teachcloud.common.threadPool.MyThreadPool;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @Description: redis消息队列
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2021/7/9
 */
@Configuration
public class RedisListenerConfig {

    @Resource
    private RedisConnectionFactory redisConnectionFactory;

    @Resource
    private RedisMessageListener messageListener;

    @Resource
    MyThreadPool myThreadPool;

    @Bean
    public RedisMessageListenerContainer initRedisContainer() {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        //redis连接工厂
        container.setConnectionFactory(redisConnectionFactory);
        //设置运行任务池
        container.setTaskExecutor(myThreadPool);
        //定义监听渠道名称为
        Topic topicPdf = new ChannelTopic(MqTagEnum.PDF.getCode());
        Topic topicFlowMqWork = new ChannelTopic(MqTagEnum.FLOW_MQ_WORK.getCode());
        Topic topicFlowMqDone = new ChannelTopic(MqTagEnum.FLOW_MQ_DONE.getCode());
        //定义监听器监听的Redis的消息
        container.addMessageListener(messageListener, Arrays.asList(topicPdf, topicFlowMqWork, topicFlowMqDone));
        return container;
    }
}
