package com.apobates.forum.letterbox.impl;

import com.apobates.forum.letterbox.impl.event.ForumLetterPostEvent;
import com.apobates.forum.letterbox.impl.event.ForumLetterPostInboxListener;
import com.github.davidmarquis.redisq.RedisMessageQueue;
import com.github.davidmarquis.redisq.consumer.MessageConsumer;
import com.github.davidmarquis.redisq.producer.DefaultMessageProducer;
import com.github.davidmarquis.redisq.producer.MessageProducer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;

/**
 *
 * @author xiaofanku
 * @since 20200511
 */
@Configuration
@ComponentScan(basePackages={"com.apobates.forum.letterbox.impl"}, useDefaultFilters=false, includeFilters={@Filter(classes={org.springframework.stereotype.Service.class}),@Filter(classes={org.springframework.stereotype.Repository.class}),@Filter(classes={org.springframework.stereotype.Component.class})})
public class LetterAppConfig {
    // loop queue
    @Bean("letterPostEventQueue")
    public RedisMessageQueue letterPostEventQueue(){
        RedisMessageQueue mq =  new com.github.davidmarquis.redisq.RedisMessageQueue();
        mq.setQueueName("letter:PostEvent");
        return mq;
    }
    // loop Producer
    @Bean("letterPostEventProducer")
    public MessageProducer<ForumLetterPostEvent> letterPostEventProducer(@Qualifier("letterPostEventQueue") RedisMessageQueue letterPostEventQueue){
        DefaultMessageProducer<ForumLetterPostEvent> dmq = new com.github.davidmarquis.redisq.producer.DefaultMessageProducer<>();
        dmq.setQueue(letterPostEventQueue);
        return dmq;
    }
    // loop consumer
    @Bean
    public MessageConsumer<ForumLetterPostEvent> letterdeliverConsumer(
            @Qualifier("letterPostEventQueue") RedisMessageQueue letterPostEventQueue,
            @Qualifier("letterdeliver") ForumLetterPostInboxListener letterdeliver){
        MessageConsumer<ForumLetterPostEvent> messageConsumer = new com.github.davidmarquis.redisq.consumer.MessageConsumer<>();
        messageConsumer.setQueue(letterPostEventQueue);
        messageConsumer.setConsumerId("ForumLetterPostEvent:deliver");
        messageConsumer.setMessageListener(letterdeliver);
        return messageConsumer;
    }
}
