Activemq与springboot整合

配置在gmall-service-util项目中

1 工具类ActiveMQUtil

public class ActiveMQUtil {
   PooledConnectionFactory pooledConnectionFactory = null;
   public  void init(String brokerUrl){
       ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(brokerUrl);
       pooledConnectionFactory = new PooledConnectionFactory(activeMQConnectionFactory);
       //设置超时时间
      
pooledConnectionFactory.setExpiryTimeout(2000);
       // 设置出现异常的时候,继续重试连接
      
pooledConnectionFactory.setReconnectOnException(true);
       // 设置最大连接数
      
pooledConnectionFactory.setMaxConnections(5);
   }
    // 获取连接
   
public Connection getConnection(){
        Connection connection = null;
        try {
            connection = pooledConnectionFactory.createConnection();
        } catch (JMSException e) {
            e.printStackTrace();
        }
        return  connection;
    }
}

 

 

 

 

2 配置类ActiveMQConfig

@Configuration
public class ActiveMQConfig {

    @Value("${spring.activemq.broker-url:disabled}")
    String brokerURL ;

    @Value("${activemq.listener.enable:disabled}")
    String listenerEnable;

    // 获取activeMQUtil
   
@Bean
    public ActiveMQUtil getActiveMQUtil(){
        if ("disabled".equals(brokerURL)){
            return null;
        }
        ActiveMQUtil activeMQUtil = new ActiveMQUtil();
        activeMQUtil.init(brokerURL);
        return  activeMQUtil;
    }

    @Bean(name = "jmsQueueListener")
    public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory(ActiveMQConnectionFactory activeMQConnectionFactory) {

        if("disabled".equals(listenerEnable)){
            return null;
        }
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(activeMQConnectionFactory);
        // 设置事务
       
factory.setSessionTransacted(false);
        // 手动签收
       
factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
        // 设置并发数
       
factory.setConcurrency("5");
        // 重连间隔时间
       
factory.setRecoveryInterval(5000L);
        return factory;
    }
    // 接收消息
   
@Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory (){
        ActiveMQConnectionFactory activeMQConnectionFactory =
                new ActiveMQConnectionFactory(brokerURL);
        return activeMQConnectionFactory;
    }
}

brokerURL: tcp://192.168.67.200:61616 消息的发布者:ActiveMq消息的消费者:ActiveMq,主要使用监听器: