ActiveMQ 安装及发布订阅模式

落爺英雄遲暮 提交于 2021-01-15 19:22:05

1. 安装

官网连接

https://activemq.apache.org/using-activemq-5

 docker安装mq:

https://hub.docker.com/r/rmohr/activemq

启动命令:

docker run -d -p 61616:61616 -p 8161:8161 \
           -v conf:/opt/activemq/conf \
           -v data:/opt/activemq/data \
           rmohr/activemq

centos普通安装mq

https://www.linuxidc.com/linux/2017-12/149912.htm

 持久化相关

http://activemq.apache.org/persistence.html

 ActiveMQ 界面

http://127.0.0.1:8161/admin

默认用户名和密码

用户名:admin

密码:admin

 

2. 发布订阅模式

activemq 实现了标准的消息服务,提供点对点队列模式,以及发布订阅模式。关于点对点模式网上资料较多,结合工作中的使用情况,着重说明下发布订阅模式。
 

几个关键的类如下:

 配置文件:application.properties

spring.activemq.broker-url=tcp://10.92.xx.xx:61616
spring.activemq.user=admin
spring.activemq.password=admin

 配置类:MQConfig

@Configuration
@EnableJms
public class MQConfig implements JmsListenerConfigurer {
    @Autowired
    private JmsListenerContainerFactory<?> jmsListenerContainerTopic;


    @Bean
    public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
        final JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setDeliveryPersistent(true);
        jmsTemplate.setPubSubDomain(true);
        jmsTemplate.setConnectionFactory(connectionFactory);
        return jmsTemplate;
    }


    // @Bean 
    public ConnectionFactory connectionFactory() {
        final ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(BROKER_URL);
        connectionFactory.setPassword(BROKER_USERNAME);
        connectionFactory.setUserName(BROKER_PASSWORD);
        connectionFactory.setUseAsyncSend(true);
        return connectionFactory;
    }
    // 如果是主题模式,一定要自己注入下面的监听器容器,否则收不到消息
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(connectionFactory);
        return bean;
    }


// 这种方式可以实现动态添加不同主题的消费者,和@JmsListener 是同样的使用,两种方式都可以


@Override
public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) {
    registrar.setContainerFactory(jmsListenerContainerTopic);
    SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
    endpoint.setId("myJmsEndpoint");
    endpoint.setDestination("demo.topic");
    endpoint.setMessageListener(new MessageListener() {
        @Override
        public void onMessage(Message message) {
            try {
                TextMessage txtMsg = (TextMessage) message;
                String messageStr = txtMsg.getText();
                System.out.println("主题监听器 接收到文本消息:" + messageStr);
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    });
    registrar.registerEndpoint(endpoint);

 针对配置文件下面说说几个关键的地方:

@EnableJms

表示启用mq,会加载相关配置类。

 JmsListenerContainerFactory

如果是发布订阅模式,一定要自己注入下面的监听器容器,否则收不到消息

 ConnectionFactory

注意:如果有配置文件中有

 spring.activemq.pool.enabled=true 

默认的ConnectionFactory配置不生效,也就是配置文件里面的内容不生效,必须自己注入ConnectionFactory。不然会报错

 如果配置

 spring.activemq.pool.enabled=false

并且没有自己注入ConnectionFactory,则默认用的CachingConnectionFactory

 如果配置

spring.jms.cache.enabled=false

则ConnectionFactory的实现类为ActiveMQConectionFactory。

 

发送者Publisher

@Service
public class Publisher {
    @Autowired
    private JmsTemplate jmsTemplate;
    public void publish(String user) {
        jmsTemplate.convertAndSend("demo.topic", user);
    }
}

 

消费者TopicConsumer

@Service
public class TopicConsumer {
    @JmsListener(destination = "demo.topic", containerFactory = "jmsListenerContainerFactory")
    public void receiver1(String text) {
        System.out.println("TopicConsumer : receiver1 : " + text);
    }


    @JmsListener(destination = "demo.topic", containerFactory = "jmsListenerContainerFactory")
    public void receiver3(String text) {
        System.out.println("TopicConsumer : receiver3 : " + text);
    }
}

  

3. 参考文档:

https://zhuanlan.zhihu.com/p/112250373

http://www.hellojava.com/a/77074.html

https://www.cnblogs.com/zq-boke/p/6738895.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!