spring boot的ActiveMQ使用

本文详细介绍如何在Spring Boot项目中集成并使用ActiveMQ消息中间件。从安装配置ActiveMQ开始,逐步介绍如何通过Maven引入依赖、配置队列、实现消息的生产和消费等关键步骤。最终完成了一个简单的消息发送与接收的例子。

消息队列能够有效的降低系统请求峰值,也能够达到解耦的效果。spring boot对MQ也有比较好的支持。本文演示spring activeMQ的使用,首先在linux安装ActiveMQ:

wget https://archive.apache.org/dist/activemq/5.13.0/apache-activemq-5.13.0-bin.tar.gz

然后解压找到 bin下面的脚本启动消息中间件:

./activemq start

此时已经启动,会暴露默认端61616。
接下来开始我们的主角spring boot.
依赖的Maven :

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-spring -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-spring</artifactId>
            <version>5.15.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>

使用Java Config的方式配置队列:

@Bean
    public Queue queue() {
        return new ActiveMQQueue("micro-queue");
    }

生产者:

import javax.jms.Queue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

/**
 * @author micro
 * @date 2017年8月3日
 * @description : 
 */
@Component
public class Producer {


    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired // 注入队列
    private Queue queue;

    public void sendMessage(String msg) {
        System.out.println("生产者发送消息");
        jmsMessagingTemplate.convertAndSend(this.queue, msg);
    }
}

接下来我们编写消费者:

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * @author micro
 * @date 2017年8月3日
 * @description : 
 */
@Component
public class Comsumer {

    @JmsListener(destination = "micro-queue")
    public void recive(String msg) {
        System.out.println("接收到消息 : " + msg);
    }
}

运行:

import javax.jms.Queue;

import org.apache.activemq.command.ActiveMQQueue;
import org.junit.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.context.annotation.Bean;


import javax.jms.Queue;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;


/**
 * @author micro
 * @date 2017年8月3日
 * @description : 
 */
@SpringBootApplication
public class ActiveMqTest implements CommandLineRunner {

    public static void main(String[] args) {
        new SpringApplication(ActiveMqTest.class).run(args);
    }

    @Autowired
    private Producer producer;

    @Override
    public void run(String... args) throws Exception {
        producer.sendMessage("你好!micro");
    }

    @Bean
    public Queue queue() {
        return new ActiveMQQueue("micro-queue");
    }
}

控制台打印:

生产者发送消息
接收到消息 : 你好!micro

完成了消息的发送与接收。

最近可能会写很多实战类的东西,因为新技术第一步就是跑起来,只有能够比较熟练的使用了才有能力深入的去研究其实现原理然后进阶到更高的层次。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值