[ActiveMQ][SpringBoot]SpringBoot中集成ActiveMQ

配置条件:
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
@SpringBootApplication
public 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")
@RestController
public 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;

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

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

使用postman测试吧

这里写图片描述

这里写图片描述

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值