spring Boot集成ActiveMQ

1、下载ActiveMQ,解压,

 在bin\win64运行activemq.bat脚本,注意运行前先检查jdk与activeMq是否匹配

成功后http://localhost:8161/admin,账号admin,密码admin

 

 2、项目中添加依赖(生产、消费)

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!--消息队列连接池-->
<dependency>
   <groupId>org.apache.activemq</groupId>
   <artifactId>activemq-pool</artifactId>
   <version>5.15.0</version>
</dependency>

3、添加配置(生产、消费)

# activeMq 配置
activemq:
    broker-url: tcp://127.0.0.1:61616
    user: admin
    password: admin
    pool:
        # 使用flase,此处改为true报错,不清楚什么原因
        enabled: false
        max-connections: 50
jms:
    listener:
        #同时消费数
        concurrency: 3
        #最大消费数
        max-concurrency: 3
    pub-sub-domain: false #配置消息的类型,如果是true则表示为topic消息,如果为false表示Queue消息

4、配置类(生产)

@Configuration
public class ActiveMqConfig {
        /**
         * 队列模式
         * @return
         */
        @Bean
        public Queue queue() {
            return new ActiveMQQueue("sms.queue");
        }

        /**
         * 广播模式
         * @return
         */
        @Bean
        public Topic topic() {
            return new ActiveMQTopic("sms.topic");
        }

        /**
         * 发布-订阅模式的ListenerContainer
         */
        @Bean
        public JmsListenerContainerFactory<?> topicListenerFactory(ConnectionFactory connectionFactory) {
            DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
            factory.setPubSubDomain(true);
            factory.setConnectionFactory(connectionFactory);
            return factory;
        }
        /**
         * P2P模式的ListenerContainer
         */
        @Bean
        public JmsListenerContainerFactory<?> queueListenerFactory(ConnectionFactory connectionFactory) {
            DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
            factory.setPubSubDomain(false);
            factory.setConnectionFactory(connectionFactory);
            return factory;
        }
}

5、启动类,添加@EnableJms注解(生产、消费)

6、生产消息(生产)


@Component
public class Producer {

    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;
    /**
     * 队列
     */
    @Resource
    private Queue queue;
    public void sendMsg(String msg) {
        System.out.println("发送消息内容 :" + msg);
        this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
    }

    /**
     * 广播
     */
    @Resource
    private Topic topic;
    public void sendTopic(String msg) {
        System.out.println("发送Topic消息内容 :"+msg);
        this.jmsMessagingTemplate.convertAndSend(this.topic, msg);
    }
}

7、消费者(消费端)

@Component
public class Consumer {

    // 使用JmsListener配置消费者监听的队列,其中name是接收到的消息
    @JmsListener(destination = "sms.queue")
    public void handleMessage(String name) {
        System.out.println("接收到queue消息:" + name);
    }


    // 使用JmsListener配置消费者监听的队列,其中name是接收到的消息
    @JmsListener(destination = "sms.topic")
    public void receiveTopic1(String name) {
        System.out.println("接收到topic消息:" + name);
    }
}

8、生产消息

这时候消费端会接受到消息。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值