package me.chyxion.tigon.redis.pub.support;

import me.chyxion.tigon.redis.pub.PubProducer;
import me.chyxion.tigon.redis.util.PubSubUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @version 0.0.1
 * @since 0.0.1
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Nov 7, 2015 5:07:50 PM
 */
public class PubProducerSupport 
    implements PubProducer {
    @Autowired
    private RedisTemplate<String, Object> redisTpl;
    @Autowired
    private RedisTemplate<String, String> strRedisTpl;

    /**
     * {@inheritDoc}
     */
    @Override
    public void publish(final String topic, final Object message) {
        if (message != null) {
            if (message instanceof CharSequence) {
                strRedisTpl.opsForList().rightPush(PubSubUtils.queueKey(topic),
                    String.valueOf(message));
            }
            else {
                redisTpl.opsForList().rightPush(PubSubUtils.queueKey(topic), message);
            }
        }
        strRedisTpl.convertAndSend(MESSAGE_CHANEL, topic);
    }
}
