SpringBoot整合ActiveMQ分析

虾米哥 阅读:364 2021-06-15 12:15:19 评论:0

一、引入外部依赖 https://mvnrepository.com/

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-activemq</artifactId> 
    <version>2.4.4</version> 
</dependency>

<dependency> 
    <groupId>org.apache.activemq</groupId> 
    <artifactId>activemq-pool</artifactId> 
    <version>5.16.1</version> 
</dependency>

二、代码

spring.activemq.broker-url=tcp://192.168.1.xxx:61616 
spring.activemq.in-memory=true 
spring.activemq.pool.enabled=false 
spring.activemq.user=admin 
spring.activemq.password=admin
package com.example.controller; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.jms.core.JmsTemplate; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 
 
@Controller 
public class ActiveMQController { 
 
    @Autowired 
    private JmsTemplate jmsTemplate; 
 
    @RequestMapping("/activeMQ") 
    @ResponseBody 
    public String tests(){ 
        jmsTemplate.convertAndSend("testname","testMsg"); 
        return "ok"; 
    } 
}
package com.example.activemq; 
 
import org.springframework.jms.annotation.JmsListener; 
import org.springframework.stereotype.Component; 
 
@Component 
public class Consumer { 
 
    @JmsListener(destination = "testName") 
    public void receiveMsg(String text){ 
        System.out.println(text+"....."); 
    } 
}

标签:Spring Boot
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

一个IT知识分享的公众号