Springboot整合Activemq

  1. 依赖配置

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-activemq</artifactId>
                <version>2.0.4.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-pool</artifactId>
                <version>5.7.0</version>
    </dependency>
    
  2. yml文件配置

    spring:
      activemq:
        broker-url: tcp://192.168.122.128:61616
        in-memory: false
        pool:
          #true表示使用连接池
          enabled: false
          #连接池最大连接数
          max-connections: 5
          #空闲的连接过期时间,默认为30秒
          idle-timeout: 30000
        packages:
          trust-all: true
        user: admin
        password: admin
      jms:
        #订阅模式要设置为true,默认false
        pub-sub-domain: true
    
  3. 配置启动类

    @SpringBootApplication
    @ComponentScan("spring")
    @EnableJms//启用消息队列
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    
  4. 配置初始化文件

    @Configuration
    public class Config {
    	//点对点模式consumer
        @Bean
        public Queue queue() {
            return new ActiveMQQueue("consumer");
        }
        //订阅模式topic
        @Bean
        public Topic topic(){
            return new ActiveMQTopic("topic");
        }
    }
    
  5. 创建生产者

    @Component
    @EnableScheduling
    public class MqProducer {
        @Autowired
        private JmsMessagingTemplate jmsMessagingTemplate;
    	
        @Autowired
        private Queue queue;
    	
        @Autowired
        private Topic topic;
    	
    	//点对点模式
        @Scheduled(fixedDelay = 2000)    // 每2s执行1次
        public void send1() {
            this.jmsMessagingTemplate.convertAndSend(this.queue, "生产者消息Queue");
        }
    	
    	//订阅模式
        //@Scheduled(fixedDelay = 2000)    // 每2s执行1次
        //public void send() {
        //    this.jmsMessagingTemplate.convertAndSend(this.topic, "生产者消息Topic");
        //}
    }
    
  6. 创建消费者

    @Component
    public class MqConsumer {
    	//点对点模式
        @JmsListener(destination = "consumer")
        public void receiveQueue(String consumer) {
            System.out.println("消费者1:" + consumer);
        }
    	//订阅模式
        //@JmsListener(destination = "topic")
        //public void receiveQueue1(String consumer) {
        //    System.out.println("消费者2:" + consumer);
        //}
    }
    
  7. 测试

    1. 订阅模式,此时需要设置yml文件 pub-sub-domain: true;发布数据时,如果没有用户接收,数据会丢失。多个用户的话,每个用户都会接收相同的数据。
    2. 点对点模式,,此时需要设置yml文件 pub-sub-domain: false;发布数据,发送给指定用户,(如果多个同名用户,不会重复消费)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

summer_du

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值