package com.brilliant.ecserver.config;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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.core.JmsMessagingTemplate;
import javax.jms.ConnectionFactory;
/**
* @ClassName AmpConfig
* @Description ActiveMQ配置类,分别创建2个不同地址的MQ连接
* @Author chengli
* @Date 2021/8/17 14:04
*/
@Configuration
public class AmqConfig {
@Bean (name = "mq19ConnectionFactory" )
public ActiveMQConnectionFactory mq19ConnectionFactory(
@Value ( "${spring.activemq19.broker-url}" ) String brokerUrl,
@Value ( "${spring.activemq19.user}" ) String username,
@Value ( "${spring.activemq19.password}" ) String password) {
return createConnectionFactory(brokerUrl, username, password);
}
@Bean (name = "mq19JmsTemplate" )
public JmsMessagingTemplate mq19JmsTemplate(
@Qualifier ( "mq19ConnectionFactory" ) ActiveMQConnectionFactory connectionFactory) {
JmsMessagingTemplate template = new JmsMessagingTemplate(connectionFactory);
return template;
}
@Bean (name = "mq19JmsListenerContainerFactory" )
public JmsListenerContainerFactory mq19JmsListenerContainerFactory(
@Qualifier ( "mq19ConnectionFactory" ) ActiveMQConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setPubSubDomain( true );
factory.setConnectionFactory(connectionFactory);
return factory;
}
//========================================================================================
@Bean (name = "mq18ConnectionFactory" )
public ActiveMQConnectionFactory mq18ConnectionFactory(
@Value ( "${spring.activemq18.broker-url}" ) String brokerUrl,
@Value ( "${spring.activemq18.user}" ) String username,
@Value ( "${spring.activemq18.password}" ) String password) {
return createConnectionFactory(brokerUrl, username, password);
}
@Bean (name = "mq18JmsTemplate" )
public JmsMessagingTemplate mq18JmsTemplate(
@Qualifier ( "mq18ConnectionFactory" ) ActiveMQConnectionFactory connectionFactory) {
JmsMessagingTemplate template = new JmsMessagingTemplate(connectionFactory);
return template;
}
@Bean (name = "mq18JmsListenerContainerFactory" )
public JmsListenerContainerFactory mq18JmsListenerContainerFactory(
@Qualifier ( "mq18ConnectionFactory" ) ConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setPubSubDomain( true );
factory.setConnectionFactory(connectionFactory);
factory.setAutoStartup( true );
return factory;
}
//========================================================================================
/**
* 创建ConnectionFactory
* @param brokerUrl
* @param username
* @param password
* @return
*/
public ActiveMQConnectionFactory createConnectionFactory(String brokerUrl, String username, String password) {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
factory.setBrokerURL(brokerUrl);
factory.setUserName(username);
factory.setPassword(password);
return factory;
}
}
|