[ActiveMQ][SpringBoot]SpringBoot中集成ActiveMQ

来源:互联网 发布:亚马逊好还是淘宝好 编辑:程序博客网 时间:2024/05/29 11:29

配置条件:
springboot:
yml,
gradle;

在springboot项目中使用activemq,首先gradle(本人的项目是用gradle管理的,maven同理)中的build.gradle引入activemq依赖

compile "org.springframework.boot:spring-boot-starter-activemq"compile "org.springframework:spring-jms"

yml中的配置:

  activemq:    broker-url: tcp://你的activemq地址:你的activemq端口    user: 你的activemq管理账号    password: 你的activemq管理账号密码    in-memory: true    pool:      enabled: false

在springboot启动类Application中加入需要使用的的队列名,并且开启Jms

@EnableJms@SpringBootApplicationpublic class RestapiApplication {    //就是这里嘛    @Bean    public Queue queue() {        return new ActiveMQQueue("sample.queue");    }    public static void main(String[] args) {        SpringApplication.run(RestapiApplication.class, args);    }}

activemq的controller,这里简单做个使用demo,主要是方面大家用postman进行队列消息生产;

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.jms.Queue;@RequestMapping(value = "/jms")@RestControllerpublic class ActiveMQController {    @Autowired    private JmsMessagingTemplate jmsMessagingTemplate;    @Autowired    private Queue queue;    //这里就是队列消息生产,方便使用postman测试    @RequestMapping("/sendMsg")    public void send(String msg) {        this.jmsMessagingTemplate.convertAndSend(this.queue, msg);    }}

ok,接下来写个测试用的消费者

import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Component;@Componentpublic class Consumer {    //JmsListener注解监听队列    @JmsListener(destination = "sample.queue")    public void receiveQueue(String text) {        System.out.println(text);    }}

ok,生产者,消费者,springboot配置都ok;

使用postman测试吧

这里写图片描述

这里写图片描述

一个,超级精简的Activemq入门;

原创粉丝点击