• SpringBoot2.0之整合ActiveMQ【发布订阅模式】


    发布订阅模式与前面的点对点模式很类似,简直一毛一样

    注意:发布订阅模式 先启动消费者

    公用pom:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.toov5</groupId>
      <artifactId>springboot-topic-producer</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.1.RELEASE</version>
        </parent>
        <!-- 管理依赖 -->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Finchley.M7</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <!-- SpringBoot整合Web组件 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- SpringBoot Activemq -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-activemq</artifactId>
            </dependency>
        </dependencies>
        <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/libs-milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    </project>
    

     与上一篇博客类似的:改改就欧克了 猜猜都知道该怎么玩

     yml:

    spring:
      activemq:
        broker-url: tcp://192.168.91.6:61616
        user: admin
        password: admin
    my_topic: springboot-topic-toov5
    server:
      port: 8081
    

      config

    package com.toov5.config;
    
    import javax.jms.Topic;
    
    import org.apache.activemq.command.ActiveMQTopic;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ConfigQueue {
       @Value("${my_topic}")
       private String myTopic;
        
        //首先将队列注入到SpringBoot容器中去
        @Bean
        public Topic queue() {
            return new ActiveMQTopic(myTopic);
        } 
        
    }

    producer

    package com.toov5.topicProducer;
    
    import javax.jms.Topic;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.core.JmsMessagingTemplate;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class TopicProducer {
        @Autowired
        private JmsMessagingTemplate jmsMessagingTemplate;
        //把队列注入进来 
        @Autowired  //此注解默认是以类型找  在配置文件中 已经注入的  @Bean 
        private Topic topic;
        
        //每隔5s时间向队列发送消息
        @Scheduled(fixedDelay=5000)  //每间隔2s向队列发送消息
        public void send() {
            String msgString = System.currentTimeMillis()+" ";
            jmsMessagingTemplate.convertAndSend(topic,msgString);
            System.out.println("发布订阅通讯,msg"+msgString);
        }
    }

    创建producer maven

    SpringBoot 默认开启点对点的!!!! 订阅模式需要手动!!!!!

    yml中: 

    #### 开启发布订阅
    jms:
    pub-sub-domain: true

     

    yml:

    spring:
      activemq:
        broker-url: tcp://192.168.91.6:61616
        user: admin
        password: admin
    my_queue: springboot-queue-toov5
    server:
      port: 8080  

     consumer

    package com.toov5.activemqConsumer;
    
    import org.springframework.jms.annotation.JmsListener;
    import org.springframework.stereotype.Component;
    
    @Component
    public class P2PConsumer {
       
        @JmsListener(destination= "${my_queue}")    //用这个注解去监听 监听的队列
        public void receiver(String msg) {
            System.out.println("消费者成功获取到生产者的消息,msg"+msg);
        }
        
        
    }

    启动类

    package com.toov5.activemqConsumer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class AppConsumer {
    
        public static void main(String[] args) {
            SpringApplication.run(AppConsumer.class, args);
        }
        
    }

     先启动消费者 然后启动生产者,多开几个端口玩玩集群~

  • 相关阅读:
    js父窗体关闭,子窗体紧随
    jquery validate 详细说明
    MyEclipse使用汇总——MyEclipse10设备SVN插入
    与策略模式工厂工作方式之间的差
    我必须发展2048配置界面设计
    Openstack部署总结:“部署过程Error: Local ip for ovs agent must be set when tunneling is enabled”问题
    国家模式c++
    Python 产生两个方法将不被所述多个随机数的特定范围内反复
    [React] Normalize Events with Reacts Synthetic Event System
    [NPM] Pipe data from one npm script to another
  • 原文地址:https://www.cnblogs.com/toov5/p/9938135.html
Copyright © 2020-2023  润新知