package com.qiaoba.common.redis.manager;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * RedisChannelManager
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/7/17 10:18
 */
@Component
@RequiredArgsConstructor
public class RedisChannelManager {

    private final RedisMessageListenerContainer messageListenerContainer;

    public void addChannelListener(String channelName, MessageListener listener) {
        ChannelTopic topic = new ChannelTopic(channelName);
        messageListenerContainer.addMessageListener(listener, topic);
        messageListenerContainer.start();
    }

    public void removeChannelListener(String channelName, MessageListener listener) {
        ChannelTopic topic = new ChannelTopic(channelName);
        messageListenerContainer.removeMessageListener(listener, topic);
    }
}
