spring boot 整合activemq

99封情书 提交于 2019-12-05 22:16:43

1 Spring BootActiveMQ整合

1.1使用内嵌服务

1)在pom.xml中引入ActiveMQ起步依赖

 

<properties>
        <spring.version>2.0.7.RELEASE</spring.version>
    </properties>

    <dependencies>
        <!--springmvc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--activemq-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
            <version>1.5.1.RELEASE</version>
        </dependency>
        <!--spirngboot热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

 

2)创建消息生产者(application 引导类 必须 和要访问的类 在同一级包下QueueController.java

@RestController
@RequestMapping("/queue")
public class QueueController {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @RequestMapping("/send")
    public void send(String text){
        jmsMessagingTemplate.convertAndSend("code",text);
    }

}

3)创建消息消费者 QueueConsumer

@Component
public class QueueConsumer {

    @JmsListener(destination = "code")
    public void readMessage(String text){
        System.out.println("接收到的消息 : " + text);
    }

}

测试:启动服务后,在浏览器执行

http://localhost:8088/send.do?text=aaaaa

即可看到控制台输出消息提示。Spring Boot内置了ActiveMQ的服务,所以我们不用单独启动也可以执行应用程序。

 

 

1.2使用外部服务

src/main/resources下的application.properties增加配置, 指定ActiveMQ的地址

spring.activemq.broker-url=tcp://192.168.25.130:61616

 

 

application.properties配置

 

# tomcat服务器端口号
server.port=8080
url=http://www.baidu.com
# activemq地址
spring.activemq.broker-url=tcp://192.168.200.128:61616

 

运行后,会在activeMQ中看到发送的queue 

 

 

1.3发送Map信息

1)向QueueController.java添加代码

 

@RequestMapping("/sendmap")
    public void sendMap(){
        Map map = new HashMap();
        map.put("name", "曜");
        map.put("sex", "男");
        map.put("age", "18");
        jmsMessagingTemplate.convertAndSend("codemap",map);
    }

 

2)向Consumer.java添加代码

 

 @JmsListener(destination = "codemap")
    public void readMap(Map map){
        System.out.println(map);
    }

 

http://localhost:8088/sendmap

控制台打印

 

 

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!