package com.foreverwin.mesnac.common.configuration;

import org.apache.activemq.RedeliveryPolicy;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;

import javax.jms.ConnectionFactory;

//@Configuration
//@ConditionalOnProperty(prefix = "spring", name="activemq.brokerUrl")
public class ActiveMQConfig {

    @Bean
    public RedeliveryPolicy redeliveryPolicy() {
        //配置重试机制
        RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
        //重发次数,默认为6次   这里设置为0次
        redeliveryPolicy.setMaximumRedeliveries(0);
        return redeliveryPolicy;
    }

    @Bean
    public SimpleJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        //DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }

    @Bean
    public SimpleJmsListenerContainerFactory jmsTopicListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain( true );
        factory.setReplyPubSubDomain( true );
        return factory;
    }
}
