• SpringBoot 整合ActiveMQ


    队列 - 生产者

    1)新建Maven工程

    clipboard

    2)引入依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <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.atguigu</groupId>
        <artifactId>boot-mq-producer</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.0.RELEASE</version>
            <relativePath/> 
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <!--spring boot整合activemq的jar包-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-activemq</artifactId>
                <version>2.1.5.RELEASE</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

    3)springboot启动类

    @SpringBootApplication
    public class MainApp_Produce {
        public static void main(String[] args) {
            SpringApplication.run(MainApp_Produce.class,args);
        }
    }

    4)编写 application.yml

    server:
      port: 7777
    
    spring:
      activemq:
        broker-url: tcp://192.168.2.180:61616 #MQ服务器地址
        user: admin
        password: admin
      jms:
        pub-sub-domain: false #false = queue   true = topic
    
    # 自己定义队列名称
    myqueue: boot-activemq-queue

    5)配置bean,向ioc容器中注入一个队列,并开启jms

    @Configuration
    @EnableJms  //
    public class ConfigBean {
    
        @Value("${myqueue}")
        private String myQueue;
    
        @Bean
        public Queue queue(){
            return  new ActiveMQQueue(myQueue);
        }
    }

    6)编写Queue_Produce,并注入到ioc容器

    @Component
    public class Queue_Produce {
    
     //SpringBoot已经自动将JmsMessagingTemplate注册到ioc容器中
        @Autowired
        private JmsMessagingTemplate jmsMessagingTemplate;
    
        @Autowired
        private Queue queue;
    
        public void produceMsg(){
            jmsMessagingTemplate.convertAndSend(queue,
                    "***"+ UUID.randomUUID().toString().substring(0, 6));
            System.out.println("消息推送到MQ成功");
        }
    }

    7)测试

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class TestActiveMq {
    
        @Autowired
        private Queue_Produce produce;
    
        @Test
        public void test(){
            produce.produceMsg();
        }
    }

    clipboard

    clipboard

    补充:生产者间隔定投

    //Queue_Produce类中加如下方法
    @Scheduled(fixedDelay=3000)  //三秒钟定时投递一次
    public void produceMsgSchedule(){
        jmsMessagingTemplate.convertAndSend(queue,
                "***"+ UUID.randomUUID().toString().substring(0, 6));
        System.out.println("消息推送到MQ成功");
    }
    
    // 主配置类中加如下注解
    @EnableScheduling

    队列 - 消费者

    1)新建maven工程

    clipboard

    2)引入pom文件(同上)

    3)SpringBoot启动类

    @SpringBootApplication
    public class MainApp_Consumer {
        public static void main(String[] args) {
            SpringApplication.run(MainApp_Consumer.class,args);
        }
    }

    4)配置文件 application.xml

    server:
      port: 7777
    
    spring:
      activemq:
        broker-url: tcp://192.168.2.180:61616 #MQ服务器地址
        user: admin
        password: admin
      jms:
        pub-sub-domain: false #表示本次目的地是队列还是主题  false = queue   true = topic
    
    # 自己定义队列名称
    myqueue: boot-activemq-queue

    5) Queue_Consumer

    @Component
    public class Queue_Consumer {
    
        @JmsListener(destination = "${myqueue}")
        public void receive(TextMessage textMessage) throws JMSException {
            System.out.println("消费者接收到消息"+textMessage.getText());
    
        }
    }

    启动消费者

    clipboard

  • 相关阅读:
    那些离不开的 Chrome 扩展插件
    Spring Boot 实战 —— 入门
    Maven 学习笔记
    Linux lvm 分区知识笔记
    Linux 双向 SSH 免密登录
    CentOS Yum 源搭建
    Ubuntu 系统学习
    iOS 测试三方 KIF 的那些事
    Swift 网络请求数据与解析
    iOS Plist 文件的 增 删 改
  • 原文地址:https://www.cnblogs.com/houchen/p/13327871.html
Copyright © 2020-2023  润新知