package com.usoftchina.uas.office.dingtalk.config;

import com.usoftchina.uas.office.listener.UasEventListenerAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * @author yingp
 * @date 2020/2/16
 */
@Configuration
public class RedisConfig {

    @Bean
    public UasEventListenerAdapter uasOperationListener() {
        return new UasEventListenerAdapter();
    }

    @Bean
    public ChannelTopic uasTopic() {
        return new ChannelTopic("channel:uas");
    }

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(LettuceConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 订阅来自uas的消息
        container.addMessageListener(uasOperationListener(), uasTopic());
        return container;
    }
}
