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.MessageListener;
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;

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

    @Resource
    private RedisConnectionFactory redisConnectionFactory;

    @Resource
    private MessageListener messageListener;

    @Resource
    MyThreadPool myThreadPool;

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