content: package org.anair.spring.config.jms;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.ImportResource;
  import org.springframework.context.annotation.Primary;
  import org.springframework.jms.connection.CachingConnectionFactory;
  import org.springframework.jms.connection.JmsTransactionManager;
  import org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter;
  import org.springframework.transaction.PlatformTransactionManager;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  import com.ibm.mq.jms.MQQueueConnectionFactory;
  import com.ibm.msg.client.wmq.WMQConstants;
  
  @Configuration
  @EnableTransactionManagement
  @ImportResource(locations={"classpath:spring-jms.xml","classpath:spring-service.xml","classpath:spring-billing-disruptor.xml","classpath:spring-datastream-disruptor.xml","classpath:spring-jmx.xml"})
  public class JmsConfiguration {
  	private static final Logger LOG = LoggerFactory.getLogger(JmsConfiguration.class);
  	
  	@Value("${environment.mq.host}")
  	private String host;
  	@Value("${environment.mq.port}")
  	private int port;
  	@Value("${environment.mq.queue-manager}")
  	private String queueManager;
  	@Value("${environment.mq.channel}")
  	private String channel;
  	@Value("${environment.mq.username}")
  	private String username;
  	@Value("${environment.mq.password}")
  	private String password;
  	@Value("${environment.mq.receive-timeout ?: 2000}")
  	private long receiveTimeout;
  
  	@Bean
  	public MQQueueConnectionFactory mqQueueConnectionFactory() {
  		MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
  		mqQueueConnectionFactory.setHostName(host);
  		try {
  			mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
  			mqQueueConnectionFactory.setChannel(channel);
  			mqQueueConnectionFactory.setPort(port);
  			mqQueueConnectionFactory.setQueueManager(queueManager);
  		} catch (Exception e) {
  			LOG.error(e.getMessage(), e);
  		}
  		return mqQueueConnectionFactory;
  	}
  
  	@Bean
  	UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(
  			MQQueueConnectionFactory mqQueueConnectionFactory) {
  		UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
  		userCredentialsConnectionFactoryAdapter.setUsername(username);
  		userCredentialsConnectionFactoryAdapter.setPassword(password);
  		userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(mqQueueConnectionFactory);
  		return userCredentialsConnectionFactoryAdapter;
  	}
  
  	@Bean
  	@Primary
  	public CachingConnectionFactory cachingConnectionFactory(
  			UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter) {
  		CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
  		cachingConnectionFactory.setTargetConnectionFactory(userCredentialsConnectionFactoryAdapter);
  		cachingConnectionFactory.setSessionCacheSize(500);
  		cachingConnectionFactory.setReconnectOnException(true);
  		return cachingConnectionFactory;
  	}
  
  	@Bean
  	public PlatformTransactionManager jmsTransactionManager(CachingConnectionFactory cachingConnectionFactory) {
  		JmsTransactionManager jmsTransactionManager = new JmsTransactionManager();
  		jmsTransactionManager.setConnectionFactory(cachingConnectionFactory);
  		return jmsTransactionManager;
  	}
  
  }
  