package net.northking.cctp.mobile.config;


import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: WuJinQuan
 * @date:2021-11-05 14:33
 **/
@Configuration
public class FixedReplyConfig {

    private final int TIMEOUT = 240;
    private final int CONCURRENT_CONSUMERS = 1;
    private final int PREFETCH_COUNT = 5;

    @Bean
    public RabbitTemplate fixedRabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        template.setReplyTimeout(TIMEOUT);
        return template;
    }

    @Bean("fixedListenerContainer")
    public SimpleMessageListenerContainer fixedListenerContainer(ConnectionFactory connectionFactory) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);        //这一步非常重要，固定队列模式要，一定要主动设置  SimpleMessageListenerContainer监听容器，监听应答队列
        container.setAcknowledgeMode(AcknowledgeMode.AUTO);//设置确认模式为手工确认
        container.setConcurrentConsumers(CONCURRENT_CONSUMERS);
        container.setPrefetchCount(PREFETCH_COUNT);
        return container;
    }

}
