• springboot整合activeMq 跳坑


    安装

      activeMq 安装请看我的另一篇https://www.cnblogs.com/milicool/p/8420926.html

    版本

      springboot 2.0.5.RELEASE

    项目结构

    POM.xml

    我这里开启了activemq连接池, 毕竟管理一下连接才更合理

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     4     <modelVersion>4.0.0</modelVersion>
     5 
     6     <groupId>com.activemq</groupId>
     7     <artifactId>demo</artifactId>
     8     <version>0.0.1-SNAPSHOT</version>
     9     <packaging>jar</packaging>
    10 
    11     <name>demo</name>
    12     <description>Demo project for Spring Boot activeMq</description>
    13 
    14     <parent>
    15         <groupId>org.springframework.boot</groupId>
    16         <artifactId>spring-boot-starter-parent</artifactId>
    17         <version>2.0.5.RELEASE</version>
    18         <relativePath/> <!-- lookup parent from repository -->
    19     </parent>
    20 
    21     <properties>
    22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    24         <java.version>1.8</java.version>
    25     </properties>
    26 
    27     <dependencies>
    28         <dependency>
    29             <groupId>org.springframework.boot</groupId>
    30             <artifactId>spring-boot-starter-activemq</artifactId>
    31         </dependency>
    32         <dependency>
    33             <groupId>org.springframework.boot</groupId>
    34             <artifactId>spring-boot-starter-web</artifactId>
    35         </dependency>
    36 
    37         <dependency>
    38             <groupId>org.springframework.boot</groupId>
    39             <artifactId>spring-boot-starter-test</artifactId>
    40             <scope>test</scope>
    41         </dependency>
    42 
    43         <!-- activemq连接池 -->
    44         <dependency>
    45             <groupId>org.apache.activemq</groupId>
    46             <artifactId>activemq-pool</artifactId>
    47             <version>5.14.5</version>
    48         </dependency>
    49 
    50         <!-- fastjson -->
    51         <dependency>
    52             <groupId>com.alibaba</groupId>
    53             <artifactId>fastjson</artifactId>
    54             <version>1.2.38</version>
    55         </dependency>
    56     </dependencies>
    57 
    58     <build>
    59         <plugins>
    60             <plugin>
    61                 <groupId>org.springframework.boot</groupId>
    62                 <artifactId>spring-boot-maven-plugin</artifactId>
    63             </plugin>
    64         </plugins>
    65     </build>
    66 </project>

    application.yml

    生产者

     1 /**
     2  * 生产者
     3  * @author milicool
     4  * Created on 2018/9/13
     5  */
     6 @Service
     7 public class Producer {
     8 
     9     /** JmsMessagingTemplate是对jmsTemplate的封装 */
    10     @Autowired
    11     private JmsMessagingTemplate jmsTemplate;
    12 
    13     /** 这里参数用Queue更好 */
    14     public void sendTestMessage(Queue queue, final String message) {
    15         jmsTemplate.convertAndSend(queue, message);
    16     }
    17 }

    消费者

    消费者

    测试类

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class DemoApplicationTests {
        private Logger log = LoggerFactory.getLogger(DemoApplicationTests.class);
    
        @Autowired
        private Producer producer;
    
        @Test
        public void contextLoads() {
            Queue queue = new ActiveMQQueue("spring_queue_test");
            for (int i = 0; i < 5; i++) {
                String msg = "hello world, 序号: " + i;
                producer.sendTestMessage(queue, msg);
                log.info("发送队列, msg: {}" + msg);
            }
        }
    }

    结果

    感谢观看哦

  • 相关阅读:
    常用公共配置类——定时任务配置
    常用公共工具类——生成token
    常用公共工具类——分页工具类(IPage)
    常用公共工具类——Shiro工具类
    常用公共工具类——Redis工具类
    fmt.sprintf,fprintf区别
    web界面不显示内容排查思路
    golang值传递和引用传递
    web开发
    聊聊crd和list-watch
  • 原文地址:https://www.cnblogs.com/milicool/p/9644079.html
Copyright © 2020-2023  润新知