springboot整合各种消息队列(二):rabbitmq消息队列

栏目: Java · 发布时间: 5年前

内容简介:请参考笔者另一篇博客,有详细介绍:新建一个springboot项目,并且修改application.yml文件,pom如下:

springboot整合各种消息队列(二):rabbitmq消息队列 本篇博客将介绍使用rabbitmq消息中间件和springboot的整合使用;

安装配置rabbitmq

请参考笔者另一篇博客,有详细介绍: https://jsbintask.cn/2019/01/25/middleware/docker-rabbitmq-install/#more

整合springboot

新建项目

新建一个springboot项目,并且修改application.yml文件,pom如下:

spring:
  rabbitmq:
    host: yourhostaddress
    username: jsbintask
    password: jsbintask

host和password修改成自己的服务器用户名密码。pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.jsbintask</groupId>
    <artifactId>springboot-rabbitmq-learning</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-rabbitmq-learning</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

消息消费者(接收者)

编写一个消息消费者类:

@Component
@Log
public class RabbitmqMsgReceiver {
    @Autowired
    private CountDownLatch countDownLatch;

    public void receivedMsg(String msg) {
        log.info("received rabbitmq msg: " + msg);
        countDownLatch.countDown();
    }
}

并且加入@Component注解,将其作为bean归spring管理,并且通过@Autowried注入了一个CountdownLatch类。

配置exchange,queue,route,加入监听:

@Configuration
public class RabbitmqConfig {
    public static final String TOPIC_EXCHANGE_NAME = "jsbintask-exchange";
    public static final String ROUTE_KEY = "cn.jsbintask.key";

    private static final String QUEUE_NAME = "jsbintask-queue";


    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME, false);
    }

    @Bean
    public CountDownLatch countDownLatch() {
        return new CountDownLatch(1);
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(TOPIC_EXCHANGE_NAME);
    }

    /**
     * 将queue和exchange绑定,并且已 route_key暴漏出去
     */
    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTE_KEY);
    }

    @Bean
    public SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
                                             MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(QUEUE_NAME);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    public MessageListenerAdapter listenerAdapter(RabbitmqMsgReceiver receiver) {
        return new MessageListenerAdapter(receiver, "receivedMsg");
    }
}

启动测试类,发送消息

@SpringBootApplication
@Log
public class SpringbootRabbitmqLearningApplication {

    public static void main(String[] args) throws Exception{
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootRabbitmqLearningApplication.class, args);
        CountDownLatch countDownLatch = context.getBean(CountDownLatch.class);
        RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);

        log.info("Sending msg....");
        rabbitTemplate.convertAndSend(RabbitmqConfig.TOPIC_EXCHANGE_NAME, RabbitmqConfig.ROUTE_KEY, "hello from jsbintask.");
        countDownLatch.await();

        System.exit(-1);
    }

}

启动,查看rabbitmq控制台以及idea控制台:收到消息并且打印:

springboot整合各种消息队列(二):rabbitmq消息队列 springboot整合各种消息队列(二):rabbitmq消息队列

这样rabitmq和springboot整合案例就完成了。 源码地址: https://github.com/jsbintask22/springboot-rabbitmq-learning.git

本文原创地址: https://jsbintask.cn/2019/01/25/springboot/springboot-rabbitmq-jms/ ,未经允许,禁止转载。

谢谢你支持我分享知识

springboot整合各种消息队列(二):rabbitmq消息队列

扫码打赏,心意已收

springboot整合各种消息队列(二):rabbitmq消息队列

打开 微信 扫一扫,即可进行扫码打赏哦


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

编码

编码

[美] Charles Petzold / 左飞、薛佟佟 / 电子工业出版社 / 2010 / 55.00元

本书讲述的是计算机工作原理。作者用丰富的想象和清晰的笔墨将看似繁杂的理论阐述得通俗易懂,你丝毫不会感到枯燥和生硬。更重要的是,你会因此而获得对计算机工作原理较深刻的理解。这种理解不是抽象层面上的,而是具有一定深度的。一起来看看 《编码》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换