宋子宪博客

Szx

Spring Boot整合ActiveMQ列队Queue小Demo
引入maven 用到的jar包依赖,pom.xml<?xml version="1.0"...
扫描右侧二维码阅读全文
19
2019/07

Spring Boot整合ActiveMQ列队Queue小Demo

引入maven 用到的jar包依赖,pom.xml

<?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>

    <!-- 继承父包  -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.songzixian</groupId>
    <artifactId>springboot-activemq</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <!-- spring-boot的启动类的jar包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- spring-boot的web启动的jar包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- spring-boot的启动类的测试jar包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!-- spring boot整合activemq-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>


    </dependencies>

    <!-- spring boot默认插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>   
</project>

编写applicaion.yml文件

server:
  post: 7777

spring:
  activemq:
    #ActiveMQ的地址
    broker-url: tcp://192.168.78.138:61616
    #ActiveMQ的用户名
    user: admin
    #ActiveMQ的密码
    password: admin

  jms:
    #fasle=Queue(不写默认) true=Topic
    pub-sub-domain: false

#自定义队列的名称
myqueue: boot-activemq-queue

编写消息生产者配置类

/**
 * @author songzixian
 * @create 2019-07-19 下午 8:39
 * @description
 */
@Component
public class ConfigBean {

    @Value("${myqueue}")
    private String myQueue;

    /**
     * 配置队列
     * @return
     */
    @Bean
    public Queue queue(){

        return new ActiveMQQueue(myQueue);
    }
}

编写消息生产者发送类

/**
 * @author songzixian
 * @create 2019-07-19 下午 8:44
 * @description 队列消息生产者
 */
@Component
public class Queue_Produce {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;


    @Autowired
    private Queue queue;

    //消息生成者
    public void produceMsg(){
        //发送消息
        jmsMessagingTemplate.convertAndSend(queue,"----"+UUID.randomUUID().toString().substring(0,6));
    }
}

编写SpringBoot启动类

/**
 * @author songzixian
 * @create 2019-07-19 下午 8:53
 * @description
 */
@SpringBootApplication
@EnableJms
public class MainApp_Produce {
    public static void main(String[] args) {
        SpringApplication.run(MainApp_Produce.class,args);
    }
}

编写测试类

/**
 * @author songzixian
 * @create 2019-07-19 下午 8:53
 * @description springboot整合activemq测试类
 */
@SpringBootTest(classes = MainApp_Produce.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestActionMQ {

    @Resource
    private Queue_Produce queue_produce;

    @Test
    public void testSend(){
        queue_produce.produceMsg();
    }

}

编写消息消费者类

/**
 * @author songzixian
 * @create 2019-07-19 下午 11:20
 * @description
 */
@Component
public class Queue_Consumer {

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

}

编写消息消费者启动类

/**
 * @author songzixian
 * @create 2019-07-19 下午 11:20
 * @description
 */
@SpringBootApplication
public class MainApp_Consumer {
    
    public static void main(String[] args) {
        SpringApplication.run(MainApp_Consumer.class,args);
    }
}
Last modification:July 19th, 2019 at 11:32 pm
如果觉得这篇技术文章对您有帮助,可以请博主喝一杯饮料

Leave a Comment