springboot配置多个Activemq

环境配置

springboot 1.5.10.RELEASE

配置文件

spring.inner.activemq.brokerUrl=tcp://1.1.1.1:61616?wireFormat.maxInactivityDuration=10000
spring.inner.activemq.user=admin
spring.inner.activemq.password=admin

spring.saas.activemq.brokerUrl=tcp://1.1.1.2:61616?wireFormat.maxInactivityDuration=10000
spring.saas.activemq.user=admin
spring.saas.activemq.password=admin

active mqconfig

package com.jms.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.context.annotation.Primary;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsMessagingTemplate;

/**
 * activemq配置多数据源
 *
 * @author sulibo
 * @date 2018年8月23日
 */
@Configuration
public class ActiveMqConfig {

	@Bean(name = "innerConnectionFactory")
	@Primary
	public ActiveMQConnectionFactory firstConnectionFactory(
			@Value("${spring.inner.activemq.brokerUrl}") String brokerUrl,
			@Value("${spring.inner.activemq.user}") String username,
			@Value("${spring.inner.activemq.password}") String password) {
		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
		factory.setBrokerURL(brokerUrl);
		factory.setUserName(username);
		factory.setPassword(password);
		return factory;
	}

	@Bean(name = "saasConnectionFactory")
	public ActiveMQConnectionFactory secondConnectionFactory(
			@Value("${spring.saas.activemq.brokerUrl}") String brokerUrl,
			@Value("${spring.saas.activemq.user}") String username,
			@Value("${spring.saas.activemq.password}") String password) {
		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
		factory.setBrokerURL(brokerUrl);
		factory.setUserName(username);
		factory.setPassword(password);
		return factory;
	}

	@Bean(name = "innerActivemqTemplate")
	@Primary
	public JmsMessagingTemplate innerActivemqTemplate(
			@Qualifier("innerConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
		JmsMessagingTemplate template = new JmsMessagingTemplate(connectionFactory);
		return template;
	}

	@Bean(name = "sasaActivemqTemplate")
	public JmsMessagingTemplate sasaActivemqTemplate(
			@Qualifier("saasConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
		JmsMessagingTemplate template = new JmsMessagingTemplate(connectionFactory);
		return template;
	}

	@Bean(name = "innerFactory")
	public JmsListenerContainerFactory innerFactory(
			@Qualifier("innerConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
		DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
		factory.setConnectionFactory(connectionFactory);
		return factory;
	}

	@Bean(name = "saasFactory")
	public JmsListenerContainerFactory saasFactory(
			@Qualifier("saasConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
		DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
		factory.setConnectionFactory(connectionFactory);
		return factory;
	}
}

consumer

@Component
@Log4j
public class SaasConsumer {
	
	
	@JmsListener(destination = "ALIM_PHASE_COUNT",containerFactory="saasFactory")
	private void consume(final Message msg) throws JMSException, Exception{

    }
@Component
@Log4j
public class InnerConsumer {
	
	
	@JmsListener(destination = "ALIM_PHASE_COUNT",containerFactory="innerFactory")
	private void consume(final Message msg) throws JMSException, Exception{
    
    }

producer

package com.jms.producer;

import javax.jms.Destination;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

/**
 * 生成推送大数据猫头鹰监控数据
 *
 * @author sulibo
 * @date 2018年4月19日
 */
@Service
public class RunlogProducer {
    /**
     * 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
     */
    @Autowired
    private JmsMessagingTemplate innerActivemqTemplate;


    /**
     * 发送消息,destination是发送到的队列,message是待发送的消息
     * @param destination 使用JMS具体实现
     * @param message 消息内容
     */
    public void sendMessage(Destination destination, final String message){
    	innerActivemqTemplate.convertAndSend(destination, message);
    }
}

 

你可能感兴趣的:(springboot配置多个Activemq)