package cn.gov.customs.wxjy.ibmmq.config;

import com.ibm.mq.jms.MQConnectionFactory;
import com.ibm.msg.client.wmq.WMQConstants;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;

@Configuration
@AllArgsConstructor
public class MQConfig {

    private MQProperties mqProperties;
	static {
		// 彻底禁用 IBM MQ 文件日志
		System.setProperty("com.ibm.msg.client.commonservices.log.outputName", "off");
		System.setProperty("com.ibm.msg.client.commonservices.log.maxBytes", "0");
		System.setProperty("com.ibm.msg.client.commonservices.log.count", "0");
		System.setProperty("com.ibm.msg.client.commonservices.trace.outputName", "off");
		System.setProperty("com.ibm.msg.client.commonservices.trace.maxBytes", "0");
		System.setProperty("com.ibm.msg.client.commonservices.trace.count", "0");

		// 可选：重定向到标准输出（如果需要查看日志）
		// System.setProperty("com.ibm.msg.client.commonservices.log.outputName", "stdout");
	}

    @Bean
    public MQConnectionFactory mqConnectionFactory() {
        MQConnectionFactory factory = new MQConnectionFactory();
        try {
            factory.setHostName(mqProperties.getHost());
            factory.setPort(mqProperties.getPort());
            factory.setQueueManager(mqProperties.getQueueManager());
            factory.setChannel(mqProperties.getChannel());
            factory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
            factory.setStringProperty(WMQConstants.USERID, mqProperties.getUser());
            factory.setStringProperty(WMQConstants.PASSWORD, mqProperties.getPassword());
        } catch (Exception e) {
            throw new RuntimeException("MQ连接工厂初始化失败", e);
        }
        return factory;
    }

    @Bean
    public CachingConnectionFactory cachingConnectionFactory(MQConnectionFactory mqConnectionFactory) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(mqConnectionFactory);
        connectionFactory.setSessionCacheSize(500);
        connectionFactory.setReconnectOnException(true);
        return connectionFactory;
    }

    @Bean
    public JmsTemplate jmsTemplate(CachingConnectionFactory connectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        jmsTemplate.setExplicitQosEnabled(true);
        jmsTemplate.setDeliveryPersistent(true); // 消息持久化
        return jmsTemplate;
    }

    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerFactory(CachingConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        // factory.setConcurrency("1-5"); // 并发消费者数量
        factory.setConcurrency("1"); // 并发消费者数量
        factory.setSessionTransacted(true);
        return factory;
    }
}
